Changes between Version 29 and Version 30 of Seeking


Ignore:
Timestamp:
May 24, 2024, 2:04:46 PM (2 years ago)
Author:
MasterQuestionable
Comment:

Rough clean-up.

Legend:

Unmodified
Added
Removed
Modified
  • Seeking

    v29 v30  
    1 [[PageOutline(1-100,Contents)]]
     1[[PageOutline( 0-9223372036854775807, Contents )]]
    22
    33= Introduction =
    44
    5 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. [http://ffmpeg.org/ffmpeg.html#Main-options The parameter -ss] is used to seek within the input and it can be used in several ways.
     5͏    To extract specific part of input: seeking has to be performed on which first.\\
     6͏    [https://ffmpeg.org/ffmpeg.html#Main-options ͏"-ss"] can be used to seek within the input several ways.
    67
    7 = Input seeking =
     8= Input seeking = #Input
    89
    9 The `-ss` parameter needs to be specified somewhere before `-i`:
     10The "-ss" parameter needs to be specified somewhere before "-i":
     11{{{#!shell
     12ffmpeg -ss 00:23:00 -i "Mononoke.Hime.mkv" -frames:v 1 "out1.jpg"
     13}}}
     14[[Image( "out.jpg" )]]
    1015
    11 {{{
    12 ffmpeg -ss 00:23:00 -i Mononoke.Hime.mkv -frames:v 1 out1.jpg
     16͏    This example will produce 1 image frame ("out1.jpg") at 23 min from the beginning of the movie.\\
     17͏    The input will be parsed '''using keyframes''', which is '''very fast'''.
     18
     19͏    As of FFmpeg 2.1, when transcoding with !`ffmpeg` (i.e. not [https://ffmpeg.org/ffmpeg.html#Stream-copy stream copying]): "-ss" is also "frame-accurate" even as input option.\\
     20͏    Previous behavior (seek only to nearest preceding keyframe, despite inaccuracy) can be restored with "-noaccurate_seek".
     21
     22= Output seeking = #Output
     23
     24The "-ss" parameter needs to be specified after "-i":
     25{{{#!shell
     26ffmpeg -i "Mononoke.Hime.mkv" -ss 00:23:00 -frames:v 1 "out2.jpg"
     27}}}
     28[ Identical Image ]
     29
     30This example will also produce 1 image frame ("out2.jpg") precisely at 23 min from the beginning of the movie.
     31
     32͏    Here, the input will be decoded (and discarded) until it reaches the position given by "-ss".\\
     33͏    This will be done '''relatively slow''', frame-by-frame.
     34
     35͏    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.\\
     36͏    I.e. when [/wiki/HowToBurnSubtitlesIntoVideo burning subtitles into a video], you don't need to modify the subtitle timestamps.\\
     37͏    But the drawback is that it will take a lot of time until it finally reaches that time point.\\
     38͏    The bigger the seeking time is, the longer you will have to wait.
     39
     40= Combined seeking = #Combined
     41
     42For this we specify the "-ss" parameter before and after "-i":
     43{{{#!shell
     44ffmpeg -ss 00:22:30 -i "Mononoke.Hime.mkv" -ss 00:00:30 -frames:v 1 "out3.jpg"
     45}}}
     46[ Identical Image ]
     47
     48͏    As of FFmpeg 2.1, combined seeking is still possible.\\
     49͏    But I ([/wiki/Seeking?action=diff&version=13 c-14]) have yet to find a valid use case for it: since "-ss" as an input option is now both fast and accurate.
     50
     51This 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).
     52
     53͏    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.\\
     54͏    To give some orientation, the x264 encoder by default uses a GOP (Group of Pictures) size of 250:\\
     55͏    Which means 1 keyframe every 10 s if the input frame rate is 25 FPS.\\
     56\\
     57\\
     58= Notes =
     59
     60== Cutting small sections == #Cutting
     61
     62͏    To extract only a small segment amid a movie, it can be used in combination with "-t":\\
     63͏    Which specifies the duration, like "-ss 60 -t 10" to capture from 60 s to 70 s.\\
     64͏    Or you can use the "-to" option to specify an out point, like "-ss 60 -to 70" to capture the same.\\
     65͏    "-t", "-to" are mutually exclusive: when both specified, "-t" takes precedence.
     66
     67͏    Note when "-ss" used before "-i" only: ("-ss" as input option) the timestamps will be reset according to 0.\\
     68͏    So "-t" and "-to" shall be the same.\\
     69͏    To keep the original timestamps: add "-copyts".
     70
     71͏    The 1st command will cut from 00:01:00 to 00:03:00 (in the original), using the faster seek.\\
     72͏    The 2nd command will cut from 00:01:00 to 00:02:00, as intended, using the slower seek.\\
     73͏    The 3rd command will cut from 00:01:00 to 00:02:00, as intended, using the faster seek.
     74{{{#!shell
     75ffmpeg -ss 00:01:00 -i "video.mp4" -to 00:02:00 -c copy "cut.mp4"
     76ffmpeg -i "video.mp4" -ss 00:01:00 -to 00:02:00 -c copy "cut.mp4"
     77ffmpeg -ss 00:01:00 -i "video.mp4" -to 00:02:00 -c copy -copyts "cut.mp4"
    1378}}}
    1479
    15 [[Image(out.jpg)]]
     80͏    If you cut with stream copy ("-c copy") you need to use the [https://ffmpeg.org/ffmpeg-formats.html#Format-Options ͏"-avoid_negative_ts 1"] option:\\
     81͏    If you want to use that segment with the [/wiki/Concatenate#demuxer ͏"concat"] demuxer.
    1682
    17 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 [https://ffmpeg.org/ffmpeg.html#Stream-copy 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.
     83E.g.
     84{{{#!shell
     85ffmpeg -ss 00:03:00 -i "video.mp4" -t 60 -c copy -avoid_negative_ts 1 "cut.mp4"
     86}}}
     87͏    If you have to re-encode anyway, e.g. to apply filters like [AfadeCurves ͏"afade"] that can be very slow.\\
     88͏    Make sure to use !`-ss 120 -i "some.mov" -to 60` alike to get the 1 min: from 120 s to (( 120 + 60 )) s.\\
     89͏    Not "-to 180" for the 3 min starting at 120 s.
    1890
    19 = Output seeking =
     91== Time unit syntax == #Time-unit
    2092
    21 The `-ss` parameter needs to be specified after `-i`:
     93͏    2 formats supported:\\
     94͏    |*| Sexagesimal ("HOURS:MM:SS.MILLISECONDS", as in "01:23:45.678")\\
     95͏    |*| Seconds
    2296
    23 {{{
    24 ffmpeg -i Mononoke.Hime.mkv -ss 00:23:00 -frames:v 1 out2.jpg
    25 }}}
     97͏    If the fraction part presents (e.g. "02:30.05"): it's interpreted as "2 m 30.05 s", not the frame !#5.\\
     98͏    Equivalent as "150.05" in seconds.
    2699
    27 [[Image(out.jpg)]]
     100== Seeking while codec copy == #codec-copy
    28101
    29 This example will also produce one image frame (out2.jpg) precisely at the twenty-third minute from the beginning of the movie.
     102͏    Using "-ss" as input/output option together with "-c copy" alike may not be accurate:\\
     103͏    Since !`ffmpeg` may only split on I-frame. (keyframe independently decodable)\\
     104͏    Though it may, if applicable: auto-adjust the stream's start time to negative to compensate.
    30105
    31 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 [https://trac.ffmpeg.org/wiki/HowToBurnSubtitlesIntoVideo 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.
     106͏    E.g. requested timestamp 157 s; but no keyframe until 159 s:\\
     107͏    It shall include 2 s of audio (no video) at the start, then start from the 1st keyframe.
    32108
    33 = Combined seeking =
    34 
    35 For this we specify the `-ss` parameter before and after `-i`:
    36 
    37 {{{
    38 ffmpeg -ss 00:22:30 -i Mononoke.Hime.mkv -ss 00:00:30 -frames:v 1 out3.jpg
    39 }}}
    40 
    41 [[Image(out.jpg)]]
    42 
    43 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.
    44 
    45 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)
    46 
    47 ''Note that all images created by the previous commands should be identical.''
    48 
    49 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).
    50 
    51 = Notes =
    52 
    53 == Cutting small sections ==
    54 
    55 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.
    56 
    57 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.
    58 
    59 The first command will cut from 00:01:00 to 00:03:00 (in the original), using the faster seek.[[BR]]
    60 The second command will cut from 00:01:00 to 00:02:00, as intended, using the slower seek.[[BR]]
    61 The third command will cut from 00:01:00 to 00:02:00, as intended, using the faster seek.
    62 
    63 {{{
    64 ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy cut.mp4
    65 ffmpeg -i video.mp4 -ss 00:01:00 -to 00:02:00 -c copy cut.mp4
    66 ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy -copyts cut.mp4
    67 }}}
    68 
    69 If you cut with stream copy (`-c copy`) you need to use the [https://ffmpeg.org/ffmpeg-all.html#Format-Options -avoid_negative_ts 1] option if you want to use that segment with the [https://trac.ffmpeg.org/wiki/How%20to%20concatenate%20(join,%20merge)%20media%20files#demuxer concat demuxer] .
    70 
    71 Example:
    72 {{{
    73 ffmpeg -ss 00:03:00 -i video.mp4 -t 60 -c copy -avoid_negative_ts 1 cut.mp4
    74 }}}
    75 
    76 If you have to re-encode anyway, e.g., to apply filters like [AfadeCurves 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.
    77 
    78 == Time unit syntax ==
    79 
    80 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.
    81 
    82 == Seeking while doing a codec copy ==
    83 
    84 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.
     109͏    So be careful when splitting and doing codec copy.