日本語

Why the Position of -ss Changes FFmpeg Video Cut Results

​​FFmpeg​ffprobe,H.264,video editing

In FFmpeg, -ss placed before -i tells FFmpeg to seek to that position in the input file before reading it. Placed after -i, it tells FFmpeg to decode the input from the beginning and discard frames until it reaches that position. This is why the position of -ss changes the speed and the result when you cut part of a video (trimming or clipping). When re-encoding, both positions start from the same frame, and placing -ss before -i was much faster. With -c copy (no re-encoding), the position of -ss produced output that started in completely different ways.

Note

I tested on Ubuntu 24.04 with FFmpeg 6.1.1 (the Ubuntu package). The test video was an MP4 made from FFmpeg's test pattern (testsrc2) using H.264 (libx264), with a keyframe every 5 seconds. I did not test FFmpeg 7 or later, formats other than MP4 (such as MKV or MPEG-TS), codecs other than H.264, or how the output plays in video players or browsers.

The Documentation Says the Meaning of -ss Depends on Its Position

FFmpeg options apply to different files depending on where you put them. An option placed before -i applies to the next input file. An option placed before the output filename applies to that output file.

The official documentation for -ss (the source file for the manual published at ffmpeg.org) describes the behavior at each position as follows:

  • Before -i (input option): Seeks within the input file to position. Most formats cannot seek to the exact position, so FFmpeg moves to the closest seekable position before position (in my test video, the previous keyframe). When re-encoding, FFmpeg decodes and discards the frames from there up to position (controlled by -accurate_seek, enabled by default). With -c copy, those frames are not discarded and remain in the output.
  • After -i (output option): Decodes the input and discards frames until their timestamp reaches position.

The diagram below shows this difference. It cuts 3 seconds starting at 7 seconds (-ss 7) from a video with keyframes every 5 seconds, as in my test video.

Diagram showing four cases of which parts of the original video are decoded and which parts go into the output when cutting with -ss 7
Differences when cutting a video with keyframes every 5 seconds using -ss 7

The Test Video

I created a 60-second video with the frame number and the time in the original video drawn in the upper left corner. By converting the first frame of each output to an image, I could see where in the original video it started. I inserted keyframes with -g 150 (every 5 seconds at 30fps).

bash
ffmpeg -f lavfi -i testsrc2=duration=60:size=640x360:rate=30 \
  -f lavfi -i sine=frequency=440:duration=60 \
  -vf "drawtext=fontfile=/path/to/DejaVuSansMono-Bold.ttf:fontsize=48:fontcolor=white:box=1:boxcolor=black@0.7:x=20:y=20:text='frame %{n}  t=%{pts\:flt}'" \
  -c:v libx264 -preset veryfast -g 150 -keyint_min 150 -sc_threshold 0 \
  -pix_fmt yuv420p -c:a aac -shortest in.mp4

I replaced the font path with a placeholder value. In the actual tests I also added -hide_banner -loglevel error -y and similar options. The commands below are the same. When I checked the keyframe positions with ffprobe, they were at 0, 5, 10 seconds, and so on, every 5 seconds.

text
$ ffprobe -v error -select_streams v:0 -skip_frame nokey -show_entries frame=pts_time -of csv=p=0 in.mp4 | head -5
0.000000,
5.000000
10.000000
15.000000
20.000000

With Re-encoding, Both Positions Start from the Same Frame

I cut 3 seconds starting at 7 seconds, re-encoding the output.

bash
# -ss before -i
ffmpeg -ss 7 -i in.mp4 -t 3 -c:v libx264 -preset veryfast -c:a aac a_input_enc.mp4
# -ss after -i
ffmpeg -i in.mp4 -ss 7 -t 3 -c:v libx264 -preset veryfast -c:a aac b_output_enc.mp4

Both outputs were 3.000000 seconds long with 90 video frames. The first frame was frame 210 of the original video (7.0 seconds). The output started at exactly the requested 7 seconds, not at the 5-second keyframe. Even with -ss before -i, the frames from the keyframe up to 7 seconds are decoded and discarded.

