Debian Codegraph @

examples/basics/remux.py

raw
1 import av
2 import av.datasets
3
4 av.logging.set_level(av.logging.VERBOSE)
5
6 input_ = av.open(av.datasets.curated("pexels/time-lapse-video-of-night-sky-857195.mp4"))
7 output = av.open("remuxed.mkv", "w")
8
9 # Make an output stream using the input as a template. This copies the stream
10 # setup from one to the other.
11 in_stream = input_.streams.video[0]
12 out_stream = output.add_stream_from_template(in_stream)
13
14 for packet in input_.demux(in_stream):
15 print(packet)
16
17 # We need to skip the empty "flushing" packet that `demux` generates at the
18 # end. Don't test `packet.dts is None` here: a valid keyframe can legitimately
19 # have no DTS (e.g. the leading reordered packets of a B-frame stream in MKV),
20 # and skipping it would drop the keyframe and corrupt the output.
21 if packet.size == 0:
22 continue
23
24 # We need to assign the packet to the new stream.
25 packet.stream = out_stream
26
27 output.mux(packet)
28
29 input_.close()
30 output.close()