wiki:Seeking

Version 30 (modified by MasterQuestionable, 2 years ago) ( diff )

Rough clean-up.

Introduction

͏    To extract specific part of input: seeking has to be performed on which first.
͏    ͏"-ss" can be used to seek within the input 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 1 image frame ("out1.jpg") at 23 min 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 also "frame-accurate" even as input option.
͏    Previous behavior (seek only to nearest preceding keyframe, despite inaccuracy) can be restored with "-noaccurate_seek".

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"

[ Identical Image ]

This example will also produce 1 image frame ("out2.jpg") precisely at 23 min 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 relatively slow, 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"

[ Identical Image ]

͏    As of FFmpeg 2.1, combined seeking is still possible.
͏    But I (c-14) 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).

͏    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 (Group of Pictures) size of 250:
͏    Which means 1 keyframe every 10 s if the input frame rate is 25 FPS.


Notes

Cutting small sections

͏    To extract only a small segment amid a movie, it can be used in combination with "-t":
͏    Which specifies the duration, like "-ss 60 -t 10" to capture from 60 s to 70 s.
͏    Or you can use the "-to" option to specify an out point, like "-ss 60 -to 70" to capture the same.
͏    "-t", "-to" are mutually exclusive: when both specified, "-t" takes precedence.

͏    Note when "-ss" used before "-i" only: ("-ss" as input option) the timestamps will be reset according to 0.
͏    So "-t" and "-to" shall be the same.
͏    To keep the original timestamps: add "-copyts".

͏    The 1st command will cut from 00:01:00 to 00:03:00 (in the original), using the faster seek.
͏    The 2nd command will cut from 00:01:00 to 00:02:00, as intended, using the slower seek.
͏    The 3rd 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.

E.g.

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" that can be very slow.
͏    Make sure to use `-ss 120 -i "some.mov" -to 60` alike to get the 1 min: from 120 s to (( 120 + 60 )) s.
͏    Not "-to 180" for the 3 min starting at 120 s.

Time unit syntax

͏    2 formats supported:
͏    |*| Sexagesimal ("HOURS:MM:SS.MILLISECONDS", as in "01:23:45.678")
͏    |*| Seconds

͏    If the fraction part presents (e.g. "02:30.05"): it's interpreted as "2 m 30.05 s", not the frame #5.
͏    Equivalent as "150.05" in seconds.

Seeking while codec copy

͏    Using "-ss" as input/output option together with "-c copy" alike may not be accurate:
͏    Since `ffmpeg` may only split on I-frame. (keyframe independently decodable)
͏    Though it may, if applicable: auto-adjust the stream's start time to negative to compensate.

͏    E.g. requested timestamp 157 s; but no keyframe until 159 s:
͏    It shall include 2 s of audio (no video) at the start, then start from the 1st keyframe.

͏    So be careful when splitting and doing codec copy.

Attachments (4)

Download all attachments as: .zip

Note: See TracWiki for help on using the wiki.