<p>The demuxed H264 stream has the same metadata content, however it is rearranged, compared to the directly produced .264 stream. So far, I don't see a way to rearrange it to match.</p>

<p>So, transcode directly to 264 and use mp4box to generate a stream inside a container</p>

<pre><code>mp4box -add direct.264 -new muxed.mp4
</code></pre>

<p>You can then use ffmpeg to mux to MKV.</p>

<pre><code>ffmpeg -i muxed.mp4 -c copy muxed.mkv
</code></pre>

<p>mp4box is part of the GPAC project. Get a nightly build from <a href="https://gpac.wp.imt.fr/downloads/gpac-nightly-builds/" rel="nofollow noreferrer">https://gpac.wp.imt.fr/downloads/gpac-nightly-builds/</a></p>

<hr>

<p>To do this entirely within ffmpeg, you can use the tee muxer to pipe a AVI mux to another ffmpeg instance</p>

<p><code>ffmpeg -i input.mov -map 0:v -vsync cfr -r fps -pix_fmt yuv420p -c:v libx264 -preset fast -tune film -b:v 20000k -maxrate 35000k -bufsize 30000k -level 4.1 -x264opts bluray-compat=1:open-gop=1:slices=4:keyint=24 -f tee "scenarist.264|[f=avi]pipe:1" | ffmpeg -f avi -i - -c copy -y playback.mkv</code></p>

<p>Verify the </p>

CC BY-SA 4.0
2019-02-11T22:34:56.600Z
Thanks Gyan, but unfortunately this is the backend for an application I built and I need it to be portable so GPAC does not work. However I did just figure out a ridiculous but workable solution. I cannot mux .264 directly to .mkv, however this is the only workflow I've found that does the trick: `mov -> 264 -> avi -> mkv`. For whatever reason the avi muxer seems to regenerate whatever metadata is needed for the mkv... Because they're all just stream copies it isn't too slow. Do you know of a way to go `264 -> avi -> mkv` in one process?
Spencer
CC BY-SA 4.0
2019-02-12T05:46:01.700Z
Added workaround. There are bugs with the method you've used. In H264 streams with hierarichical B-frames, ffmpeg may reorder B-frames presentation order. However, Blu-ray limits the B-frame count, so you may be OK. Test a few MKVs, though.
Gyan
CC BY-SA 4.0
2019-02-12T05:46:35.483Z
What's the barrier with GPAC? It's FOSS and cross-platform. Installation is not needed.
Gyan
CC BY-SA 4.0
2019-02-12T23:09:05.833Z
Thanks for all of your help! I couldn't find a portable version of GPAC, just an exe? Your tee mux recipe gave me a better idea, why not just transcode a .264 and .mkv at the same time? Then I don't need to mux the .264 later, that can just go straight to scenarist and I can mux the mkv with chapters and audio later. Don't know why I didn't think of it before!
Spencer
CC BY-SA 4.0
2019-02-13T04:55:10.270Z
*why not just transcode a .264 and .mkv at the same time?* --> that's double CPU + RAM.
Gyan
CC BY-SA 4.0
2019-02-13T06:24:32.173Z
Even with the tee muxer?
Spencer
CC BY-SA 4.0
2019-02-13T07:07:22.607Z
The bitstream format demanded by a container like MP4/MKV requires a global_header, which has to be implemented by the encoder. `.264` requires in-band headers, which is the direct opposite. The tee muxer is meant for when identical streams have multiple recipients.
Gyan
CC BY-SA 4.0
2019-02-13T21:15:20.897Z
I see, I'm not very familiar with headers. It did seem to work though without a problem, does that make sense? I didn't benchmark it or test it very thoroughly beyond running out a 10 second test video (which seemed to work okay, and scenarist at least imported)
Spencer
CC BY-SA 4.0
2019-02-11T08:48:51.840Z
2019-02-12T05:38:03.197Z
2019-02-12T05:38:03.197Z
Gyan
<p>I have a very specific pipeline I need to set up, it starts with an mov file from Avid which gets transcoded to mkv for internal review and .264 for burning to Blu-ray in Scenarist. It goes:</p>

<p><code>.mov (DNxHD) -&gt; .mkv (h264) -&gt; .264</code></p>

<p>For purposes of quality and speed I transcode to .mkv in FFMPEG and then when the time comes to make a Blu-ray I demux the mkv to the .264, however I'm told this is throwing a very ambiguous error in scenarist and I can't figure out what I'm doing wrong. Interestingly if I transcode directly to .264 instead of demuxing it there is no problem, so this appears to be a metadata issue. Here's the demux recipe I'm using:</p>

<p><code>-i input.mkv -codec copy -bsf:v h264_mp4toannexb output.264</code></p>

<p>Here's the transcode recipe if that is useful:</p>

<p><code>-i input -c:v libx264 -preset fast -tune film -b:v 20000k -maxrate 35000k -bufsize 30000k -level 4.1 -pix_fmt yuv420p -x264opts bluray-compat=1:open-gop=1:slices=4:keyint=24 -r fps outfile.mkv</code></p>

