[FFmpeg-user] Find the correct video cut point based on time into the file?

Bo Berglund bo.berglund at gmail.com
Thu May 18 19:42:33 EEST 2023

Excuse me if this is a longish post, but I wanted to supply all the info I have
at this point...

I have written a video player application (in FreePascal if you are curious),
which I use to find edit points to use in ffmpeg calls to extract sections from
a video.
With the application I can play the video in 50-200 % speed and I can jump by an
entered time in seconds back and forward. And I can halt the video at certain

The application allows me to define a start and end time of a cut by setting a
marker in the two places.

These times are later used to compose a set of ffmpeg commands which:
1) Extracts all of the cuts based on the time in seconds
2) Then concatenates these back into a new shorter video file

The source videos are created by an ffmpeg download from a video stream using
libx264 and aac for video and audio respectively into mp4 format in 480p size.

This works fairly well except I have noted that there are glitches in the final
video where two cuts are joined together. Sometimes at that time video stops and
audio continues for 1-2 seconds or so. Sometimes the start of the video displays
the image from before the cut point silently and then it starts running OK.

So when discussing this matter on a different forum concerned with software
development someone suggested that the points where the cuts are made should not
be willy-nilly anywhere, instead they should happen at some internal points
where the video itself starts a new "frame" (I am not familiar with the correct
terminology here).
Apparently the video file contains some kind of "frames" lasting short or long
times where there is a starting blob of the image and then differences only, or
something like this.

So if one cuts the video file in the middle of such a section and concatenates
to another similarly cut video section then problems occur...

The person suggested thath there might be ways to adjust the cut points slightly
such that they happen on the internal border between these "frames".

Hence I ask here if there is a way using ffmpeg to find the exact point where
the video file starts a frame which is closest to the given cut point in

For each extraction it shall do it sends this command:

ffmpeg -hide_banner -i inputfile.mp4 -ss 138 -t 519 -c:v copy -c:a copy

Obviously the ss and t times vary with the cut as well as the output file name.

Once this has been done for all of the wanted cuts it creates a joinfile file
listing all the cut files in the format:
file cut_1.mp4
file cut_2.mp4

Then it is fed to the ffmpeg concat command:

ffmpeg -hide_banner -f concat -i joinfile -c copy finaloutputfile.mp4

What I want to check here is if there is a way to initially when the cut points
are set ask ffmpeg to supply the exact time for the closest "frame" start in the

Right now they are just manually set by me when viewing the video at possibly
0.5 s accuracy or so and truncated to the closest second.

I am using the public API of VLC to run the video display and read off the
current playing time of the video.

And, I have tried to instead re-encode the cuts from the source into a new full
video and this makes these artifacts at the borders go away, BUT this process
takes between 5-10 minutes for each video whereas the concat only takes a few

Any suggestions on how to find the closest frame border time in say milliseconds
from a given time in integer seconds??

Bo Berglund
Developer in Sweden

More information about the ffmpeg-user mailing list