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

Bo Berglund bo.berglund at gmail.com
Sun May 21 11:33:47 EEST 2023


On Thu, 18 May 2023 18:42:33 +0200, Bo Berglund <bo.berglund at gmail.com> wrote:

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

See below for the looks of the script!

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

What he refers to is what is called segments in ffmpeg...
He suggests this:

>> Unless someone else comes up with a better suggestion, I think you can
>> probably achieve this with -ss, -segment and -segment_time as it only 
>> allows cuts on a keyframe and (AFAICT) always continues for a time period 
>> that ensures complete GOPs.
>> 
>> My guess would be that it would take your start time (-ss) and duration 
>> (t , where t is parsed to -segment_time instead) then round them to the 
>> nearest downstream keyframe and complete GOP, so you won't get the precise 
>> timings you want, but it should produce technically correct segments as 
>> close to those times as possible.
>> 
>> To stop it truncating the start of your chosen section, you could make 
>> it round to the nearest previous GOP by subtracting one intra period from 
>> -ss before parsing it to ffmpeg.
>> 
>> To improve the timing precision, you could put keyframes closer together 
>> when capturing by lowering --intra.

To check this I have now downloaded the very big ffmpeg book from here:
http://www.astro-electronic.de/FFmpeg_Book.pdf

In that I searced for some reference to "intra" and the closest I get is on
section 2.59 page 113 where they discuss some processing step for this, but when
I try to split a downloaded video like the example it returns an error:

$ ffmpeg -i input.mp4 -f segment -segment_time 2 -c copy output.mp4

[mp4 @ 0x55b1cbae3580] Invalid segment filename template 'ouput.mp4'
Could not write header for output file #0 (incorrect codec parameters ?):
Invalid argument
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
    Last message repeated 1 times

I don't understand the message at all....


>
>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
>seconds???
>
>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
>cut_x.mp4
>
>Obviously the ss and t times vary with the cut as well as the output file name.
>

What I think could be possible is if the stream *download command* itself could
be modified such that it will produce an output video where the sements all
start at even seconds, then the video will be OK if I cut it at even seconds
when editing, right?

But how should I modify my download command to accomplish this?

My working download script essentially uses this command to get the stream into
an mp4 file on the Ubuntu server:

ffmpeg -hide_banner -referer \"${VIDEOURL}\" -i \"${M3U8URL}\" -vf \
scale=w=-4:h=480 -c:v libx264 -preset fast -crf 26 -c:a aac \
-t 3600 output.mp4

How can it be modified such that the segments start at integer seconds?

And then how can I adjust the editing script that cuts the sections I need such 
that the cuts will be on the segment start times?
(If the segments are longer than 1 second my cut request could be in the middle
of such a segment anyway...)

Any ideas?


-- 
Bo Berglund
Developer in Sweden



More information about the ffmpeg-user mailing list