2021年2月16日 星期二

使用 FFmpeg 將行車記錄器影片多餘內容裁剪、保留重點段落

這篇就是單純筆記加上一些簡單範例。

其實就是下面這些選項的結合 ( via https://ffmpeg.org/ffmpeg.html#Main-options )

-t duration (input/output)

When used as an input option (before -i), limit the duration of data read from the input file.

When used as an output option (before an output url), stop writing the output after its duration reaches duration.

-to and -t are mutually exclusive and -t has priority.

-to position (input/output)

Stop writing the output or reading the input at position.

-to and -t are mutually exclusive and -t has priority.

-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.

-sseof position (input)

Like the -ss option but relative to the "end of file". That is negative values are earlier in the file, 0 is at EOF.

-itsoffset offset (input)

Set the input time offset.

The offset is added to the timestamps of the input files. Specifying a positive offset means that the corresponding streams are delayed by the time duration specified in offset.


上面提到的 duration、position、offset 參數,其實都是同樣的時間格式 ( via https://ffmpeg.org/ffmpeg-utils.html#Time-duration ),像是

55

55 seconds

0.2

0.2 seconds

200ms

200 milliseconds, that’s 0.2s

200000us

200000 microseconds, that’s 0.2s

12:03:45

12 hours, 03 minutes and 45 seconds

23.189

23.189 seconds



以 5 分鐘的行車記錄器影片檔為例,各種裁剪需求舉例如下:


前面 1 分鐘的內容都不要

ffmpeg -ss '00:01:00' -i input.avi -c copy output.avi


只保留前面 1 分鐘的內容

ffmpeg -t '00:01:00' -i input.avi -c copy output.avi

ffmpeg -to '00:01:00' -i input.avi -c copy output.avi


只保留 1 分 23 秒起算,長 2 分 34 秒的內容

ffmpeg -ss '00:01:23' -t '00:02:34' -i input.avi -c copy output.avi


只保留 1 分 23 秒至 4 分 56 秒之間的內容

ffmpeg -ss '00:01:23' -to '00:04:56' -i input.avi -c copy output.avi


只保留最後 1 分鐘的內容

ffmpeg -sseof '-00:01:00' -i input.avi -c copy output.avi


沒有留言: