David542
David542

Reputation: 110562

ffmpeg command to extract first two minutes of a video

What would be the simplest ffmpeg command to truncate the video to the first two minutes (or do nothing if the video is less than two minutes) ? It can do a pass-through on any of the initial video settings.

Upvotes: 28

Views: 20554

Answers (2)

Suuuehgi
Suuuehgi

Reputation: 5010

This is an adapted version of David542's post since my edit was rejected as "completely superfluous or actively harm[ing] readability".

For extractions, it is essential to add the -c copy flag,

ffmpeg -i in.mov -ss 0 -t 120 -c copy out.mov

what is shorthand for -vcodec copy -acodec copy. Otherwise ffmpeg reencodes the selection what is about 1000 times slower on my machine here and possibly alters the outcome in quality due to default settings taken.


Edit

As ack-inc indicated, it makes a difference whether -ss is placed before (input option) or behind (output option) the -i.

In conjunction with stream copying (-c copy), ffmpeg will seek to the closest seek point before the position (e.g. the closest keyframe), if used as an input option. This is very fast but may be inaccurate depending on the codec and codec settings used.

If used as an output option, ffmpeg first decodes the complete stream up to the position. This uses more CPU resources and therefore takes longer, but the cut is now precise.

The latter takes about 16 times longer on my computer, but in the end it only takes about 1.5 seconds to search through 1.5 hours of FHD stream (conjunction with -c copy). In other words, computers are now sufficiently fast that this is the way of choice for the vast majority of end users.

Without stream copying -- when trans-coding -- the difference was a factor of about 500 (or 6 min at 100 % CPU).

Upvotes: 41

David542
David542

Reputation: 110562

$ ffmpeg -i in.mov -ss 0 -t 120 out.mov

Upvotes: 31

Related Questions