Log in

View Full Version : Cut beginning of h264 video with minimal re-encoding?


Wowfunhappy
18th April 2018, 17:01
Ffmpeg makes it super easy to losslessly cut off the end of an existing h264 stream: you just use -codec copy alongside -t or -to to specify how long you want the clip to be.

Cutting off the beginning of a stream with -ss, however, does not work so well. I always end up with a corrupted second or two of video at the beginning of the resulting clip.

I understand why this is—h264 frames cannot exist independently, as they rely on information from previous frames. As a result, some frames will always have to be re-encoded—but I should be able to avoid re-encoding everything, right?

How can I cut a portion from the beginning of an h264 stream while re-encoding only the minimum number of frames required? I would prefer to do this via ffmpeg, if possible

Thanks!

sneaker_ger
18th April 2018, 22:15
According to the documentation (https://trac.ffmpeg.org/wiki/Seeking) ffmpeg is supposed to seek to keyframes so corruption isn't supposed to happen. Try latest version and if that doesn't work make a bug report. But maybe that is a limitation with OpenGOP style encodings. I do not know. ffmpeg doesn't support automatic "smart" encoding where only the affected GOP is re-encoded and the rest is copied.

There are other softwares for that: SolveigMM Video Splitter, VideoReDo, Selur's "MKV Cutter (https://www.videohelp.com/software/MKV-Cutter)" and probably a hand full of others I forgot about.

Wowfunhappy
20th April 2018, 15:58
Thanks for that link. I'd seen it before but a very long time ago. Reading through, I might I misinterpreted my problem:

Using -ss as input option together with -c:v copy might not be accurate since ffmpeg is forced to only use/split on i-frames. Though it will—if possible—adjust the start time of the stream to a negative value to compensate for that. Basically, if you specify "second 157" and there is no key frame until second 159, it will include two seconds of audio (with no video) at the start, then will start from the first key frame. So be careful when splitting and doing codec copy.

I think this is working correctly. When I cut the video, players output a couple seconds of audio with no video before the video comes in. I interpreted this as "the video stream is corrupted."

Is there a way to tell ffmpeg to not include audio only at the beginning?

ChaosKing
20th April 2018, 16:17
If you want a GUI for cutting use http://avidemux.sourceforge.net/
You can jump to next keyframe (IDR) and cut without re-encoding.

kuchikirukia
23rd April 2018, 06:45
Just use mkvtoolnix GUI. It cuts cleanly on keyframes.

manolito
23rd April 2018, 14:19
If key frame accuracy is not enough, there are not many free choices. Solveig and VideoRedo are commercial. The only free offerings for frame accurate cutting I have found are MKVCutter by Selur and the current versions of VidCutter. I have not tested them, but they sure look promising...

Cheers
manolito