text
$ ffprobe -v error -show_entries format=start_time,duration:stream=codec_type,start_time,duration,nb_frames -of compact=p=0 a_input_enc.mp4
codec_type=video|start_time=0.000000|duration=3.000000|nb_frames=90
codec_type=audio|start_time=0.000000|duration=3.000000|nb_frames=131
start_time=0.000000|duration=3.000000

For Long Videos, -ss Before -i Is Much Faster

The difference shows up in processing time. I created a 1-hour video (3600 seconds, 320×180, no audio) with keyframes every 5 seconds. Then I measured how long it took to cut 3 seconds starting at 3002 seconds (a non-keyframe position) with re-encoding, three times each (the real value from the time command).

Position of -ss Run 1 Run 2 Run 3
Before -i 0.154 s 0.164 s 0.153 s
After -i 10.630 s 10.656 s 11.376 s

As the official documentation explains, with -ss after -i, FFmpeg decodes the video from the beginning up to 3002 seconds and then discards it. This took more than 10 seconds. With -ss before -i, FFmpeg jumps to the previous keyframe (3000 seconds) and decodes only from there.

I also compared the decoded frames using a hash of each frame (framemd5). For cuts starting at 3002 seconds and at the keyframe at exactly 3000 seconds, the hashes matched for both placements. When re-encoding, placing -ss before -i caused no problems in my tests.

With -c copy, the Position Produces Completely Different Output

-c copy does not decode or re-encode. It repackages the data (packets) into a new file as is. As the official documentation on stream copy says, it is very fast and does not reduce quality. However, because nothing is decoded, the cut must start at a keyframe. How FFmpeg handles a cut point that does not match a keyframe depends on the position of -ss.

bash
# -ss before -i
ffmpeg -ss 7 -i in.mp4 -t 3 -c copy c_input_copy.mp4
# -ss after -i
ffmpeg -i in.mp4 -ss 7 -t 3 -c copy d_output_copy.mp4

I exported the first frame of each of the four cut videos as PNG and stacked them vertically (one above the other). Only the -c copy case with -ss after -i started at 10 seconds instead of 7 seconds.

Four first frames from the different cutting methods, stacked vertically. The top three show frame 210, and only the bottom one shows frame 300
First displayed frame. Only -c copy with -ss after -i started at 10 seconds

With -ss Before -i, the 2 Seconds from the Keyframe Are Also in the File

When decoded with FFmpeg, c_input_copy.mp4 (with -ss before -i) started displaying at the 7.0-second frame. However, when I looked at the file with ffprobe, it contained 152 video packets, and the first packet's timestamp was -2.0 seconds.

text
$ ffprobe -v error -select_streams v:0 -show_entries packet=pts_time,flags -of csv=p=0 c_input_copy.mp4 | head -3
-2.000000,KD_
-1.900000,_D_
-1.966667,_D_

The D in the second column is a flag that marks a packet used for decoding but not displayed. (In the ffprobe source, AV_PKT_FLAG_DISCARD, the value that marks packets to discard after decoding, is shown as D.) The 2 seconds from the 5-second keyframe up to 7 seconds are in the file, and they are treated as not displayed.

FFmpeg reads an MP4 file according to the timeline described by its edit list. To check this, I used -ignore_editlist 1 (MP4 demuxer option) to ignore the edit list. Then 152 frames were decoded, and the first frame was frame 150 (5.0 seconds). Starting display at 7 seconds, after skipping the first 2 seconds, is the result of following the edit list.

Also, although I specified -t 3, FFmpeg decoded 92 video frames. That is 2 more than the 90 frames from the re-encoded cut. So a -c copy cut does not produce an exact frame-level length.

When I cut from the keyframe at exactly 10 seconds (-ss 10 -i in.mp4 -t 3 -c copy), the first packet was the keyframe at 0 seconds, and there were no packets marked D. All 92 frames in the file were decoded, and the first frame was frame 300 (10.0 seconds).

With -ss After -i, the Video Is Dropped Until the Next Keyframe

