Log in

View Full Version : Split MKV on non-keyframe, discard the last part


Sakura-chan
13th May 2013, 06:08
Hi!

I have a new challenge (?) in the MKV world. I have an old TV recording (MKV, h264 and AAC) that has ads on the beginning and the end and I want to cut them.

I understand that mkvmerge can split, but only on keyframes, as new h264 streams must start with one of those. The first part is fine, there's a sudden scene change and the programme starts on a keyframe, perfect for split.

But at the end there's no keyframe when ads start. The first one is way ahead, so if I split I get several seconds of ads as mkvmerge accommodates to the nearest one.

My problem here is that I don't want the last part. I shouldn't care about starting it with a keyframe if I'm going to discard it anyway.

Is there some way to split an MKV on an exact frame, without bothering if the following part will start or not on a keyframe? (or better, muxing only the segment in the middle).

I tired searching but I always end up with mkvmerge splitting and/or the "parts must start on keyframe" thing :(

Nozdrum
28th May 2013, 00:41
I've the same problem, trying to remove an ad from a video but with MKV Merge GUI it always cuts several seconds off the specified frame, is there any way to "force" mkv merge to split mkv files using the specified number of frames?

I know that I should encode the video using Trim() but I don't know how to split the audio after encoding the video, using MeGUI for some reason it doesn't work, it totally ignores the clt cuts file and I can't find the reason why, also I'd like to split the subtitles together with the whole file without using a subtitle editor and if possible to avoid re-encoding the video because the quality is already not so good, a new conversion would make it horrible.

Is it seriously impossible to make something similar to MKV Merge split function that would work based on the number of frames only, and not the key frame? I think anyone using MKV Merge already know which frame they want to cut, although I've no idea why there's not any program with such a function, so I hope anyone of you could tell me some technical detail and maybe a solution to this problem.

Thanks in advance!

Warperus
28th May 2013, 09:41
If any frame refers to anything beyond split point your tool should reencode such frames. I've heard VideoRedo can do something like this, but it's not free.
With a some manual work you can do it youself. Extract h264 stream, split it on keyframe and measure the part you need afterwards. Encode this part with parameters similar to main stream. Then merge two streams and mux into new mkv file adding properly splitted audio streams.

paradoxical
28th May 2013, 15:30
Is there some way to split an MKV on an exact frame, without bothering if the following part will start or not on a keyframe? (or better, muxing only the segment in the middle).

Your issue has nothing to do with the container. Your issue is that you can't cut an H.264 stream, or really any mpeg video stream, at arbitrary positions. You must cut at a keyframe or else to do so the tool you use to cut the stream must re-encode the GOP surrounding the cut point. Obviously mkvmerge does not offer that latter functionality since it does not come with an encoder. As Warperus mentioned there are tools do so but anything with such a smart cut functionality are almost never going to be free. VideoRedo is a great tool to do what you want but unless you're going to be doing this a lot of times you might not want to pay the ~$100.

JimmyBarnes
10th July 2013, 02:12
Your issue has nothing to do with the container. Your issue is that you can't cut an H.264 stream, or really any mpeg video stream, at arbitrary positions.

Have to disagree with you there.

If I encode x264 in an AVI, I am able to truncate it at exact frame positions i.e. I can end a cut on a particular frame. I do this routinely with the free VirtualDubMod 1.5.10.2 which works well with AVIs (but not modern MKVs).

Of course if you cut a stream and want the segment AFTER the cut, that must begin on a keyframe, even in an AVI. This seems to be the problem with MKVmerge GUI: Suppose you want to split an MKV into 2 parts, it knows the trailing part must begin on a keyframe, but unfortunately it enforces that condition on the first part as well i.e. it ends the 1st segment just before the keyframe. VDubMod isn't a splitter as such, just a fantastic editor for AVIs, the equivalent of which does not exist for MKVs unfortunately.

sneaker_ger
10th July 2013, 05:44
This seems to be the problem with MKVmerge GUI: Suppose you want to split an MKV into 2 parts, it knows the trailing part must begin on a keyframe, but unfortunately it enforces that condition on the first part as well i.e. it ends the 1st segment just before the keyframe.

It's not that easy because you also have to take b-frames into account - they reference future frames as well as past frames. Anyhow, mkvmerge simply cannot do it, as mentioned in the other thread.

raffriff42
10th July 2013, 12:40
I can end a cut on a particular frame. I do this routinely with the free VirtualDubMod 1.5.10.2 which works well with AVIs (but not modern MKVs).Read the topics "Processing: Direct Mode" and "Processing: Smart rendering" in VirtualDub (not *Mod) help.
You will have to download VirtualDub to get the file if it isn't included in your VirtualDubMod installation. It explains how VirtualDub is able to do this - basically, by re-compressing.

Here's a very old version of the Help file ('http://www.estadistica.cl/public_disk/Programas/DeTodo/Manejo%20y%20pegado%20de%20subtitulos/VirtualDub-1.6.14/help/p-direct.html') (no smart rendering yet):
Limitations on editing compressed video streams
...a portion of video to be removed must end on a keyframe.

JimmyBarnes
10th July 2013, 13:00
Read the topics "Processing: Direct Mode" and "Processing: Smart rendering" in VirtualDub (not *Mod) help.
You will have to download VirtualDub to get the file if it isn't included in your VirtualDubMod installation. It explains how VirtualDub is able to do this - basically, by re-compressing.

Here's a very old version of the Help file ('http://www.estadistica.cl/public_disk/Programas/DeTodo/Manejo%20y%20pegado%20de%20subtitulos/VirtualDub-1.6.14/help/p-direct.html') (no smart rendering yet):

That's interesting. It seems to say that VDub cuts like MKVmerge GUI, automatically adjusting a cut to a keyframe if required.

All I can say is, that when I use VDubMod (not VDub) in Direct Stream Copy mode to remove unwanted end segments, the result is as expected, a clean removal as wanted and not ending in the middle of some sequence as MKVmerge GUI often does.

If you get MKVmerge GUI to remove just the segment you wanted, you consider yourself lucky, as almost always it does not. VDubMod OTOH cuts where required in hundreds if not thousands of instances, on a routine basis. I have never noticed the sort of anomaly with VDubMod that almost always occurs with MKVmerge GUI.

prijatelj.v
6th August 2013, 12:43
As for cutting video files, somewhat "trusted" programs use a technique (function) "Direct Stream Copy" - "Direct Stream Clone Mode" (audio + video). This means that the film is cut (remove) the desired parts (personnel ) and the rest of the movie will literally be copied and converted without any loss of quality.
The most accurate programs for this "cutting" have (use) the "Smart Rendering": when you mark the beginning of the end-where the video will be severed using the function "Smart Rendering"...using the same codec,the program will effectively convert only selected start-end-the rest will be "Direct Stream Copy" (copied).Shortened the output movie,the beginning and the end will be cut off max.precisely, exactly where marked-(Virtual Dub, etc.)...
____________________________________________________________________________________________________

Now, we're talking program that most accurately cut parts of the film but what about the compatibility of such a shortened film ?
With such a "shortened" the film certainly can occur problem.This is mainly with the computer and not notice but such "sectioned" movies knows that losing a-v synchronization,the compatibility of the player, flash,external hard - TV or home DVD.

Even with the computer,some programs or players just after they have a problem to "Indexing" or "creep" and reproduce such film.This problem is mainly related to the audio-video stream ... etc..

Whenever loseless cutting must be done mux those streams that cut into the new conatiner.Mux is not at all naive surgery.Format container such as MKV and MP4 accident and were very often happens that after mux in this containere not all as expected. Muxer and cutting programs for these formats must be able to handle all formats and streams that go into the containere.Whatever they do not correspond to the unusual or strange not give the desired result ...
In theory,none of container formats (except for streaming video) is not intended to loseless cutting.Everybody loseless these tools for cutting, in a way that is unofficial.No guarantee of quality, whether to keep the synchronization of sound,losing compatibility with players,etc.
________________________________________________________________________________________________

Respect and greetings to fellow "raffriff42".I'm familiar with your posts on the forum for Virtual Dub "External Encoders".Maybe you make a more accessible-understandable tutorial with pictures that would make it accessible (understandable) and beginners.

Here, for example ...
even "powerful" Virtual Dub is sometimes a problem with cutting the AVI movie (Direct Stream Copy) and later processing in MeGUI-(MeGUI-File Indexer - Indexing error).One solution to such a sectioned film before crawling "AVI-Mux GUI" pull through...regards...

Selur
6th August 2013, 16:37
btw. MKV Cutter (http://forum.selur.de/topic220-mkv-cutter-tool-to-cut-h264-mkv-files-frame-accurate.html) might also be worth a look if it's about cutting mkv files

Overdrive80
6th August 2013, 22:30
Hi!

I have a new challenge (?) in the MKV world. I have an old TV recording (MKV, h264 and AAC) that has ads on the beginning and the end and I want to cut them.

I understand that mkvmerge can split, but only on keyframes, as new h264 streams must start with one of those. The first part is fine, there's a sudden scene change and the programme starts on a keyframe, perfect for split.

But at the end there's no keyframe when ads start. The first one is way ahead, so if I split I get several seconds of ads as mkvmerge accommodates to the nearest one.

My problem here is that I don't want the last part. I shouldn't care about starting it with a keyframe if I'm going to discard it anyway.

Is there some way to split an MKV on an exact frame, without bothering if the following part will start or not on a keyframe? (or better, muxing only the segment in the middle).

I tired searching but I always end up with mkvmerge splitting and/or the "parts must start on keyframe" thing :(

Videoredo can help you, realize split reencoding underlying areas to indicated frame.

JimmyBarnes
7th August 2013, 00:39
Now, we're talking program that most accurately cut parts of the film but what about the compatibility of such a shortened film ?

I eyeball all my edits carefully and have never detected a problem using VDubMod in this way

JB

JimmyBarnes
7th August 2013, 00:42
btw. MKV Cutter (http://forum.selur.de/topic220-mkv-cutter-tool-to-cut-h264-mkv-files-frame-accurate.html) might also be worth a look if it's about cutting mkv files

MKV will cut MKVs on non-keyframes but it obviously does so by recoding, not "direct stream copy" like VDubMod does. The cut fragment size is considerably bigger than the source.

It's also messy, producing a plethora of files rather than just the 2 you would desire from a simple split..

bxyhxyh
7th August 2013, 05:08
...split it on keyframe and measure the part you need afterwards. Encode this part with parameters similar to main stream. Then merge two streams and mux into new mkv file adding properly splitted audio streams.

I think there is no other free way.
I always do this way.
OLD x264 encode won't merged with recent ones. So i download old x264 which is same as my old video's mediainfo. Then i encode necessary part.

But manual is manual, it needs some time.

prijatelj.v
7th August 2013, 12:00
I eyeball all my edits carefully and have never detected a problem using VDubMod in this way

Problem with cutting the film is very broad term :
we can to string this topic ad infinitum - for this compatibility does not exist single solution and depends a case-by-case ...
I (purely for example) listed (perhaps insignificant) error which is thrown when indexing MeGUI film that has been previously cut into Virtual Dub :

http://files.myopera.com/prijateljv/albums/14609892/1a.png

http://files.myopera.com/prijateljv/albums/14609892/2a.png

Of course,in addition to AVI-Mux GUI,for this there are still plenty of other solutions.

Error - Compatibility is not the same ...
Compatibility in my post is mentioned (in general) for some other applications (video cutters) not Virtual Dub.
I totally agree with you - for any processing AVI movies,there is simply no better solution from Virtual Dub.
But,do not go out of this topic,which is related to MKV,and I have my opinion and fact checked (which include various examinations,tests,inspections etc.) already stated in the first (my above) post...regards...

Selur
8th August 2013, 05:22
MKV will cut MKVs on non-keyframes but it obviously does so by recoding,
If you cut frame accurately on non-keyframes, reencoding of the involved gops is needed. (MKV Cutter shouldn't reencode if you only cut on key-frames)
btw. if you only cut on key frames you can use mkvmerges splitting option,..

hello_hello
8th August 2013, 08:29
Problem with cutting the film is very broad term :
we can to string this topic ad infinitum - for this compatibility does not exist single solution and depends a case-by-case ...
I (purely for example) listed (perhaps insignificant) error which is thrown when indexing MeGUI film that has been previously cut into Virtual Dub :

I think VirtualDub (or maybe it's VirtualDubMod) can sometimes inset junk data into the audio where it's cut to prevent changing the audio sync (even though it'd only be by a small amount). I think sometimes it also does the same thing instead of using an audio delay even if you're not splitting (don't quote me on any of that as I'm not 100% certain). If that's what's happening, I've never known it to cause a playback problem but it might upset ffmsindex. I think you'll find if you try running that indexing job again without the audio, it'll index without any error.
Or try remuxing the AVI as an MKV with MKVMergeGUI. That should remove any junk data from the audio and if so, MKVMergeGUI will tell you in the log file. If you then open the MKV for indexing, I don't think you'll have a problem.

I have an AVI here with junk data in the MP3 audio. ffmsindex fails with the same error if I try to index it with the audio. After remuxing as an MKV the problem goes away. In fact I can demux the MP3 audio, open it with MP3DirectCut, resave it as a new MP3 (which effectively removes the junk data) and use the new MP3 to replace the original audio in the AVI. When I do, I can open the AVI with MeGUI for indexing and it'll index without any error.

Your example above may cause ffmsindex to fail when trying to index the audio, but I doubt it'll cause any playback problems.

Error - Compatibility is not the same ...
Compatibility in my post is mentioned (in general) for some other applications (video cutters) not Virtual Dub.
I totally agree with you - for any processing AVI movies,there is simply no better solution from Virtual Dub.
But,do not go out of this topic,which is related to MKV,and I have my opinion and fact checked (which include various examinations,tests,inspections etc.) already stated in the first (my above) post...regards...

How have you confirmed splitting an MKV effects compatibility and how does it change the audio/video if you split on keyframes? I've split MKVs quite a few times with MKVMergeGUI and rejoined them later etc and it's not caused a playback problem I'm aware of (well there was a bug I discovered relating to splitting and joining MKVs containing DTS audio with MKVMergeGUI, but that was fixed many versions ago).

prijatelj.v
8th August 2013, 15:31
greeting to a colleague hello_hello...
AVI movie that is completely processed in Virtual Dub (without cutting) has no problem with indexing in MeGUI.Reproduction is generally good and with such cut AVI movie...otherwise,you're absolutely right : if you turn off the audio indexing in cut Movie work great.Also,your assertion is true for AVI to MKV remux (MKVMergeGUI),where after changes in the MKV container,no more problems with indexing even such a cut AVI movies,but...

I used to really like "untouched" AVI movie directly insert and indexed in such a way MeGUI.So get two (audio + video) AVS scripts :

http://files.myopera.com/prijateljv/albums/14609892/avs%201.png

_______________________________________________

LoadPlugin("D:\Programi\MeGUI 2375\tools\ffms\ffms2.dll")
FFVideoSource("D:\Sample\sample cut-mux.avi", threads=1)
#deinterlace
#crop
#resize
#denoise
___________________________________________________

LoadPlugin("D:\Programi\MeGUI 2375\tools\ffms\ffms2.dll")
FFAudioSource("D:\Sample\sample cut-mux.avi", track=1, cachefile="D:\Sample\sample cut-mux.avi.ffindex")
# detected channels: 2 channels
# detected channel positions:
_________________________________________________

Before indexing film crawl through AVI-Mux GUI. Maybe there is a better - quality way...I'm so used to - (a habit is a miracle).
To sum up :
My presentation for the Virtual Dub and cutting AVI movie focuses on an error that displays when indexing MeGUI - not compatibility or playback cut AVI movies.
____________________________________________________

For cutting MKV movie ...
the problem that I had with the mention MKVMergeGUI: cutting,converting MKV and after joining (Add or Append).Even here was plain AAC audio.Now,as you say,the problem is (hopefully) corrected.
I thought the client to do max.precision and maybe use a function (Smart Rendering).I agree with you that MKVMergeGUI (maybe the best) final tool for any MKV movie...well then,perhaps for the cutting.If we agree that a good compatibility and playback -
is there a solution for max.precision cutting this MKV movie ?

hello_hello
8th August 2013, 18:25
prijatelj.v,
Yeah.... I think the problem is ffmsindex's inability to handle any junk data in audio files, although I'm sure at one stage I was using an older ffmsindex version which happily indexed the AVI I've been using for testing while the current version won't. I'd have to experiment with different versions of ffmsindex to confirm that for sure.

I think the reason MeGUI creates two scripts when indexing AVIs is it can't extract the audio from them, so when re-encoding AVIs it has to use AVIsynth to re-encode the audio. If you don't want to re-encode it but would rather keep the original audio instead, remuxing the AVI as an MKV and then opening the MKV for re-encoding should do the trick as MeGUI can extract the audio from MKVs.

I'm accustomed to VirtualDub and kind of know what to expect, but when it comes to splitting files and rejoining them, probably the best rule of thumb is to use the same tool for rejoining as you used for splitting as different tools sometimes do things differently.
For example I've not used AVI-Mux GUI very much but when I split an AVI with it, it didn't add junk data to the audio of the second half as VirtualDub did (16ms), but I'm not quite sure what it did. I could rejoin the two halves with AVI-Mux GUI after it split the AVI, but when I tried to join those same halves with VirtualDub I got this error:

Cannot append segments. The audio streams do not share a common sampling rate.
Stream 1 = 20000 samples per second
Stream 2 = 19999 samples per second.

Is that was AVI-Mux GUI does? Instead of using an audio delay or adding junk data as a pseudo delay, does it fudge the audio sample rate to try to compensate?

Anyway, for MKV cutting I've pretty much only ever used MKVMergeGUI. If it happens to be able to cut exactly where I want it to that's great, if not..... I just live with it.
You might want to try Video To Video Converter (http://www.videotovideo.org/). It has a file splitter and commercial removal tool (both under the Tools menu). They let you split/edit using a preview. It'll also remux from one container to another etc as well as doing all the usual conversion stuff.
I haven't played with Video To Video Converter much, but I'm sure (by default at least) it only cuts on keyframes like MKVMergeGUI (but at least it gives you a preview).
However it also does this wired thing I've not seen before, where if you tell it to split on a non-keyframe it splits at the previous/next keyframe but then repeats it until it gets to the split point, so basically the audio gets split exactly where specified, but you get video which effectively "pauses" briefly at the beginning or end where it's split. I'm probably not explaining it well.... I may even not understand the way it works correctly.... I've only played with it briefly.

I'm pretty sure you can also switch to AVISynth input when cutting or splitting which may mean it can do "smart cutting", but I've not tried it myself.

Anyway, Video To Video Converter may be worth a look. If you do and decide it does something clever, or you understand the way it cuts/edits better than I do at the moment, please let me know. At worst it should let you edit/split MKVs with a preview. Pretty much like splitting/editing AVIs with VirtualDub and using DirectStreamCopy, I guess.

hello_hello
8th August 2013, 18:39
PS. If you're editing AVIs/MKVs in order to re-encode the edited version, a better way would probably be to create the usual script for re-encoding the unedited version and save it to your hard drive. Then open the script with the AVS cutter under the Tools menu, add your edits to the script, save the "cuts file" for re-encoding the audio, and then re-encode it. That way you're effectively editing as you re-encode.

Maybe I'm missing out, but I've never messed around with "smart cutting". I either just do keyframe cutting, or if there's a lot of editing, I re-encode the whole thing using the above method.

Just a suggestion in case you weren't aware of the AVS cutter. There's also an audio cutter so you can cut the audio to match without re-encoding it.

sneaker_ger
8th August 2013, 20:24
Is that was AVI-Mux GUI does? Instead of using an audio delay or adding junk data as a pseudo delay, does it fudge the audio sample rate to try to compensate?

AVI Mux GUI uses silent audio packets instead of junk data.

hello_hello
8th August 2013, 22:35
Maybe it's some weird AVI-Mux GUI thing when it comes to MP3 audio. I tried using it to split a couple more AVIs and each time VirtualDub refused to join the two parts together again. The last one I tried had VBR MP3 and instead of complaining about the sample rates, VirtualDub complained about the audio data rates being different. VirtualDubMod said the audio streams had different data formats. It only seems to happen with MP3 audio. I split an AVI using AVI-Mux GUI which contained AC3 and VirtualDub(Mod) was happy enough to join the two parts together again.