CC BY-SA 4.0
2019-02-07T19:11:24.627Z
Add `-bsf:v dump_extra=k` and check. Annex B bsf is auto-inserted since v3.2
Gyan
CC BY-SA 4.0
2019-02-07T19:39:57.507Z
@Gyan Just tried that and Scenarist says "Media file invalid". If I inspect the file it seems almost all metadata is now gone, and if I check it in FFPROBE I get a massive stream of PPS and SPS errors. Tried dump_extra=all and got a 0kb file...
Spencer
CC BY-SA 4.0
2019-02-07T20:18:01.340Z
What was the original Scenarist error? Workaround is to transcode to .264 and mux that into MKV.
Gyan
CC BY-SA 4.0
2019-02-07T22:03:55.513Z
@Gyan The error says "Parse error: The garbage data exists in ES. offset=0, size=773". I am also re-evaluating if we really need to go to .264. If some other format works then I believe I can change this requirement, though I did just try an m4v and it did not work. (though encoding an m4v from Adobe Media Encoder did work...)
Spencer
CC BY-SA 4.0
2019-02-08T05:12:41.943Z
Contacted Scenarist and they gave us a 46 page white paper on Blu-Ray specifications! Wasn't quite able to glean anything useful out of it yet, except that "Video streams shall be...MPEG-4 AVC video format ([ISO/IEC 14496-10](http://www.staroceans.org/e-book/ISO-14496-10.pdf)) compliant...". Don't know if anyone can decypher what that means or not...
Spencer
CC BY-SA 4.0
2019-02-08T05:26:28.377Z
The error msg is helpful. Can you generate and share a 5 second sample of both methods - direct to .264 and via mkv, using your original commands?
Gyan
CC BY-SA 4.0
2019-02-08T21:03:21.807Z
@Gyan for various reason sadly no, I cannot upload a file. However if you just take the above recipe that is exactly what I'm using - I only change the output file name from .mkv to .264 respectively. I'm on version 4.0.2 win64.
Spencer
CC BY-SA 4.0
2019-02-08T21:05:10.333Z
@Gyan One other interesting thing to note is that if I compare a file encoded `.mov->.264` vs `.mov->.mkv->.264` with FFPROBE the latter gives me some PPS and SPS errors. Otherwise everything else is exactly the same, so that appears to have something to do with the problem.
Spencer
CC BY-SA 4.0
2019-02-08T22:51:24.393Z
@Gyan I'm willing to pay some bounty to figure this one out! I'm at a dead end, not sure what else to do. If you can figure this out I'll put up a bounty tomorrow and sacrifice a few reputation.
Spencer
CC BY-SA 4.0
2019-02-09T12:14:26.543Z
I'll look at this today or tomorrow, time permitting.
Gyan
CC BY-SA 4.0
2019-02-10T21:52:32.280Z
@Gyan Thanks! Just tried encoding to 264 then muxing to mkv as you suggested and got the following error: `Cant write packet with unknown timestamp`. Adding `-fflags -genpts` as some other users suggested did not fix it...
Spencer
<ffmpeg><libx264><bluray>
FFMPEG demux .264 for burning in scenarist
Spencer
# FFMPEG demux .264 for burning in scenarist

(Asked by: username_0 on 2019-02-07)

I have a very specific pipeline I need to set up, it starts with an mov file from Avid which gets transcoded to mkv for internal review and .264 for burning to Blu-ray in Scenarist. It goes:

`.mov (DNxHD) -> .mkv (h264) -> .264`

For purposes of quality and speed I transcode to .mkv in FFMPEG and then when the time comes to make a Blu-ray I demux the mkv to the .264, however I'm told this is throwing a very ambiguous error in scenarist and I can't figure out what I'm doing wrong. Interestingly if I transcode directly to .264 instead of demuxing it there is no problem, so this appears to be a metadata issue. Here's the demux recipe I'm using:

`-i input.mkv -codec copy -bsf:v h264_mp4toannexb output.264`

Here's the transcode recipe if that is useful:

`-i input -c:v libx264 -preset fast -tune film -b:v 20000k -maxrate 35000k -bufsize 30000k -level 4.1 -pix_fmt yuv420p -x264opts bluray-compat=1:open-gop=1:slices=4:keyint=24 -r fps outfile.mkv`

**Tags:** ffmpeg, libx264, bluray

