FFmpeg / X264: Split video in the middle of the GOP without re-encoding the entire stream

I have a H264 video (stored in an MP4 file). Each batch of GOP is about 10 s. I want to trim the first couple of seconds from the video, which means that I need to split the first GOP. Is there a way to do this without re-encoding the entire video?

I have FFmpeg and x264. I am happy to use either the FFmpeg command line or my own program related to ffmpeg x264 libraries.

Thanks in advance!

+4
source share
1 answer

I think you understand that some transcoding is inevitable. You may be able to avoid re-encoding the entire video using the following approach:

  • Divide the video into two parts: the first GOP and the rest of the video. You should be able to do this without re-encoding (use -vcodec copy )
  • Transform the first GOP into something that you can easily cut, like YUV
  • Cut yuv file
  • Re-encode the section file using the same parameters as the original video (you can get them from ffprobe .
  • Attach the re-encoded section file to the rest of the video (from the first step). Again, you should use -vcodec copy to ensure that re-encoding is not performed.

I've never had to do this, but the FAQ seems to have a section on joining video files.

+4
source

All Articles