You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just noticed that a simple test I was trying to do didn't work, so I went back the Remux example and it didn't work either. I modified it to do both the video and audio and here's that:
importavimportsysav.logging.set_level(av.logging.VERBOSE)
input_game=av.open(sys.argv[1])
output=av.open(sys.argv[2], "w")
# Make an output stream using the input as a template. This copies the stream# setup from one to the other.in_video=input_game.streams.video[0]
in_audio=input_game.streams.audio[0]
#out_video = output.add_stream(template=in_video)#out_audio = output.add_stream(template=in_audio)out_video=output.add_stream_from_template(in_video)
out_audio=output.add_stream_from_template(in_audio)
forpacketininput_game.demux():
print(packet)
# We need to skip the "flushing" packets that `demux` generates.ifpacket.dtsisNone:
continueifpacket.stream==in_video:
packet.stream=out_videoelse:
packet.stream=out_audiooutput.mux(packet)
input_game.close()
output.close()
I ran it with a test file I found online ( https://dl6.webmfiles.org/video-h265.mkv ) and VLC will play the video when I run this with 13.1 but it only plays the audio when I run this with 14.0 and newer.
Thanks for the clear repro. It's a bug in the remux example, not in add_stream_from_template.
The culprit is this line:
# We need to skip the "flushing" packets that `demux` generates.ifpacket.dtsisNone:
continue
That assumes only the trailing flush packet has no DTS. But for this file the IDR keyframe legitimately demuxes with dts=None. MKV stores only PTS, and for the leading reordered (B-frame) packets libavformat can't reconstruct a DTS, so it leaves it as None. Demuxing the video stream gives:
size
pts
dts
keyframe?
115606
120
None
yes — the keyframe
30520
360
None
no
0
None
None
no — the real flush packet
So if packet.dts is None: continue throws away the keyframe. The muxer then can't start the first cluster on a keyframe and drops a couple more packets, so the stream begins mid-GOP — hence "Could not find ref with POC …" from the decoder and audio-only playback.
Why 13.1 worked and 14.0 didn't: it's the bundled FFmpeg. The 13.1 wheels shipped FFmpeg 7.0; 14.0 moved to 7.1, which changed how the MKV demuxer assigns DTS to those leading packets (now None), exposing the latent bug in the example.
The correct way:
forpacketininput_game.demux():
ifpacket.size==0: # was: if packet.dts is Nonecontinue
...
Activity
WyattBlue commentedon Jun 7, 2026
Thanks for the clear repro. It's a bug in the remux example, not in
add_stream_from_template.The culprit is this line:
That assumes only the trailing flush packet has no DTS. But for this file the IDR keyframe legitimately demuxes with
dts=None. MKV stores only PTS, and for the leading reordered (B-frame) packets libavformat can't reconstruct a DTS, so it leaves it asNone. Demuxing the video stream gives:So
if packet.dts is None: continuethrows away the keyframe. The muxer then can't start the first cluster on a keyframe and drops a couple more packets, so the stream begins mid-GOP — hence "Could not find ref with POC …" from the decoder and audio-only playback.Why 13.1 worked and 14.0 didn't: it's the bundled FFmpeg. The 13.1 wheels shipped FFmpeg 7.0; 14.0 moved to 7.1, which changed how the MKV demuxer assigns DTS to those leading packets (now
None), exposing the latent bug in the example.The correct way:
Fix remux examples dropping keyframes with no DTS (#1917)
Fix remux examples dropping keyframes with no DTS (#1917)
Fix remux examples dropping keyframes with no DTS (#1917)