Use FFmpeg to achieve precise video cutting

Basic cut command:

Use  -ss and  -t options, start from the 30th second, capture 10 seconds of video backwards, and save:

  1. ffmpeg -i input.wmv -ss 00:00:30.0 -c copy -t 00:00:10.0 -codec copy output.wmv

  2. ffmpeg -i input.wmv -ss 30 -c copy -t 10  -codec copy output.wmv

To achieve the same effect, you can also use  -ss and  -to options,

ffmpeg -i input.wmv -ss 30 -c copy -to 40 -codec copy  output.wmv

But the video cut in this way is not accurate, and the cut position will be repeated

According to online information, the -accurate_seek parameter is added in front of -i and the -avoid_negative_ts 1 parameter

ffmpeg -ss {0} -to {1} -accurate_seek -i {2}  -codec copy  -avoid_negative_ts 1  -y {3}

The result is that the cropping accuracy of the video head is basically satisfactory but the tail is still inaccurate. The second paragraph deviates forward by 5s, which is definitely not acceptable.

How to realize the cutting position, the front and rear ends are very accurate, after searching, the following three ways are feasible:

The first method: adjust the position of the key frame of the video before cutting, and then cut,

Key parameters: -g 1 -keyint_min 2 

example:

ffmpeg -y -i beautiful myth.mp4 -ss 0 -t 295 -c:a copy -vcodec libx264 -keyint_min 2 -g 1 -y 11.mp4

The second type: After consulting the data, it is found that ffmpeg will automatically accurate time when transcoding, that is, it does not use -codec copy encoding, so we can encode and output when cutting

-ss {0} -to {1} -accurate_seek -i {2} -c:v libx264  -avoid_negative_ts 1  -y {3}

The third type: It is said that the video intercepted by OpenCV is extremely accurate but has no sound, then use OpenCV to intercept the video, ffmpeg intercepts the audio, and then merges the audio and video

Write video using OpenCV's VideoWriter.write

Compress video to audio

ffmpeg -y -I filename -vn -ar 44100 -ac  -ab 192 -f mp3

then cut the audio

ffmpeg -y -vn -ss start -t duration -i filenam -acodec copy

finally merge audio video

ffmpeg -y -i filename -i filename2 -vcode copy -acodec copy

But I haven't tried it, so I will record it for emergencies!

Reference material: FFmpeg precise time cutting video files - know almost

FFMPEG editing Dafa - 哔哩哔哩

Guess you like

Origin blog.csdn.net/yunxiaobaobei/article/details/106529585
Recommended