1

Let's say I need to remove everything but the second minute from a video.

I can do this, for example, in the following way:

ffmpeg -copyts -ss <start> -i input.mp4 -to <end> -c:v libx264 -crf 23 -c:a aac -b:a 192k output.mp4

I am interested in whether the -ss should be before or after the -i.

I have read all the answers and comments here and here. From what I read there, it seems that in older versions of FFmpeg, before 2.1, placing -ss before -i was for faster processing, while placing -ss after -i was for more accurate processing, but in newer versions, placing -ss before -i is not only for faster processing, but also for more accurate processing.

But on the other hand, the sections “Input seeking” and “Output seeking” here don't mention this nuance, and after reading them I'm not so sure that putting -ss before -i is "the one and only solution".

Could someone explain this? (What I want from processing is accuracy. I don't care how long it will take.)

jsx97
  • 219

1 Answers1

2

Compressed codecs use keyframes (or intra-frames) at given time-intervals. Only in keyframes is stored a complete image, while intermediate frames store only the difference to the neighboring keyframes. So, any decoding of a stream must always start at a keyframe.

Before version 2.1, ffmpeg's -ss option could not seek accurately when used as an input option (i.e. before the -i parameter). Rather, it would start at the nearest keyframe, so it was not possible to cut exactly at an indicated time. You first needed to decode the whole stream so you could select the exact frame at which to cut, and to that end you needed to use -ss as output option (i.e. after the -i parameter).

Nowadays however, the official documentation states :

-ss position (input/output)

When used as an input option (before -i), seeks in this input file to position. Note that in most formats it is not possible to seek exactly, so ffmpeg will seek to the closest seek point before position. When transcoding and -accurate_seek is enabled (the default), this extra segment between the seek point and position will be decoded and discarded. When doing stream copy or when -noaccurate_seek is used, it will be preserved.

When used as an output option (before an output url), decodes but discards input until the timestamps reach position.

So, if you are looking for accuracy, nowadays it does not make any sense to use the -ss parameter as an output option, as you just waste time to decode something which you will discard anyway.

The main use for -ss as output parameter is that it will maintain the original time codes, which might be useful in some cases.

Note that the above implies that you cannot do stream copy and at the same time start encoding at an arbitrary seek point. On the other hand, if the exact starting point is not that important, you might use -ss in combination with stream copy and avoid re-encoding.

1NN
  • 10,044