Extract segments of video using ffmpeg.

ffmpeg video

Cutting out a segment of video using ffmpeg is not hard, but I always forget the flags. NO MORE once I have written this today-I-learned / today-I-remembered. The post contains two different ways to extract a clip: indicating the clip’s end time (-to), or the clip’s length (-t).

ffmpeg docs.

Example 1 - end time.

An example: we have input.mp4 as the full video, and we want the section from 30 seconds to 42 seconds saved in clip.mp4

ffmpeg -i input.mp4 -ss 30 -to 42 output.mp4

This example used the -to flag. From the ffmpeg docs,

-to position (input/output)

Stop writing the output or reading the input at position. position must be a time duration specification, see (ffmpeg-utils)the Time duration section in the ffmpeg-utils(1) manual.

-to and -t are mutually exclusive and -t has priority.

(In the docs, I use a search on the web page for particular flags.)

For completeness, here is the documentation on -ss: the start time.

-ss position (input/output)

When used as an input option (before -i), seeks in this input file to position. Note that in most formats it is not possible to seek exactly, so ffmpeg will seek to the closest seek point before position. When transcoding and -accurate_seek is enabled (the default), this extra segment between the seek point and position will be decoded and discarded. When doing stream copy or when -noaccurate_seek is used, it will be preserved.

When used as an output option (before an output url), decodes but discards input until the timestamps reach position.

position must be a time duration specification, see (ffmpeg-utils)the Time duration section in the ffmpeg-utils(1) manual.

Example 2 - clip length.

Suppose you know the length of the clip you want instead of the end time. -t is the flag used in this case:

ffmpeg -i input.mp4 -ss 30 -t 12 output.mp4

And here is the documentation for -t:

-t duration (input/output)

When used as an input option (before -i), limit the duration of data read from the input file.

When used as an output option (before an output url), stop writing the output after its duration reaches duration.

duration must be a time duration specification, see (ffmpeg-utils)the Time duration section in the ffmpeg-utils(1) manual.

-to and -t are mutually exclusive and -t has priority.

Example 3 - time representation.

There are a few ways to represent time in ffmpeg, explained at the ffmpeg-utils page. For instance, we can write

ffmpeg -i input.mp4 -ss 0:00:30 -t 12 output.mp4

and get the same output as in Example 1 and Example 2.

© Amy Tabb 2018 - 2023. All rights reserved. The contents of this site reflect my personal perspectives and not those of any other entity.