Page 1 of 2

mkvmerge --split returns unexpected results

Posted: Sat Mar 23, 2024 4:48 pm
by RAHRL
I have a movie (Penelope (1966)) that consists of two .mp4 files, i.e. part 1 and part 2. I already know how to combine the two parts into one file, but unfortunately part1.mp4 ends with a 6 second banner that I want to discard before stitching the two parts together.

I have tried to use mkvmerge --split, but whatever I try (using duration or timestamps), the file is not split at the expected point.

For example, according to the Windows Explorer:
part1.mp4 duration = 00:49:09

and after:
mkvmerge -o output.mkv --split timestamps:00:49:03 part1.mkv

I get:
output-001 duration = 00:49:06
output-002 duration = 00:00:03


There is also a message:
Timestamp used in split decision: 00:49:05.992000000

I couldn't try using frame numbers, because I do not know how to display frame number and go through them one by one.

Re: mkvmerge --split returns unexpected results

Posted: Sat Mar 23, 2024 11:43 pm
by Sunoo
It will split at the nearest keyframe, which can potentially be far away depending on the video.

My solution for that sort of thing is to use LosslessCut, which can be frame perfect in more situations: https://github.com/mifi/lossless-cut

Re: mkvmerge --split returns unexpected results

Posted: Sat Mar 23, 2024 11:55 pm
by Radiocomms237
I tried (and abandoned) LosslessCut because it too would only cut on keyframes, maybe I had some setting incorrect (or maybe it's been changed via an update since I tried it).

Every video editing suite I've ever used has been able to cut on an exact timecode though, and most have a lossless output function if the codec is supported.

Unfortunately video editing suites, in my experience, won't trim ALL the associated tracks (audio, subtitles, chapters) like MKVToolNix does.

Re: mkvmerge --split returns unexpected results

Posted: Sun Mar 24, 2024 12:44 am
by Sunoo
In LosslessCut, on the Export screen, turn off “Keyframe cut mode”.

Depending on what you’re doing, you may want to try turning on “Smart cut”, which reencodes only the section between the closest keyframe and the cut point. In my experience, that is usually not required when trimming off the end though.

Re: mkvmerge --split returns unexpected results

Posted: Sun Mar 24, 2024 1:16 am
by Radiocomms237
It's a shame we can't turn off the keyframe mode in MKVToolNix as well!

Re: mkvmerge --split returns unexpected results

Posted: Sun Mar 24, 2024 2:09 am
by Sunoo
The trick is that you can’t easily split without a keyframe, since you can’t start the next segment between keyframes without re-encoding at least a piece of it.

Re: mkvmerge --split returns unexpected results

Posted: Mon Mar 25, 2024 9:52 pm
by RAHRL
I played a bit with a VLC addon "Jump to time Previous frame" which lead me nowhere, but I finally managed to trim the .mp4 file with the standard Windows 10 video editor (and then it hung up my laptop, fortunately after saving the trimmed file).

It let's you determine the exact (well, in seconds) position where to trim using the mouse and better: the cursor keys.

The trimmed file is now 6 seconds shorter than the original, which is what I needed, but I still have no idea why mkvmerge wouldn't do it, with a part of the banner noticably remaining in the video.

Without much knowledge of matters I keep thinking "How can such a simple thing, like trimming a video file, be so complicated to do?"

Re: mkvmerge --split returns unexpected results

Posted: Mon Mar 25, 2024 10:11 pm
by dcoke22
RAHRL wrote:
Mon Mar 25, 2024 9:52 pm
Without much knowledge of matters I keep thinking "How can such a simple thing, like trimming a video file, be so complicated to do?"
If you want to learn a little bit: Video compression picture types

Re: mkvmerge --split returns unexpected results

Posted: Tue Mar 26, 2024 3:22 am
by RAHRL
dcoke22 wrote:
Mon Mar 25, 2024 10:11 pm
If you want to learn a little bit: Video compression picture types
That's exactly what I mean, too complicated material for me. I want to be the driver of the car, not an automotive engineer.
The tooling for even the simplest operations on video files (e.g. splitting, stitching, subtitles, etc.) is all over the place, it comes from every corner of the internet.

LosslessCut seems nice at first sight, but costs around 20 euro to download from the MS app store and there is no trial version. The source code on github is free, but do I really need to be a source code developer to trim a video file?

Re: mkvmerge --split returns unexpected results

Posted: Tue Mar 26, 2024 1:07 pm
by Sunoo
RAHRL wrote:
Tue Mar 26, 2024 3:22 am
LosslessCut seems nice at first sight, but costs around 20 euro to download from the MS app store and there is no trial version. The source code on github is free, but do I really need to be a source code developer to trim a video file?
It’s free on GitHub, and you do not need to compile it yourself… https://github.com/mifi/lossless-cut/releases/latest

Re: mkvmerge --split returns unexpected results

Posted: Tue Mar 26, 2024 4:28 pm
by RAHRL
Sunoo wrote:
Tue Mar 26, 2024 1:07 pm
It’s free on GitHub, and you do not need to compile it yourself… https://github.com/mifi/lossless-cut/releases/latest
Yep, thx, I managed to download and decompress it. No further installation required, just clicking on the .exe file suffices. Works far better than the Windows tool that I used. It also shows the absolute framenumber and it can also merge video files.

Next question, regarding the merge, how can I get a smooth transition between the two parts? At the moment, the transition is very abrupt.

Re: mkvmerge --split returns unexpected results

Posted: Tue Mar 26, 2024 11:45 pm
by Sunoo
It’s not clear to me what you’re looking for. A merge is effectively just playing two files back to back.

Re: mkvmerge --split returns unexpected results

Posted: Wed Mar 27, 2024 12:33 am
by Radiocomms237
AFAIK, the only way to add a "transition" is by using a video editing suite (pretty much ANY video editing suite).

Re: mkvmerge --split returns unexpected results

Posted: Thu Mar 28, 2024 1:36 pm
by RAHRL
Sunoo wrote:
Tue Mar 26, 2024 11:45 pm
It’s not clear to me what you’re looking for. A merge is effectively just playing two files back to back.
I'll try to explain better. The first part ends with a particular scene, the second part starts with an other scene. When I stitch them to together with MKVmerge, it just does its job and there is no smooth transition the two scenes. As an alternative, there is also no "black screen" interval between the two scenes. It just jumps from one scene to the other which is not pleasant to watch.

Ideal would be that the first scene is faded out, and the second scene is faded in. I found something promising like that on:

How To Fade to Black in Shotcut | Fade In and Fade Out Effect | Shotcut Tutorial
https://www.youtube.com/watch?v=FLPOqcylAnQ

but I haven't got around to testing it.

I wonder if LosslessCut could do it, because the amount of tools for doing this and that with video's on my laptop seems to never stop increasing.

To be honest, I cannot imagine that somebody stitching 2 parts of a movie together, wouldn't want this.

Re: mkvmerge --split returns unexpected results

Posted: Thu Mar 28, 2024 8:49 pm
by Mr. Red
Depends on the videos/scenes you stitch together. It works really well for the extended Lord of the Ring films (disc 1 and 2).

You could make a feature request on github for losslesscut. But I doubt that it will be considered since it is not what the program is intended for.
Furthermore it would require to re-render at least the changed part.
If you want to limit yourself to a single program that can cut, merge, sync, add transitions, etc. you have to use one of the full fledged video editing tools.


What you could try is to search for a black and silent part of the video and cut a chunk of it (~1s) with losslesscut. Than you can stitch it (with mkvtoolnix) between the two parts. You won't have a fancy fade-out/in effect, but at least some kind of separator between the scenes.