Make options changeable after codec or (de)muxer has been opened
Created by: Tjoppen
Overview
Currently, as best I can tell, it is only possible to set options once: when a codec or (de)muxer is opened. There are cases where one might want to change some option afterward. My use case is changing -crf in the libx264 encoder during encoding based on user preference and changing network conditions.
Existing FFmpeg API
The av_opt_set*() in libavutil/opt.h. av_opt_get*() are probably also prudent to implement.
Doxy link: https://ffmpeg.org/doxygen/trunk/group__avoptions.html
Expected PyAV API
The av_opt functions are very generic, so the easiest route would probably be to guarantee that all relevant classes have self.ptr exposed that is guaranteed to be passable to the av_opt functions. This could be hidden from the user so that they only have to pass a CodecContext, OutputContainer or InputContainer.
Example based on aiortc's h264.py:
from av.avutil import av_opt_set_int, av_opt_get_int
class H264Encoder(Encoder):
# [...]
def set_crf(self, crf: int)
av_opt_set_int(self.codec, "crf", crf) # search_flags could be optional, default to 0
def get_crf(self) -> int:
return av_opt_get_int(self.codec, "crf")
Errors returned by av_opt_get_int() could be raised as Exceptions, which simplifies the get functions like above.
It would also be useful to expose AV_OPT_SEARCH_* and optionally allow setting them on the set and get calls.