View Full Version : Trying to cut the first few seconds of a MP4 video on a non-keyframe
Cyber Akuma
1st December 2018, 04:23
So, this has been driving me nuts. I have a video that I went to cut out the first 30-40 or so seconds without re-encoding the entire thing. Issue is, the part I want to cut on is not on a keyframe, so most software I used either cuts on the keyframe before the part I want (such as Avidemux) leaving in a few moments of garbage video, or gives me a video that is just bank for the first few moments (ffmpeg) until the next keyframe.
I couldn't figure out a way to make anything read the previous seconds of the video and generate a new keyframe at the beginning of the cut, so I figured my next best bet would be to actually split the video on that keyframe, then trim the part I want from the beginning by re-encoding just the half or so segment I want, and then joining that with the rest of the video.
Issue is, I have no idea how to attempt to duplicate the settings of the second half of the video so I can join it. All attempted have just resulted in it rejecting the second half of the video to join (mp4box) or spitting out a video that after that first half second or so is just corrupted and does not play the rest (avidemux).
Does anybody have any suggestions on this? I am assuming the re-encoding route is my only option, but how would I duplicate the settings of the original video so I can re-join it?
manolito
1st December 2018, 05:14
I had very good results using VidCutter for MP4 files. It has a "Smart Rendering" feature which means that it can cut frame accurate without reencoding the whole file.
http://forum.doom9.org/showthread.php?p=1855172#post1855172
https://www.videohelp.com/software/VidCutter
Good luck
manolito
Cyber Akuma
4th December 2018, 23:14
It doesn't seem to be working. Whenever I have Smart Rendering enabled, it just gets stuck at 0% when I try to trim a clip, I see no CPU or HDD usage from VidCutter.
manolito
7th December 2018, 01:49
Yes, VidCutter still does have many problems when Smart Rendering is enabled. Many users report that oftentimes it just gets stuck at a certain percentage of the process. It also happens here, I could not really fix it, but I found some things which improve it a little bit...
I am on Win7 - 64bit, so I first used the 64-bit version of VidCutter. But after switching to the 32-bit version I got the feeling that the 32-bit version is a bit more reliable.
Another thing which improved the behavior was to use a newer FFmpeg version instead of the embedded version. I also found that it is not necessary to use shared FFmpeg versions, the static versions work fine.
The source file format also plays a role. I had better success with MP4 containers than with MKV. And probably the most importat reason for the software to just get stuck is the duration of the input file.
So it looks like the author still has a lot of work to do, I hope that he will get it right soon...
Cheers
manolito
Taurus
7th December 2018, 16:15
@manolito
I've just tested your suggestions.
And yes, the only halfway reliable workflow is repacking all your stuff into a mp4 container.
With mkv I've got scrambled/corrupted frames all over the place.
Mts/m2ts is fine in the timeline, editing ok... but the program just hangs at decoding/encoding stage.
No CPU, no HDD, no nothing :mad::p
Exchanging both versions (32/64bit) to the latest ffmpeg binairies.
But same behaviour as far as I can see.
It's a shame, such a nice gui, and exactly what I needed.
Now back to search for a new tool....
manolito
8th December 2018, 02:11
... so I figured my next best bet would be to actually split the video on that keyframe, then trim the part I want from the beginning by re-encoding just the half or so segment I want, and then joining that with the rest of the video.
Issue is, I have no idea how to attempt to duplicate the settings of the second half of the video so I can join it. All attempted have just resulted in it rejecting the second half of the video to join (mp4box) or spitting out a video that after that first half second or so is just corrupted and does not play the rest (avidemux).
If VidCutter does not work in your case then I think your approach indeed is your best bet. If your reencoded segment uses the same codecs and the same frame size for video then there is a good chance that you can join these parts.
I first tried with MkvToolNix. I did get a warning about different private codec parameters, but the resulting file played fine.
The other thing I tried was FFmpeg for joining the files. dmMediaConverter is an FFmpeg GUI with some very useful options, and among other things it has an option to "Merge the same". This means that the input files need to use identical codecs and frame sizes, and I also got good results with this method.
What I did notice for both methods was a very small audio dropout at the merge point. Hardly audible, but of course this can be fixed with a WAV editor...
Cheers
manolito
kuchikirukia
16th December 2018, 03:55
What I did notice for both methods was a very small audio dropout at the merge point.
Mkvtoolnix is really good at cutting without audio drops.
So:
Mux to mkv
Split with mkvtoolnix on keyframe
Reencode the first segment with the same codec, trimming how you want.
Trim the audio.
Append.
Cyber Akuma
3rd January 2019, 01:12
If VidCutter does not work in your case then I think your approach indeed is your best bet. If your reencoded segment uses the same codecs and the same frame size for video then there is a good chance that you can join these parts.
Sorry, was not getting notifications that people were replying to this topic anymore.
I tried that, but the issue was that I was not able to figure out how to make the encode I make the same as the rest of the video, so attempting to join it would fail.
poisondeathray
3rd January 2019, 04:44
If you use a different --sps-id , you can join sections with different encoding settings or when other methods fail
Cyber Akuma
15th January 2019, 19:12
Hmm, seems VidCutter has some kind of bug or something where it fails to do a smart-render if your cut ends at or close to the end of the actual video. I tried experimenting a bit, and it would get stuck at 0% if I was trying to cut a clip that extends all the way to the end of the video even if it's not from the start of the video.
Also it seems the video it ends up spitting out is problematic. I tried it in VLC and MPC:HC, MPC plays it fine but VLC crashes....
manolito
15th January 2019, 19:47
I don't use VidCutter all that often, but I feel that its reliability improved after replacing the built-in versions of FFmpeg and FFprobe with the latest builds from Zeranoe (works with the static builds, no need to download the shared builds). And I only use it for MP4, MKV always causes problems.
Cheers
manolito
Cyber Akuma
26th January 2019, 00:02
I checked, seems like I am already using the Zeranoe versions of FFmpeg and FFprobe, and the files I am working with are MP4.
But I am still having issues where VidCutter gets stuck at 0% if the end of the segment I am clipping is near the end of the video.
I have tried adjusting every setting in the app, but it just gets stuck at 0% if I attempt to create a clip with SmartCut if the endpoint is near the end of the video.
manolito
11th August 2019, 10:40
Even though it looks like VidCutter is no longer maintained, I have the feeling that it does get better with newer FFmpeg versions... :cool:
I replaced the built-in FFmpeg and FFprobe versions with the latest stable versions 4.2 from Zeranoe (static build), and now even when using 1024p sources it does not hang any more. And this is not only true for MP4 sources, MKV sources also seem to work.
Worth another try...
Cyber Akuma
30th June 2020, 21:16
Just noticed your reply.
I tried using the latest ffmpeg, nope, still stuck at 0%
I also realized I could open the console, and saw this happen when VidCutter pretty much freezes at 0%:
https://i.imgur.com/0hvBW2b.png
First of all.... why is it using frontslashes? Windows/Dos uses backslashes... and it appears to just flat out be erroring out in it's python code and not realizing this, keeping it stuck.
manolito
1st July 2020, 15:26
I had not used VidCutter for a while, so I updated it to use the latest FFmpeg version 4.3 (static 32-bit version). I use the 32-bit version of VidCutter.
Tried it with some longer HD files. Video codec was AVC, container either MKV, MP4 or TS. The TS file did not work at all, it would hang at 0%. But both the MP4 and also the MKV files worked like a charm. So I really regret that VidCutter development seems to be dead, it does have potential...
Cyber Akuma
6th July 2020, 03:06
Seems to be utterly broken for me. Were you using SmartCut by any chance? It works for me with SmartCut off.... but the whole reason I am using it instead of other software is BECAUSE of the SmartCut feature.
I tried it on Windows. I tried the version of ffmpeg it came with, and my own.
I tried toggling every combination of settings on and off.
I then tried it in Linux. Also toggling every setting on and off.
Exact same phyton script error happened every time (the one I posted a screenshot of) when SmartCut was on. Regardless of other settings, regardless of OS.
manolito
6th July 2020, 15:31
Really weird... :confused:
Of course I use SMART cutting mode. Could it be that you are not really using a current FFmpeg version? Because VidCutter extracts the built-in FFmpeg at every program start to the user Temp folder (C:\Users\xxxxx\AppData\Local\Temp\_MEI54722\Bin in my case) and uses this version unless you changed the location in the settings. My settings window looks like this:
https://i.postimg.cc/YSmcf6Ld/tools.png (https://postimages.org/)
Cyber Akuma
6th July 2020, 20:30
I don't think it has anything to do with FFMPEG, that error message seems to indicate that there is an error in the Phython script itself.
Are you using the latest version of VidCutter by any chance?
And what versions of ffmpeg and mprobe are you using? And type?
manolito
6th July 2020, 22:41
I use the very latest VidCutter 6.0 version 32-bit.
For FFmpeg and FFprobe I use the latest GIT version 4.3 32-bit static. You have to be careful with current Zeranoe Point Releases. For a while all the Point Releases were the LGPL versions which did not include libx264 and libx265. I believe that this has been fixed now, but it would not hurt to check your FFmpeg version if "Enable gpl" is in this build.
If you also use a different MediaInfo version, please make sure that you do not use anything newer than version 18.5.
manolito
15th September 2020, 13:48
Just the other day I needed to make some frame accurate cuts in an AVC/AAC HD video clip, and this time I gave VidCutter a thorough test. Result: Not useable for frame accurate cutting.
I did find the reason for the crashes, though. The first and the last video sections are problematic. If either the first section starts at frame 0 of the source clip AND / OR the last section ends at the last source frame then it is guaranteed that you will get a crash. To avoid crashes the first section has to start at a certain distance from the source start, and the last section needs to end a good deal before the last source frame.
This problem would be manageable, but the other problem is that VidCutter in frame accurate mode tends to ruin the audio. My sources all have AAC audio, and after cutting with VidCutter I get clicks and audible distortion in the resulting file. This only happens in frame accurate cutting mode, and I think it is an FFmpeg issue. It also has nothing to do with different FFmpeg versions.
No wonder that the author has abandoned his software, getting frame accurate cutting to work seems to be an impossible task...
I have completely switched to Smart Cutter. It is not cheap, the user interface looks a little retro (which I like), but the results are flawless.
god_md5
22nd January 2023, 05:07
what about lossless-cut, smart cutter is alway hang with 4k file
vBulletin® v3.8.11, Copyright ©2000-2025, vBulletin Solutions Inc.