Changes between Version 11 and Version 12 of Seeking


Ignore:
Timestamp:
Aug 24, 2013, 1:33:13 AM (6 years ago)
Author:
slhck
Comment:

copyedit; clarified a few things here and there

Legend:

Unmodified
Added
Removed
Modified
  • Seeking

    v11 v12  
    77= Fast seeking =
    88
    9 (-ss parameter before -i)
     9The `-ss` parameter needs to be specified before `-i`:
    1010
    1111{{{
    12 ffmpeg -ss 00:03:00 -i Underworld.Awakening.avi -vframes 1 out1.jpg
     12ffmpeg -ss 00:03:00 -i Underworld.Awakening.avi -frames:v 1 out1.jpg
    1313}}}
    1414
    1515[[Image(out1.jpg)]]
    1616
    17 This example will produce 1 image frame (out1.jpg) somewhere around 3rd minute from the beginning of the movie. The input will be parsed '''using keyframes''', which is '''very fast'''. The drawback is that it will also finish the seeking at some keyframe, not necessarily located at specified time (00:03:00), so the seeking will not be as accurate as expected.
     17This example will produce one image frame (out1.jpg) somewhere around the third minute from the beginning of the movie. The input will be parsed '''using keyframes''', which is '''very fast'''. The drawback is that it will also finish the seeking at some keyframe, not necessarily located at specified time (00:03:00), so the seeking will not be as accurate as expected.
    1818
    1919= Accurate seeking =
    2020
    21 (-ss parameter after -i)
     21The `-ss` parameter needs to be specified after `-i`:
    2222
    2323{{{
    24 ffmpeg -i Underworld.Awakening.avi -ss 00:03:00 -vframes 1 out2.jpg
     24ffmpeg -i Underworld.Awakening.avi -ss 00:03:00 -frames:v 1 out2.jpg
    2525}}}
    2626
    2727[[Image(out2.jpg)]]
    2828
    29 This example will also produce 1 image frame (out2.jpg) precisely at 3rd minute from the beginning of the movie.
     29This example will also produce one image frame (out2.jpg) precisely at the third minute from the beginning of the movie.
    3030
    3131''Note the time difference on the clocks between the images out1.jpg and out2.jpg. The first one shows  "00:02:05:05" and the second shows "00:02:05:06".''
    3232
    33 The input is parsed '''very slowly''', frame by frame. The advantage is that you'll get the frame at exactly 3rd minute, 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.
     33Here, the input will be decoded until it reaches the position given by `-ss`. This will be done  '''very slowly''', frame by frame. The advantage is that you'll get the frame at the third minute, 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.
    3434
    3535= Fast and accurate seeking =
    3636
    37 (-ss parameter before and after -i)
     37For this we specify the `-ss` parameter before and after `-i`:
    3838
    3939{{{
    40 ffmpeg -ss 00:02:30 -i Underworld.Awakening.avi -ss 00:00:30 -vframes 1 out3.jpg
     40ffmpeg -ss 00:02:30 -i Underworld.Awakening.avi -ss 00:00:30 -frames:v 1 out3.jpg
    4141}}}
    4242
    4343[[Image(out3.jpg)]]
    4444
    45 This approach is combining the best characteristics of both fast and accurate ways of seeking in FFmpeg. Shortly, we first seek very fast somewhere before the 3rd minute and then we slow down and seek frame by frame to the 3rd minute. This works because FFmpeg will first seek by keyframes, until it reaches at 00:02:30, when it will stop at the last keyframe found (somewhat before 00:02:30, depending on the [http://en.wikipedia.org/wiki/Group_of_pictures gop size]/keyframes interval of the input) and then it will slowly seek the next 00:00:30 seconds to the desired time point. The result should be the same as in "Accurate seeking" section, only a lot faster.
     45This approach is combining the best characteristics of both fast and accurate ways of seeking in ffmpeg. We first seek very fast somewhere before the third minute. Then we slow down and seek frame by frame to the third minute. This works because ffmpeg will first seek by keyframes, until it reaches 00:02:30. This is where it stops at the last keyframe found (somewhere before 00:02:30, depending on the [http://en.wikipedia.org/wiki/Group_of_pictures GOP size]/keyframe interval of the input) and then it will slowly seek the next 00:00:30 seconds to the desired time point. The result should be the same as in "Accurate seeking" section, only a lot faster.
    4646
    4747''Note that both out2.jpg and out3.jpg show the same time on the clock "00:02:05:06"''
    4848
    49 There is no general rule how to correctly set both time points for -ss options, because those depend on the keyframe interval used when the input was encoded. But, just for the purpose of some orientation, the x264 encoder uses the gop size of 250 (which means 1 keyframe each 10 seconds if the input frame rate is 25 fps).
     49There 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).
    5050
    5151= Notes =
    5252
    53 == Cutting small section ==
     53== Cutting small sections ==
    5454
    55 For instance 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 a outpoint, like -ss 60 -to 70 to capture from second 60 to 70. "-t" and "-to" are mutually exclusive and if you use both "-t" will be used.
     55To 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.
    5656
    57 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] .
     57Note that if you specify `-ss` before `-i` only, the timestamps will be reset to zero, so `-t` and `-to` have the same effect:
     58{{{
     59ffmpeg -ss 00:01:00 -i video.mp4 -to 00:02:00 -c copy cut.mp4
     60ffmpeg -i video.mp4 -ss 00:01:00 -to 00:02:00 -c copy cut.mp4
     61}}}
    5862
    59 Example :
     63Here, the first command will cut from 00:01:00 to 00:03:00 (in the original), whereas the second command would cut from 00:01:00 to 00:02:00, as intended.
     64
     65If 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] .
     66
     67Example:
    6068{{{
    6169ffmpeg -ss 00:03:00 -i video.mp4 -t 60 -c copy -avoid_negative_ts 1 cut.mp4
     
    6472== Timestamp syntax ==
    6573
    66 Note that you can use timestamp inputs like 00:02:30 or 150 (mean the same thing).  Also if you use a fraction, like 02:30.05 this is interpreted at "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).
     74Note that you can use two different formats, 00:02:30 or 150, i.e. `[HH:MM:SS]` or just seconds. They mean the same thing.  Also, if you use a fraction, like `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.
    6775
    68 == vcodec copy ==
     76== Doing a bitstream copy gives me a broken file? ==
    6977
    70 If you use -ss with "-vcodec copy" sometimes weird things can happen, since it's forced to only use/split on i-frames.  For instance it might insert audio for 0.5s before the first video frame appears, so be careful!
     78If you use `-ss` with `-c:v copy`, the resulting bitstream might end up being choppy, not playable, or out of sync with the audio stream, since ffmpeg is forced to only use/split on i-frames.
    7179
    72 == multiple at one ==
     80== Multiple cuts at once ==
    7381
    74 You can do do multiple splits with the same file, as well, see http://stackoverflow.com/questions/5651654/ffmpeg-how-to-split-video-efficiently/12939780#12939780 which may or may not save time/cpu.
     82You can do do multiple splits with the same file as well, see [http://stackoverflow.com/questions/5651654/ffmpeg-how-to-split-video-efficiently How to split video efficiently], which may or may not save you time/CPU.