Log in

View Full Version : Bug with ffmpeg copying hevc streams from mkv?


hellgauss
14th June 2026, 15:14
I do not know if it is a bug or simply I'm missing a flag. Extracting hevc stream from a .mkv repeats global headers even if they are present at each keyframe.

I'm using
ffmpeg version 2026-06-10-git-b29bdd3715-full_build-www.gyan.dev
on Windows 11

Unwanted behaviour reproduction:
rem prepare mkv source
ffmpeg -y -f lavfi -i testsrc -t 75 -pix_fmt yuv420p -c:v libx265 -preset ultrafast -f hevc test0.hevc
mkvmerge --deterministic 1 -o "test.mkv" test0.hevc

rem for reference
mkvextract test.mkv tracks 0:"test_reference.hevc"

rem the unwanted behaviour is here:
ffmpeg -y -i "test.mkv" -c:v copy "test_bug.hevc"

Notes:
- test0.hevc and test_reference.hevc are nearly identical. With a fast hex view it seems that the only difference is in the number of 0x00 at the beginning each NAL unit (two or three 0x00 bytes). They both have four extra NALs at each key frame (SPS+VPS+PPS+SEI).
- test_bug.hevc has eight extra NALs at each keyframe, since they are repeated
- Same workflow with x264 is ok.

Z2697
14th June 2026, 18:28
"problem" is how mkvtoolnix handles 'em
IIRC in 55.0 or somewhere near that the mkvmerge is changed to duplicate headers with mkvextract being able to strip them away (automatically)

Update:
So I was not RC :) The mkvtoolnix only copies the headers when they are not present and the mkvextract will not strip the headers.
But yeah FFmpeg's raw hevc muxer is a bit weird. It will automatically enable repeat-headers. Or maybe x265 is weird for defaulting to no-repeat-headers.
And there might be bitstream filter involved.
Whatever, as long as the file plays I have no interest.

hellgauss
14th June 2026, 18:51
mkvmerge only puts 8 extra units at the beginning of the file, I assume 4 of them are a "global mkv header" or something similar. Next keyframes only contain four extra NALs as expected, but when extracted by ffmpeg they always becomes 8. It is like the global headers are re-inserted at each keyframe.

Just look for "HEVC codec" string in a hex editor in the test.mkv and test_bug.hevc (It appears once in each SEI NAL)

This does not happen with x264.

Z2697
14th June 2026, 19:52
Repeat the process and I can have any number of parameter sets!
19168