View Full Version : Remux MKV to MP4 with correct timestamps?
DragonQ
21st June 2014, 21:03
I'm trying to use a video editor that doesn't support MKV and it really struggles with TS files too (Sony Vegas Pro). It seems MP4 is the best input format for it, and it works for some files, but I'm not having any joy remuxing some 1080i/25 TV recordings (AVC/AC3 in an MKV) into working MP4s with correct timestamps.
I'm using ffmpeg to remux and have tried a few different things (straight from MKV to MP4, from MKV to TS to MP4, MKV to elementary stream to MP4) but the resulting MP4 just doesn't play properly in MPC-HC, let alone within Vegas Pro (the frames jump around out-of-order). For example, using this input line:
ffmpeg -i "D:\input.mkv" -vcodec copy -acodec copy "D:\output.mp4"
I get loads of warnings during the remuxing like this:
Non-monotonous DTS in output stream 0:0; previous: 3119681, current: 3119040; changing to 3119682. This may result in incorrect timestamps in the output file.
Is it even possible to get a working MP4 from an MKV like this? Maybe there's another remuxer I could try or there's some ffmpeg options that will help?
Thanks for any advice.
Simon88
21st June 2014, 22:01
extract the video stream & audio stream & ttext (if applicable). Also extract timecodes if you notice sync issues (non-standard frame-rates). Then remux using mp4box (latest GPAC version).
DragonQ
21st June 2014, 22:07
How do I extract timecodes? Also, I've tried MP4Box (via YAMB), it also produces a dodgy MP4 file like ffmpeg does.
EDIT: OK I see I can use timecodes_v2 (don't see this option in MKV Extract GUI 2). Are there any GUIs that will let me make an MP4 using the timecode files?
EDIT: Every time I download a Windows binary of MP4Box GPAC I get the error "installer integrity check has failed". :(
Simon88
21st June 2014, 22:54
I only use the official, latest GPAC mp4box.exe extracted from the installer. They do have a much older stable release. I don't like using GUIs, the command line has so much more options... Maybe gMKVExtractGUI will work... don't know, haven't tried it before...
An easier method may just be using LATEST!!!!! ffmpeg... it is updated daily...:D and mux it into a MP4 then using the GPAC mp4box and remux it into a "better/fixed" MP4...
The method I presented earlier is rather complex, even I get so confused that I use a custom batch script....
DragonQ
21st June 2014, 22:56
OK well I can't find any information about how to use mp4box to mux using timecodes. Any help?
Simply remuxing the MKV into an MP4 using ffmpeg and then remuxing that MP4 into another MP4 using mp4box doesn't help. Looks like I need the timecodes...
Simon88
21st June 2014, 23:08
latest ffmpeg, when convert from mkv to mp4 is suppose to preserve the timecodes... is the mkv file container broken or too old? you may need to "upgrade" the mkv container on your video file to the latest version, then convert using latest ffmpeg... it's trial and error sometimes, when dealing with uncooperative files
DragonQ
21st June 2014, 23:09
I have the latest ffmpeg (updated yesterday before trying this stuff) and latest MKV Merge. I only made the MKV a few days ago.
ITV HD probably has a dodgy stream though. I know that MediaPortal reports continuity errors constantly when watching ITV HD (not for other channels) even though the video plays fine (50 fps, no dropped frames).
Simon88
21st June 2014, 23:16
I see... then you need something like TS-Doctor to fix the captured transport stream file first, before processing with ffmpeg or mkvtoolnix..
DragonQ
21st June 2014, 23:18
Done that already, I always use TS-Doctor before muxing to MKV. :)
Simon88
21st June 2014, 23:19
OK.. then use mp4fpsmod to insert the timecodes back into the MP4 file, after extraction from MKV. Then your all set... :)
Simon88
21st June 2014, 23:23
The one I listed is just free & a bit buggy, you may wish to search the net for a more easier to use & a bit more polished timecode insertion utility... It was the 1st one I found that was open sourced and cheap... :)
DragonQ
22nd June 2014, 00:29
OK using mp4fpsmod I can get a working MP4 with correct timestamps, woo! Unfortunately, Vegas Pro won't bloody open it, despite opening some other MP4 files with AVC fine. :(
Simon88
22nd June 2014, 01:02
perhaps Vegas Pro does not like the container codec ID used by gpac mp4box, so you just re-mux again using ffmpeg or libav and try using Vegas Pro... and remux back using mp4box if needed... :D A pain, but may work...
DragonQ
22nd June 2014, 01:09
If I remove the audio before muxing (don't need it anyway), Vegas Pro opens the file. However, in the preview window the timestamps are all messed up again, even though the MP4 plays fine in MPC-HC. It also says it's 125 fps despite clearly playing at 50 fps in the preview window.
Sigh.
Simon88
22nd June 2014, 01:34
Use "Fix bitstream timing information", I believe Mosu added that option not too long ago...
huokok
22nd June 2014, 02:45
Barely know anything about Sony Vegas, does it accept Avisynth as input?
If you can use mpc-hc to palyback accurately, then there should be a suitable source to open the mkv you made and serve to Sony Vegas.:)
Hi all.. I solved this issue with Sony Vegas Pro, using AVFS and Pismo File Mount..
Ghitulescu
22nd June 2014, 09:04
I still do not understand the reason of converting MKV to MP4 ...
Vegas Pro accepts individual streams (assets), so a demux should be he only thing you need. Then place them on the timeline (slugs) as needed.
I know no device that records in MKV, but if there is an issue with the MKV, then the problem has to be searched on that device. Again TC/IVTC and NDF/DF are issues not present in raw/captured videos, only in edited ones.
DragonQ
22nd June 2014, 09:51
Use "Fix bitstream timing information", I believe Mosu added that option not too long ago...
I'm confused. How will that help me? I'd still need to demux the "fixed" MKV.
Hi all.. I solved this issue with Sony Vegas Pro, using AVFS and Pismo File Mount..
I guess I'll look into this, thanks.
I still do not understand the reason of converting MKV to MP4 ...
Vegas Pro accepts individual streams (assets), so a demux should be he only thing you need. Then place them on the timeline (slugs) as needed.
The timestamps are screwed up when I simply import the H264 stream without a container, just like when using the MP4 file.
Simon88
22nd June 2014, 14:53
I'm confused. How will that help me? I'd still need to demux the "fixed" MKV.
The problem is you may handling variable bitrate files and if you take a look at the video w/ MediaInfo you will see alot of strange values... However those values will be carried over when you re-mux to MP4.... "Fix bitstream timing information" adjusts such info to match the container...
DragonQ
22nd June 2014, 14:57
Well I did try that and it didn't help. The resulting MP4 still has incorrect timestamps in Vegas Pro. I guess I need to try to avisynth thing, which sucks cos I know nothing about avisynth.
Simon88
22nd June 2014, 19:07
AviSynth is something I often relied on about a decade ago... it acts as a powerful script-based frameserver. Not sure if you wish to re-process your videos...
..I guess I need to try to avisynth thing, which sucks cos I know nothing about avisynth.
In Your case with MP4,do avs script, like that:
DirectShowSource("../path to/your.mp4", audio=false)
..and then, mount this script.avs with Pismo File Mount..
When You mount it, in script folder will be avi file, which must to be accepted by Sony Vegas Pro..
Nothing difficult..
Ghitulescu
23rd June 2014, 08:15
I am confused now.
Everyone speaks now about what is used to be called timecode. AFAIK timestamps are timecodes overposed on video, either hard (like in surveillance cameras or old VHS camcorders) or soft (like eg in DV or some AVCHD camcorders - mostly as subcodes or subtitles).
Either way it should be no problem with Sony Vegas, unless there is something that escapes my current understanding of OP original message (like variable frame rate and stuff). AFAIK Sony Vegas adds TC based on fps to any video file it loads, so does it for audio. Scripts to create continuous timestamps (like those found in surveillance cameras) exist, too.
PS: in case this might help: Vegas is designed to use raw footage, that is footage that is not telecined (2:3) or otherwise tempered. If one has to process eg NTSC DVDs he should IVTC them, before passing them to Vegas.
DragonQ
23rd June 2014, 08:41
No, no, I don't want anything overlaid on the video. The fact is that the video does not play smoothly without the timecodes and it seems that Vegas Pro doesn't recognise those timecodes even when they are embedded into the MP4 file. This means that the frames don't play in the correct order.
As I said in the OP, the files are 1080i/25. I expect Vegas Pro to handle interlaced files.
Ghitulescu
23rd June 2014, 09:05
There is no such thing as 1080i/25 (maybe 50 :) ).
The obvious solution is to recreate the timecodes eg by remuxing them, eg with tsmuxer/tsremux.
The DTS should then be continuous (so the PTS), IIRC at GOP level.
PS: there might be another cause - a bad reception.
PS2: AFAIK vegas has to be instructed to load timecodes.
DragonQ
23rd June 2014, 18:42
There is no such thing as 1080i/25 (maybe 50 :) ).
Sure there is, in EBU notation (notice the slash). (http://en.wikipedia.org/wiki/1080i)
The obvious solution is to recreate the timecodes eg by remuxing them, eg with tsmuxer/tsremux.
The DTS should then be continuous (so the PTS), IIRC at GOP level.
But that doesn't work. I can create a TS file using TSMuxer but Vegas Pro doesn't like it (it just freezes when I click the file in the "Import Media" dialog).
PS: there might be another cause - a bad reception.
No, the MKV plays fine.
PS2: AFAIK vegas has to be instructed to load timecodes.
Hmm, how? Can't see any options for this.
Ghitulescu
23rd June 2014, 19:28
I don't use Vegas for a long time, it's too complex for my simple jobs, but there should have been an option something like Lay Out Tracks Using Media Timecode or timestamps or similar.
And IIRC there is also a possibility to preview them in preferences or options or settings, you'll find it for sure.
However, try to demux the TS back to elementar streams, that was my advice. Keep the last H264 and the original audio (and of course the original MKV file).
Ghitulescu
23rd June 2014, 19:31
PS: there might be another cause - a bad reception.
No, the MKV plays fine.
Don't confuse the things - even a bad stream can be played flawless if the error correction can correct the errors.
However, editing DOES NOT imply playback, just using it, and consequently the error correction algorithms are not applied.
DragonQ
23rd June 2014, 23:07
I don't use Vegas for a long time, it's too complex for my simple jobs, but there should have been an option something like Lay Out Tracks Using Media Timecode or timestamps or similar.
And IIRC there is also a possibility to preview them in preferences or options or settings, you'll find it for sure.
Hmm OK I'll have a look.
However, try to demux the TS back to elementar streams, that was my advice. Keep the last H264 and the original audio (and of course the original MKV file).
That doesn't help because Vegas Pro doesn't read elementary streams. At least it doesn't recognise a .h264 file anyway.
Don't confuse the things - even a bad stream can be played flawless if the error correction can correct the errors.
However, editing DOES NOT imply playback, just using it, and consequently the error correction algorithms are not applied.
Well TS-Doctor would've flagged up any errors. There are usually a couple of warnings but the way the file plays in Vegas Pro would suggest errors once a second at least, which is certainly not the case.
DragonQ
23rd June 2014, 23:30
In Your case with MP4,do avs script, like that:
DirectShowSource("../path to/your.mp4", audio=false)
..and then, mount this script.avs with Pismo File Mount..
When You mount it, in script folder will be avi file, which must to be accepted by Sony Vegas Pro..
Nothing difficult..
Just tried this. Seems to work fine and Vegas Pro accepts the file (eventually) and it plays back correctly...except it's playing back twice as fast as it should. I've tried changing the field order from "progressive scan" to "upper field first" but it didn't help. Do I need to change the avisynth script perhaps?
EDIT: Aaargh, although the imported AVI appears to play fine, when I trim it into sections, put them on the timeline and render, it comes out with all the frames in the wrong order again. This is ridiculous. It's as if as soon as I make an edit, that part of the video becomes corrupted whenever I view it in the previous window and the rest of the original AVI I've imported is fine.
Can you give a sample your file.mp4?
Ghitulescu
24th June 2014, 07:49
That doesn't help because Vegas Pro doesn't read elementary streams. At least it doesn't recognise a .h264 file anyway.
Well TS-Doctor would've flagged up any errors. There are usually a couple of warnings but the way the file plays in Vegas Pro would suggest errors once a second at least, which is certainly not the case.
Strange that it doesn't work with .TS, then try with .M2TS streams - I am positive that I fed it with such files in the past and it worked.
DragonQ
24th June 2014, 23:16
Can you give a sample your file.mp4?
Sure here's a trimmed MP4 with correct timecodes (http://www.aotplaza.com/Files/HTPC/Samples/MP4%20Timecodes%20Sample.mp4). If I play it in MPC-HC (with MadVR & LAV Filters) it plays fine. If I import into Vegas Pro and play the preview window, the frames jump all over the place.
As a side-note, I can't get avisynth to create a virtual file that works with interlacing, rather than being 25 fps weaved for example.
Strange that it doesn't work with .TS, then try with .M2TS streams - I am positive that I fed it with such files in the past and it worked.
It does support M2TS (soemwhat) but if I mux the MKV file to TS, then rename to M2TS, Vegas Pro just says it can't open it.
foxyshadis
25th June 2014, 09:45
Try DirectShowSource(.... ,convertfps=true,fps=25)
The convertfps option is required for handling dropped frames and other variable frame rates. Plus you should specify the framerate; whatever decoder you have is probably returning fields per second instead of frames.
..If I import into Vegas Pro and play the preview window, the frames jump all over the place..
Do the avs script, like that:
DirectShowSource("../path to/your.mp4", audio=false, fps=25, convertfps=true)
Must work..
Or, use DGIndexNV, and script like:
DGSource("../path to/your.dgi", deinterlace=1, use_top_field=True)
foxyshadis ahead of me.. :)
DragonQ
25th June 2014, 11:40
Do the avs script, like that:
DirectShowSource("../path to/your.mp4", audio=false, fps=25, convertfps=true)
Must work..
Nope, that's worse. Makes it play at what looks like 12 fps in MPC-HC, with weave deinterlacing leaving combing artefacts everywhere. You can try it with the sample I provided to see the effect.
Nope, that's worse. Makes it play at what looks like 12 fps in MPC-HC, with weave deinterlacing leaving combing artefacts everywhere..
Which source You use with MPC-HC?
DragonQ
25th June 2014, 11:53
File Source Async, LAV Splitter, LAV Video Decoder, MadVR.
No, I mean, what kind of file you are playing in MPC-HC and get 12 fps?..
DragonQ
25th June 2014, 12:02
As I said above, that happens when using this avisynth script on the sample MP4:
DirectShowSource("../path to/your.mp4", audio=false, fps=25, convertfps=true)
And how Vegas Pro works with this script?
DragonQ
25th June 2014, 12:22
Looks about the same. Frame stepping sometimes changes the image, sometimes it doesn't. No discernible pattern.
Ghitulescu
25th June 2014, 13:12
It does support M2TS (soemwhat) but if I mux the MKV file to TS, then rename to M2TS, Vegas Pro just says it can't open it.
Why on hell would someone do such a thing?
Renaming a BMP file into AVI won't make a movie out of a photo.
I've done demux of 'MP4 Timecodes Sample.mp4' with tsMuxeR 2.6.12 and this (MP4 Timecodes Sample.track_1.264) works smooth in Vegas and MPC-HC..
DragonQ
25th June 2014, 14:11
@DragonQ Try this:
DirectShowSource("..path\to\MP4 Timecodes Sample.mp4", audio=false, fps=50, convertfps=true)
Looks fine..
Nope, still looks lower than 25 fps and with combing everywhere.
DragonQ
25th June 2014, 14:46
Why on hell would someone do such a thing?
Renaming a BMP file into AVI won't make a movie out of a photo.
Doesn't matter but muxing directly to M2TS doesn't work either. Vegas Pro just freezes when I click on it.
Simon88
25th June 2014, 16:29
If your important video can be played by ffplay, then you can try a decode to a loss-less audo/video codec using ffmpeg, then perform a re-encode to a de-interlaced h.264/AAC mp4.
Another way is to extract the video and and audio into their individual RAW files and re-mux them together and fix synchronization manually. If it is variable frame-rate then it'll most likely be not worth the trouble...:(
Ghitulescu
25th June 2014, 16:32
Well TS-Doctor would've flagged up any errors. There are usually a couple of warnings but the way the file plays in Vegas Pro would suggest errors once a second at least, which is certainly not the case.
And how did you get the MKV out of it? AFAIK, tsDoctor does not accept MKV, only TS, M2TS and other MPEG-2 formatted files (like those of satellite-receivers). I believe in latter versions it can also output MKV, but why haven't you tried with the original file?
DragonQ
25th June 2014, 16:56
And how did you get the MKV out of it?
My workflow is:
- Record HD channel using MediaPortal to TS format (AVC / AC3)
- Cut & fix using TS-Doctor
- If there are errors, copy TS to archive. If no errors, mux to MKV using MKV Merge and copy MKV to archive.
I obviously delete the TS file once the MKV is made but I don't see why that's relevant: I can remux the MKV to TS and it plays fine. It's only the fact that Vegas Pro is stupidly picky about its input formats that's causing these issues. It doesn't accept MKV or TS, it supposedly accepts M2TS but it just freezes, it accepts MP4 but it seems to ignore the timecodes in the MP4 file so that's no good. It accepts raw AVI (from avisynth mount) but as soon as any cuts are made it gets confused and frame order gets screwed up again.
All I want to do is edit some videos together without re-encoding (until rendering of course). :(
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.