> **Comments:**
> **username_1**: Add `-bsf:v dump_extra=k` and check. Annex B bsf is auto-inserted since v3.2  
> **username_0**: @username_1 Just tried that and Scenarist says "Media file invalid". If I inspect the file it seems almost all metadata is now gone, and if I check it in FFPROBE I get a massive stream of PPS and SPS errors. Tried dump_extra=all and got a 0kb file...  
> **username_1**: What was the original Scenarist error? Workaround is to transcode to .264 and mux that into MKV.  
> **username_0**: @username_1 The error says "Parse error: The garbage data exists in ES. offset=0, size=773". I am also re-evaluating if we really need to go to .264. If some other format works then I believe I can change this requirement, though I did just try an m4v and it did not work. (though encoding an m4v from Adobe Media Encoder did work...)  
> **username_0**: Contacted Scenarist and they gave us a 46 page white paper on Blu-Ray specifications! Wasn't quite able to glean anything useful out of it yet, except that "Video streams shall be...MPEG-4 AVC video format ([ISO/IEC 14496-10](http://www.staroceans.org/e-book/ISO-14496-10.pdf)) compliant...". Don't know if anyone can decypher what that means or not...  
> **username_1**: The error msg is helpful. Can you generate and share a 5 second sample of both methods - direct to .264 and via mkv, using your original commands?  
> **username_0**: @username_1 for various reason sadly no, I cannot upload a file. However if you just take the above recipe that is exactly what I'm using - I only change the output file name from .mkv to .264 respectively. I'm on version 4.0.2 win64.  
> **username_0**: @username_1 One other interesting thing to note is that if I compare a file encoded `.mov->.264` vs `.mov->.mkv->.264` with FFPROBE the latter gives me some PPS and SPS errors. Otherwise everything else is exactly the same, so that appears to have something to do with the problem.  
> **username_0**: @username_1 I'm willing to pay some bounty to figure this one out! I'm at a dead end, not sure what else to do. If you can figure this out I'll put up a bounty tomorrow and sacrifice a few reputation.  
> **username_1**: I'll look at this today or tomorrow, time permitting.  
> **username_0**: @username_1 Thanks! Just tried encoding to 264 then muxing to mkv as you suggested and got the following error: `Cant write packet with unknown timestamp`. Adding `-fflags -genpts` as some other users suggested did not fix it...  

---

## 1 answers

### Answer by username_1 on 2019-02-11. Score: 0 | Accepted answer

The demuxed H264 stream has the same metadata content, however it is rearranged, compared to the directly produced .264 stream. So far, I don't see a way to rearrange it to match.
So, transcode directly to 264 and use mp4box to generate a stream inside a container
mp4box -add direct.264 -new muxed.mp4

You can then use ffmpeg to mux to MKV.
ffmpeg -i muxed.mp4 -c copy muxed.mkv

mp4box is part of the GPAC project. Get a nightly build from https://gpac.wp.imt.fr/downloads/gpac-nightly-builds/

To do this entirely within ffmpeg, you can use the tee muxer to pipe a AVI mux to another ffmpeg instance
ffmpeg -i input.mov -map 0:v -vsync cfr -r fps -pix_fmt yuv420p -c:v libx264 -preset fast -tune film -b:v 20000k -maxrate 35000k -bufsize 30000k -level 4.1 -x264opts bluray-compat=1:open-gop=1:slices=4:keyint=24 -f tee "scenarist.264|[f=avi]pipe:1" | ffmpeg -f avi -i - -c copy -y playback.mkv
Verify the

> **Comments:**
> **username_0**: Thanks username_1, but unfortunately this is the backend for an application I built and I need it to be portable so GPAC does not work. However I did just figure out a ridiculous but workable solution. I cannot mux .264 directly to .mkv, however this is the only workflow I've found that does the trick: `mov -> 264 -> avi -> mkv`. For whatever reason the avi muxer seems to regenerate whatever metadata is needed for the mkv... Because they're all just stream copies it isn't too slow. Do you know of a way to go `264 -> avi -> mkv` in one process?  
> **username_1**: Added workaround. There are bugs with the method you've used. In H264 streams with hierarichical B-frames, ffmpeg may reorder B-frames presentation order. However, Blu-ray limits the B-frame count, so you may be OK. Test a few MKVs, though.  
> **username_1**: What's the barrier with GPAC? It's FOSS and cross-platform. Installation is not needed.  
> **username_0**: Thanks for all of your help! I couldn't find a portable version of GPAC, just an exe? Your tee mux recipe gave me a better idea, why not just transcode a .264 and .mkv at the same time? Then I don't need to mux the .264 later, that can just go straight to scenarist and I can mux the mkv with chapters and audio later. Don't know why I didn't think of it before!  
> **username_1**: *why not just transcode a .264 and .mkv at the same time?* --> that's double CPU + RAM.  
> **username_0**: Even with the tee muxer?  
> **username_1**: The bitstream format demanded by a container like MP4/MKV requires a global_header, which has to be implemented by the encoder. `.264` requires in-band headers, which is the direct opposite. The tee muxer is meant for when identical streams have multiple recipients.  
> **username_0**: I see, I'm not very familiar with headers. It did seem to work though without a problem, does that make sense? I didn't benchmark it or test it very thoroughly beyond running out a 10 second test video (which seemed to work okay, and scenarist at least imported)  

---

