wiki:Seeking

Introduction

If you need to extract only a specific part of your input, you'll need to use the seeking option to get to that specific part in the input first. The parameter -ss is used to seek within the input and it can be used in several ways.

Input seeking

The -ss parameter needs to be specified somewhere before -i:

ffmpeg -ss 00:23:00 -i Mononoke.Hime.mkv -frames:v 1 out1.jpg

Example output for "Seeking with FFmpeg".

This example will produce one image frame (out1.jpg) at the twenty-third minute from the beginning of the movie. The input will be parsed using keyframes, which is very fast. As of FFmpeg 2.1, when transcoding with ffmpeg (i.e. not stream copying), -ss is now also "frame-accurate" even when used as an input option. Previous behavior (seeking only to the nearest preceding keyframe, even if not precisely accurate) can be restored with the -noaccurate_seek option.

Output seeking

The -ss parameter needs to be specified after -i:

ffmpeg -i Mononoke.Hime.mkv -ss 00:23:00 -frames:v 1 out2.jpg

Example output for "Seeking with FFmpeg".

This example will also produce one image frame (out2.jpg) precisely at the twenty-third minute from the beginning of the movie.

Here, the input will be decoded (and discarded) until it reaches the position given by -ss. This will be done very slowly, frame by frame. As of FFmpeg 2.1, the main advantage is that when applying filters to the output stream, the timestamps aren't reset prior to filtering (i.e. when burning subtitles into a video, you don't need to modify the subtitle timestamps), but the drawback is that it will take a lot of time until it finally reaches that time point. The bigger the seeking time is, the longer you will have to wait.

Combined seeking

For this we specify the -ss parameter before and after -i:

ffmpeg -ss 00:22:30 -i Mononoke.Hime.mkv -ss 00:00:30 -frames:v 1 out3.jpg

Example output for "Seeking with FFmpeg".

As of FFmpeg 2.1, combined seeking is still possible but I have yet to find a valid use case for it since -ss as an input option is now both fast and accurate.

This approach uses keyframes to seek until 00:22:30, and then seeks frame-by-frame until it reaches 00:23:00 (00:22:30 + 00:00:30)

Note that all images created by the previous commands should be identical.

There is no general rule on how to correctly set both time points for -ss options, because those depend on the keyframe interval used when the input was encoded. To give some orientation, the x264 encoder by default uses a GOP size of 250 (which means 1 keyframe each 10 seconds if the input frame rate is 25 fps).

Notes

Cutting small sections

To extract only a small segment in the middle of a movie, it can be used in combination with -t which specifies the duration, like -ss 60 -t 10 to capture from second 60 to 70. Or you can use the -to option to specify an out point, like -ss 60 -to 70 to capture from second 60 to 70. -t and -to are mutually exclusive. If you use both, -t will be used.

Note that if you specify -ss before -i only, the timestamps will be reset to zero, so -t and -to will have the same effect. If you want to keep the original timestamps, add the -copyts option.

The first command will cut from 00:01:00 to 00:03:00 (in the original), using the faster seek.
The second command will cut from 00:01:00 to 00:02:00, as intended, using the slower seek.
The third command will cut from 00:01:00 to 00:02:00, as intended, using the faster seek.

ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy cut.mp4
ffmpeg -i video.mp4 -ss 00:01:00 -to 00:02:00 -c copy cut.mp4
ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy -copyts cut.mp4

If you cut with stream copy (-c copy) you need to use the -avoid_negative_ts 1 option if you want to use that segment with the concat demuxer .

Example:

ffmpeg -ss 00:03:00 -i video.mp4 -t 60 -c copy -avoid_negative_ts 1 cut.mp4

If you have to re-encode anyway, e.g., to apply filters like afade, which can be very slow, make sure to use, e.g., -ss 120 -i some.mov -to 60 to get one minute from 120s to 120+60s, not -to 180 for three minutes starting at 120s.

Time unit syntax

Note that you can use two different time unit formats: sexagesimal (HOURS:MM:SS.MILLISECONDS, as in 01:23:45.678), or in seconds. If a fraction is used, such as 02:30.05, this is interpreted as "5 100ths of a second", not as frame 5. For instance, 02:30.5 would be 2 minutes, 30 seconds, and a half a second, which would be the same as using 150.5 in seconds.

Seeking while doing a codec copy

Using -ss as input/output option together with -c:v copy might not be accurate since ffmpeg is forced to only use/split on i-frames. Though it will—if possible—adjust the start time of the stream to a negative value to compensate for that. Basically, if you specify "second 157" and there is no key frame until second 159, it will include two seconds of audio (with no video) at the start, then will start from the first key frame. So be careful when splitting and doing codec copy.

Last modified 8 months ago Last modified on Jul 16, 2023, 6:40:47 PM

Attachments (4)

Download all attachments as: .zip

Note: See TracWiki for help on using the wiki.