time_base for the stream returned when calling add_stream_from_template is None and if you try to assign it, then it gives an error about it being a decoder.
Here's code that can be used to reproduce it:
import sys
from av import open as av_open
def _main(input_filename: str, output_filename: str, set_time_base: bool):
with av_open(input_filename) as in_container, av_open(output_filename, "w") as out_container:
in_video = in_container.streams.video[0]
in_audio = in_container.streams.audio[0]
print("In:", in_video.time_base, in_audio.time_base)
out_video = out_container.add_stream_from_template(in_video)
if set_time_base:
out_video.time_base = in_video.time_base
out_audio = out_container.add_stream_from_template(in_audio)
if set_time_base:
out_audio.time_base = in_audio.time_base
print("Out:", out_video.time_base, out_audio.time_base)
for packet in in_container.demux():
out_container.mux_one(packet)
if __name__ == "__main__":
if len(sys.argv) <= 2:
print("usage:", sys.argv[0], "<input> <output>")
sys.exit(1)
_main(sys.argv[1], sys.argv[2], len(sys.argv) > 3 and sys.argv[3] in ("y", "Y"))
Running with a file will output the following:
In: 1/16000 1/8000
Out: None None
And then adding a 3rd argument of y or Y will try to set it and cause it to fail with this error:
Traceback (most recent call last):
File "/usr/src/app/test_add_stream.py", line 30, in <module>
_main(sys.argv[1], sys.argv[2], len(sys.argv) > 3 and sys.argv[3] in ("y", "Y"))
File "/usr/src/app/test_add_stream.py", line 19, in _main
out_audio.time_base = in_audio.time_base
File "av/stream.pyx", line 127, in av.stream.Stream.__setattr__
File "av/codec/context.pyx", line 541, in av.codec.context.CodecContext.time_base.__set__
RuntimeError: Cannot access 'time_base' as a decoder
time_basefor the stream returned when callingadd_stream_from_templateisNoneand if you try to assign it, then it gives an error about it being a decoder.Here's code that can be used to reproduce it:
Running with a file will output the following:
And then adding a 3rd argument of
yorYwill try to set it and cause it to fail with this error: