0

So I am looking for a way to cut a video file to the last 10 seconds of the file. Normally I would use FFMPEG or mencoder but the situation is that the file to be cut is going to be a different size every time it's cut. So I found some stuff for a general duration using ffmpeg but the problem is I would need the duration to be in seconds instead of 00:00:00 so then I could just use the var-10 for the -ss.

I don't know if I explained that well enough but in short:

Different length video every time it is ran, only need the last 10 seconds in a new file. Need something light on resources but still fast.

Thanks in advance for anyone who answers!

1 Answers1

0

It is easy, but not very easy, and certainly not very fast. Generally what you want is:

  • Obtain the full length of video. This may be very easy to get it from header, or may be very difficult such as when you have an RTP stream dump in a file, or when a file format supports (and uses) the variable FPS and has no "length" field in the header. Those cases may require playing the whole file to determine length.

  • Seek to the nearest keyframe before -10s of this length. This may be very easy in case of indexed file with proper keyframes, or difficult in the case above. It also may be the case that the only keyframe is the first keyframe, so you'd need to decode the whole stream just to get to the last 10s.

  • Decode a few frames after the keyframe until you hit this -10s mark.

  • Reencode the remaining frames. In theory if your keyframe is exactly at -10s mark you can just dump the packets without reencoding, but you're not likely to be so lucky.

George Y.
  • 11,307
  • 3
  • 24
  • 25