| 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 | |
| | 24 | The "-ss" parameter needs to be specified after "-i": |
| | 25 | {{{#!shell |
| | 26 | ffmpeg -i "Mononoke.Hime.mkv" -ss 00:23:00 -frames:v 1 "out2.jpg" |
| | 27 | }}} |
| | 28 | [ Identical Image ] |
| | 29 | |
| | 30 | This 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 | |
| | 42 | For this we specify the "-ss" parameter before and after "-i": |
| | 43 | {{{#!shell |
| | 44 | ffmpeg -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 | |
| | 51 | 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). |
| | 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 |
| | 75 | ffmpeg -ss 00:01:00 -i "video.mp4" -to 00:02:00 -c copy "cut.mp4" |
| | 76 | ffmpeg -i "video.mp4" -ss 00:01:00 -to 00:02:00 -c copy "cut.mp4" |
| | 77 | ffmpeg -ss 00:01:00 -i "video.mp4" -to 00:02:00 -c copy -copyts "cut.mp4" |
| 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. |