Skip to content

Remux example breaks with 14.0 release #1917

Closed

Description

@daveisfera

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:

import av
import sys

av.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)

for packet in input_game.demux():
    print(packet)

    # We need to skip the "flushing" packets that `demux` generates.
    if packet.dts is None:
        continue

    if packet.stream == in_video:
        packet.stream = out_video
    else:
        packet.stream = out_audio

    output.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.

Activity

  1. WyattBlue commented on Jun 7, 2026

    @WyattBlue
    Member

    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.
    if packet.dts is None:
        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:

    for packet in input_game.demux():
        if packet.size == 0:   # was: if packet.dts is None
            continue
        ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions