faissaloo / SponSkrub

Strip advertisements from downloaded YouTube videos
GNU General Public License v3.0
178 stars 7 forks source link

Trim video without re-encoding #32

Open bharathyes opened 3 years ago

bharathyes commented 3 years ago

Is trimming videos using ffmpeg a possibility instead of re-encoding it? Skipping chapters isn't always convenient in all devices TBH.

I was able to use these two ffmpeg commands to get desired result in bash, in case it could be adapted to DLang.

To create chunks of desired segments: ffmpeg -nostdin -i input.mp4 -ss 00:03:00 -to 00:07:00 -c copy -movflags +faststart output.mp4

Merge the segments into a single video file: ffmpeg -nostdin -f concat -safe 0 -i mylist.txt -c copy -movflags +faststart output.mp4

-nostdin for use in scripts else it errored out after trimming the first segment. -movflags +faststart I believe is how the metadata (like video resolution, codec, etc) is stored within the mp4. Was getting a warning during merge without the flag. This stores it in the beginning of file.

mylist.txt contains all segment filenames in format like:

file '/path/to/file1'
file '/path/to/file2'
file '/path/to/file3'

My script: https://gist.github.com/bharathyes/43cc91f81e8506fc47a03591025cde75

I hope this can be done in SponSkrub since it already utilizes ffmpeg. If this has already been implemented or attempted, I would be really happy.


I am unsure of the format of data received from Sponsorblock API but this method only requires the file names and the timestamps. It should be simple to mirror the timestamps from the API to select the non-sponsor segments from it.


PS:

This is magnitudes faster than re-encoding. For me it takes less than a minute to cut out an hour long file into 5 chunks and merge back again. So this could be a valuable option.

caution: There is sometimes a small stutter/frame skipping in between the merged segments almost similar to what happens on the browser and is acceptable for my purpose.

pukkandan commented 3 years ago

Related: #22 #23

faissaloo commented 3 years ago

Given how many times this has been requested I've changed my mind and intend to implement it at as an option at some point in future, with a warning indicating that it can cause stutter

bharathyes commented 3 years ago

@faissaloo I am curious as to what filters are used and the reason/benefits of them.

Is it just to avoid the stutters from trimming encoded files or is there more to it?

faissaloo commented 3 years ago

@bharathyes

@faissaloo I am curious as to what filters are used and the reason/benefits of them.

Is it just to avoid the stutters from trimming encoded files or is there more to it?

It's moreso that it was the way straightforward way I'd found of doing it. The filters used are asplit,split and concat, this is the code that handles cutting: https://github.com/faissaloo/SponSkrub/blob/3077602bff10463e5e87af2c8eb95ed285d21336/src/sponskrub/methods/cut.d#L51