Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFmpeg/X264: Split video mid-GOP without reencoding entire stream

I've got an H264 video (Stored in an MP4 file). Each GOP is approx 10s long. I want to trim the first couple of seconds off the video, which means I need to split the first GOP. Is there a way to do this without re-encoding the entire video?

I've got FFmpeg and x264 available. I'm happy to use either the FFmpeg command line, or my own program linked against ffmpeg of x264 libraries.

Thanks in advance!

like image 301
yoda_alex Avatar asked Sep 25 '11 10:09

yoda_alex


1 Answers

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

  • Split 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)
  • Convert the first GOP into something that you can easily cut, such as YUV
  • Cut the YUV file
  • Re-encode the cut file using the same parameters as the original video (you can get these from ffprobe.
  • Join the re-encoded cut file with the rest of the video (from step one). Again, you would use -vcodec copy to ensure no re-encoding is performed.

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

like image 99
mpenkov Avatar answered Nov 15 '22 09:11

mpenkov