PyAV-Org / PyAV

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

Expose CodecContext flush_buffers #1382

Closed skeskinen closed 4 months ago

skeskinen commented 4 months ago

expose avcodec_flush_buffers()

From ffmpeg documentation (https://ffmpeg.org/doxygen/trunk/group__lavc__misc.html#gaf60b0e076f822abcb2700eb601d352a6)

void avcodec_flush_buffers  (   AVCodecContext *    avctx   )   
Reset the internal codec state / flush internal buffers.

Should be called e.g. when seeking or when switching to a different stream.

Currently with PyAV the only way to reset the CodecContext state is to call seek on the container. This has the downside that it resets the codecs for ALL the streams. This can be annoying if you are doing more advanced stuff like switching between streams midway but don't want to call seek, selectively decode only some streams based on logic, or have the decoding heads at different points in time for different streams and need to sync them up, etc. Exposing avcodec_flush_buffers makes it much easier to decode in whatever order you want or even in different orders in different streams.

I also added type hints for encode and decode since those are missing from codec/context.pyi.