# (Efficiently) Extract multiple clips from a long video, possibly with ffmpeg

I've got a long video (2 hours at high resolution). We might as well assume it's in mp4 format, because it's in an AVCHD, and I'll have to extract and convert it to mp4 anyhow.

I'd like to extract from it about 40 clips, of varying lengths. I know the "start" and "stop" time for each. It might look like this:

0:00:15 to 0:01:30 -> clip1.mp4
0:01:55 to 0:03:10 -> clip2.mp4
0:03:45 to 0:05:12 -> clip2.mp4
... (many more lines)
1:55:45 to 1:57:11 -> clip41.mp4


I can certainly write a script to extract each of these individually (via something like what's described here), but that requires "reading through" the source forty times, which can be slow. Is there a way to get ffmpeg to do this in some sort of sequential fashion in a single pass? Or do I just resign myself to an overnight background process?

• The referenced command does an efficient job, and won't read through MP4s but use an index to jump to the inpoint. – Gyan Feb 19 '19 at 5:58
• In my experience, using -ss, even with the newest versions of ffmpeg, does not give perfect frame accuracy. It gives frame accuracy 99% of the time, but that's not good enough in a production environment. The only thing that I've found to be perfectly accurate is  -vf select=between(n\,123\,456)  However, this has exactly the speed problem that you're trying to avoid. – Andrew Klaassen May 3 '19 at 17:25
• Can you share the command+file which doesn't give perfect accuracy? – Gyan May 3 '19 at 17:32
• Anything along the lines of "ffmpeg -ss <start> -i input.mp4 ....". If you use "-vcodec copy", as in the link you suggested, you'll get cuts on keyframes, which can be a number of frames away from the frame you actually want to cut on. Even if you re-encode, though, you'll often get off-by-one errors, presumably from floating point division mismatches. Let's say you want to start at frame 23 of a 23.976fps movie. Since -ss doesn't allow frame numbers, you'll have to specify something like "-ss 0.9592926259592927". Sometimes that'll start you at frame 22 instead of frame 23. – Andrew Klaassen May 3 '19 at 17:41
• I made a change today morning that may improve seeking in MP4/MOVs. Try the latest builds. – Gyan May 3 '19 at 18:07