I'm trying to find the most efficient way to cut up a video with frame precision then join the pieces together and have it play without any issues or glitches. But so far my experiments have failed and I can't figure out where I'm going wrong. First of all I understand that the only way to get the video cut precisely unless all frames are I-frames is to re-encode it and that's fine except to my surprise the cuts on the clips I tried are not accurate despite of re-encoding. Here's my code.
ffmpeg -i input.mp4 -filter_complex
"[0:v]trim=start_frame=23:end_frame=343,setpts=PTS-STARTPTS[cp1];
[0:v]trim=start_frame=2905:end_frame=3248,setpts=PTS-STARTPTS[cp2];
[cp1][cp2]concat[out]" -map [out] -c:v libx264 -crf 8 -preset ultrafast -fflags +genpts output.mkv -y
When I play this the second clip [cp2] appears like it's been cut at least one frame earlier resulting in an unwanted flash. So for my second test I tried something simpler. First I opened the input file in MPC to get timing info for frames of cp2 start_frame=2905 is 00:02:01.162 and end_frame=3248 is 00:02:15.512. So I tried the following.
ffmpeg -i input.mp4 -ss 00:02:01.162 -to 00:02:15.512 -c:v libx264 -crf 8 -preset ultrafast -fflags +genpts output.mkv -y
The result was exactly the same with cut happening earlier than specified. Note: the -to time is actually the end of the clip. Finally someone has suggested to force key frames by specifying your cuts time stamps first like:
ffmpeg -i input.mp4 -force_key_frames 00:02:01.162,00:02:15.512 output.mp4
Then take the output.mp4 and feed it as input to the code where I use the filter_complex but unfortunately this returned the same results.
I'm still very new to ffmpeg. What can I try next?
question from:
https://stackoverflow.com/questions/65838020/best-way-to-precisely-trim-and-concatenate-with-ffmpeg 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…