Breakthrough / PySceneDetect

:movie_camera: Python and OpenCV-based scene cut/transition detection program & library.
https://www.scenedetect.com/
BSD 3-Clause "New" or "Revised" License
2.97k stars 374 forks source link

ffmpeg segment_times options #387

Open mo-han opened 3 months ago

mo-han commented 3 months ago

Problem/Use Case splitting without transcoding results in segments overlaping at begining and ending.

Solutions

ffmpeg segment_times option

segment_times times

    Specify a list of split points. times contains a list of comma separated duration specifications, in increasing order. See also the segment_time option.

Proposed Implementation:

scenedetect just provide those split points, and let ffmpeg do the segment job.

Alternatives:

List any alternative solutions or related ideas you've considered.

Examples:

Attach or link to any relevant videos or images that are relevant.

Breakthrough commented 3 months ago

Without transcoding, isn't this the same as running ffmpeg several times with -c:v copy -c:a copy?

That being said I am curious if this might be a possible cause of #159

mo-han commented 3 months ago

I've no idea about #159


running ffmpeg several times with -c:v copy -c:a copy

I assume you are refering to extracting one segment. IIRC it's different from segment.

Seeking while doing a codec copy

Using -ss as input/output option together with -c:v copy might not be accurate since ffmpeg is forced to only use/split on i-frames. Though it will—if possible—adjust the start time of the stream to a negative value to compensate for that. Basically, if you specify "second 157" and there is no key frame until second 159, it will include two seconds of audio (with no video) at the start, then will start from the first key frame. So be careful when splitting and doing codec copy.

https://stackoverflow.com/questions/43814119/split-a-video-with-overlap-between-segments

FFmpeg has a segment muxer which can segment the file in one command but it does not create overlapping segments so not suitable here.

I'm a noob about ffmpeg, since it's so complicated. But I'm sure the segment muxer won't output overlapping segments.

Breakthrough commented 3 months ago

I'm a noob about ffmpeg, since it's so complicated. But I'm sure the segment muxer won't output overlapping segments.

Likewise, could definitely use some guidance on the proper way to go about this. That being said, I think you can use list-scenes -n in your command, and the last line of the output should be the list of split points you need. It should be possible to use that as the arg to ffmpeg segments.

Can you try that, and let me know if that works? And if so, what command line arguments you are using?

I might opt to not switch how codec-copy mode works as it would break how output filenames are formatted. PySceneDetect supports much more formatting options for filenames than ffmpeg currently supports. That being said, it would be pretty easy to add or hack the code as this is similar to how mkvmerge splitting works. If the above works, I can look into adding a config file option for this.

mo-han commented 3 months ago

Can you try that, and let me know if that works? And if so, what command line arguments you are using?

I'd love to, but not now, maybe a fews days later.


What do you think about this:

If you cut with stream copy (-c copy) you need to use the -avoid_negative_ts 1 option if you want to use that segment with the ​concat demuxer .

(https://trac.ffmpeg.org/wiki/Seeking#Cuttingsmallsections)