slhck / ffmpeg-black-split

Split a video based on black periods
Other
11 stars 2 forks source link

Selecting or ignoring periods #2

Open Unshackle8078 opened 1 month ago

Unshackle8078 commented 1 month ago

Sometimes even finely tuning the black detect filter results in more periods detected than desired. The most common usage is splitting one episode in two but if for example there are three periods detected, that would be too many splits.

There needs to be some bias for selecting the one most middle-oriented period to cut, or in the cases of three part episodes, two third-size slices.

I can see clearly that one out of the three periods is the correct cut spot to be in the middle but I have to rejoin the extra cuts.

Short of going with a bias of some sort, being able to pass a parameter in or an edited black detect list would be ideal so I can remove the extraneous periods programmatically and the remaining list would get passed into ffbs.cut_all_periods(outputDir, False, True, blackPeriodsList) for example.

slhck commented 1 month ago

This is now possible since v0.5.0 and commit c92688fae9e4d0a5d3762cf3a0da0beec82b0ad0.

The main changes to the FfmpegBlackSplit class API are:

  1. There is a filter_black_periods method that implements a heuristic based on the number of splits. It takes the list of black periods as its first argument and returns the filtered list.
  2. The cut_all_periods method now accepts an optional filtered_black_periods parameter, for which you can use the output of the former method.

Black period detection remains the same:

fbs = FfmpegBlackSplit("input_video.mp4")
black_periods = fbs.detect_black_periods()

Filtering black periods:

# For a single cut (e.g., splitting an episode in two)
filtered_periods = FfmpegBlackSplit.filter_black_periods(black_periods, num_cuts=1)

# For two cuts (e.g., splitting into three parts)
filtered_periods = FfpmegBlackSplit.filter_black_periods(black_periods, num_cuts=2)
  1. Cutting the video using filtered periods:
fbs.cut_all_periods(
    output_directory="output_dir",
    no_copy=True,
    filtered_black_periods=filtered_periods
)

Here, the filtered_black_periods override whatever was determined before.

I hope the new API allows for more flexibility. Let me know how that works for you.

Unshackle8078 commented 1 month ago

Thanks this works perfectly and solves the problem!