d_output_copy.mp4 (with -ss after -i) had about 3 seconds of audio but only 2 video frames. Those video frames also start at 3.0 seconds in the output.

text
$ ffprobe -v error -show_entries format=start_time,duration:stream=codec_type,start_time,duration,nb_frames -of compact=p=0 d_output_copy.mp4
codec_type=video|start_time=3.000000|duration=0.066667|nb_frames=2
codec_type=audio|start_time=0.011995|duration=2.995374|nb_frames=129
start_time=0.011995|duration=3.054672

The 2 frames were the 10-second keyframe (frame 300) and the frame at 10.1 seconds. The video from 7 to 10 seconds is not in the output. As the official documentation for -copyinkf explains, with -c copy FFmpeg does not include frames before the first keyframe in the output. Because the cut started at 7 seconds, all frames up to the next keyframe (10 seconds) were dropped.

I also tried adding -copyinkf to include the frames from 7 to 10 seconds. The file then contained 90 video packets. However, when decoded with the FFmpeg version in my test environment, the following errors appeared, and only the 2 frames from the 10-second keyframe could be decoded.

text
$ ffprobe -v error -select_streams v:0 -count_frames -show_entries stream=nb_frames,nb_read_frames -of compact=p=0 h_output_copy_inkf.mp4
[h264 @ 0x557eb7d689c0] co located POCs unavailable
[h264 @ 0x557eb7d689c0] mmco: unref short failure
[h264 @ 0x557eb7e108c0] co located POCs unavailable
[h264 @ 0x557eb7e108c0] mmco: unref short failure
[h264 @ 0x557eb7e108c0] co located POCs unavailable
nb_frames=90|nb_read_frames=2

Even when I specified exactly 10 seconds, the keyframe (-i in.mp4 -ss 10 -t 3 -c copy), no video was included. The output file contained only an audio stream, and FFmpeg's output also showed video:0kB.

Warning

After cutting, check the number of frames that are actually decoded. nb_frames is the number of packets in the file. In the -c copy example with -ss before -i, nb_frames was 152, but only 92 frames were decoded. The nb_read_frames value from the following command is the number of frames actually decoded.

bash
ffprobe -v error -select_streams v:0 -count_frames \
  -show_entries stream=nb_frames,nb_read_frames -of compact=p=0 out.mp4

The Meaning of -to Also Changes with the Position of -ss

The position of -ss also changed the result when specifying the end point with -to. I wanted to cut from 7 to 10 seconds and used -to 10 in three ways. The output lengths were as follows.

Command Output length
ffmpeg -ss 7 -i in.mp4 -to 10 … 10.000000 s
ffmpeg -i in.mp4 -ss 7 -to 10 … 3.000000 s
ffmpeg -ss 7 -to 10 -i in.mp4 … 3.000000 s

Only the first line, with -ss before -i and -to after -i, produced 10 seconds. The last frame was frame 509 of the original video (16.97 seconds), so the output covered 7 to 17 seconds. Because the input was seeked, the output timestamps start at 0 (the start_time of this file was 0). The output option -to 10 stops at the 10-second position of that output, so the result is "10 seconds from 7 seconds."

When both -ss and -to are placed before -i, or both after -i, they are interpreted as positions in the original video, and the result was 3 seconds from 7 to 10 seconds. If you want to specify the length, use -t to avoid confusion. According to the official documentation for -t, when both -t and -to are specified, -t takes priority.

How to Write It for Each Goal

The following table summarizes the test results by goal.

What you want to do How to write it Result in my tests
Cut starting at an exact time -ss before -i, with re-encoding Starts at 7.0 s. About 0.15 s even at 3002 s in a 1-hour video
Cut quickly without re-encoding -ss before -i, with -c copy Starts at 7.0 s when decoded with FFmpeg. The 2 seconds from the keyframe are also in the file
Avoid this -ss after -i, with -c copy With 7 s, video is dropped until 10 s. With 10 s, no video is included

With -c copy and -ss before -i, when I cut from the keyframe at exactly 10 seconds, no extra packets were included. You can check keyframe positions with the -skip_frame nokey ffprobe command shown above.

​Posted by 86