PyAV-Org / PyAV

Pythonic bindings for FFmpeg's libraries.
https://pyav.basswood-io.com/
BSD 3-Clause "New" or "Revised" License
2.44k stars 359 forks source link

Expose `av_buffersink_set_frame_size` #1482

Closed rectalogic closed 1 month ago

rectalogic commented 1 month ago

Overview

Expose av_buffersink_set_frame_size for use on audio filter graph abuffersinks

Existing FFmpeg API

AudioResampler uses av_buffersink_set_frame_size internally on it's internal filter graph abuffersink. https://github.com/PyAV-Org/PyAV/blob/f5ec8ca837c41fb8905f2c1cd76e64c2c93c3624/av/audio/resampler.pyx#L98

I need a more complex graph that also uses atrim etc. but I still want to constrain the frame size. So it would be nice if av_buffersink_set_frame_size was exposed for applications to use directly.

Expected PyAV API

Maybe there should be a new add_abuffersink() API on Graph, similar to add_abuffer(), that allows more configuration of the sink?

graph = av.filter.Graph()
abuffersink = graph.add_abuffersink(frame_size=1024)

It might make sense to add add_buffersink() for video too - both of these sinks have non-str options and I'm not sure how they are configured using the pyav API currently.

>>> graph.add("buffersink").filter.options
(<av.Option pix_fmts (BINARY at *0x10) at 0x108a70fd0>,)

>>> graph.add("abuffersink").filter.options
(<av.Option sample_fmts (BINARY at *0x20) at 0x108a70e20>, <av.Option sample_rates (BINARY at *0x60) at 0x108a71060>, <av.Option channel_layouts (BINARY at *0x30) at 0x108a71090>, <av.Option channel_counts (BINARY at *0x40) at 0x108a710c0>, <av.Option ch_layouts (STRING at *0x50) at 0x108a710f0>, <av.Option all_channel_counts (BOOL at *0x58) at 0x108a71120>)

Alternatively just expose the api directly, on Graph. av_buffersink_set_frame_size doesn't seem to care what kind of filter context it is called on, or even if it is an audio filter.

graph = av.filter.Graph()
sink = graph.add("abuffersink")
graph.buffersink_set_frame_size(sink, 1024)