Can I cut at keyframes using ffmpeg?

Viewed 1405

I am using this command

ffmpeg -start_at_zero -copyts -ss <start time> -t <duration> -i <SrcFile> -c copy <DstFile>

to fast cut a part of the mp4 video. I got the keyframes so that I choose the <start time> to be of one of them. I thought this will give me an accurate output video starting exactly at that keyframe, but this doesn't happen. How can I achieve this (fast and accurate cut at the keyframe) ? In another words: How ffmpeg decides where to start cutting? I thought it uses the neatest keyframe.

And another question: How can I encode a portion of the mp4 with exact same video and audio options? I don't know those options. I just want to keep the output as the original video. The cut in this second case can be at any frame (not always a keyframe). Thanks.

1 Answers

I have a solution, but I don't know how to do it using current ffmpeg commands (my trials to copy at keyframes didn't come accurate too. I want to know how ffmpeg decides the cutpoints).

I suggested suggest this algorithm, to divide the segment (t1, t2) that we want to copy to 3 parts: 1.a part (t1-x, t1+y), which is a complete encoded block that should be re-encoded to be able to copy the part (t1, y) precisely. 2.a part (t2-z, t3+w), which is a complete encoded block that should be re-encoded to be able to copy the part (z, t2) precisely. 3.a middle part (y, z) which contains complete encoded blocks, where it can be copied as is. 4.Join the 3 parts resulted from the above steps.

Note that the first two parts are expected to be small (and one of them or both can be zero length), so, the re-encoding process will be fast. This will make us able to have exact cuts with slightly slower operation but still super faster than re-encoding the full video. It can be even faster if we can do multiple cuts with one command, so we traverse the frames once. I hope if someone can apply this, and tell us how, or mention some of the ffmpeg team, or deliver it to them anyhow.

Related