Here are two PowerShell scripts to split long videos into smaller chapters by black scenes .
Save them as Detect_black.ps1 and Cut_black.ps1. Download ffmpeg for Windows and tell the script the path to your ffmpeg.exe and your video folder under the option section.
Both scripts won't touch existing video files, they remain untouched.
However, you will get a couple of new files at the same place where your input videos are
- A logfile per video with the console output for both used ffmpeg commands
- A CSV file per video with all timestamps of black scenes for manual fine tuning
- A couple of new videos depending on how many black scenes are previously detected
First script to run: Detect_black.ps1
### Options __________________________________________________________________________________________________________
$ffmpeg = ".\ffmpeg.exe" # Set path to your ffmpeg.exe; Build Version: git-45581ed (2014-02-16)
$folder = ".\Videos\*" # Set path to your video folder; '\*' must be appended
$filter = @("*.mov","*.mp4") # Set which file extensions should be processed
$dur = 4 # Set the minimum detected black duration (in seconds)
$pic = 0.98 # Set the threshold for considering a picture as "black" (in percent)
$pix = 0.15 # Set the threshold for considering a pixel "black" (in luminance)
### Main Program ______________________________________________________________________________________________________
foreach ($video in dir $folder -include $filter -exclude "*_???.*" -r){
### Set path to logfile
$logfile = "$($video.FullName)_ffmpeg.log"
### analyse each video with ffmpeg and search for black scenes
& $ffmpeg -i $video -vf blackdetect=d=`"$dur`":pic_th=`"$pic`":pix_th=`"$pix`" -an -f null - 2> $logfile
### Use regex to extract timings from logfile
$report = @()
Select-String 'black_start:.*black_end:' $logfile | % {
$black = "" | Select start, end, cut
# extract start time of black scene
$start_s = $_.line -match '(?<=black_start:)\S*(?= black_end:)' | % {$matches[0]}
$start_ts = [timespan]::fromseconds($start_s)
$black.start = "{0:HH:mm:ss.fff}" -f ([datetime]$start_ts.Ticks)
# extract duration of black scene
$end_s = $_.line -match '(?<=black_end:)\S*(?= black_duration:)' | % {$matches[0]}
$end_ts = [timespan]::fromseconds($end_s)
$black.end = "{0:HH:mm:ss.fff}" -f ([datetime]$end_ts.Ticks)
# calculate cut point: black start time + black duration / 2
$cut_s = ([double]$start_s + [double]$end_s) / 2
$cut_ts = [timespan]::fromseconds($cut_s)
$black.cut = "{0:HH:mm:ss.fff}" -f ([datetime]$cut_ts.Ticks)
$report += $black
}
### Write start time, duration and the cut point for each black scene to a seperate CSV
$report | Export-Csv -path "$($video.FullName)_cutpoints.csv" –NoTypeInformation
}
How does it work
The first script iterates through all video files which matches a specified extension and doesn't match the pattern *_???.*
, since new video chapters were named <filename>_###.<ext>
and we want to exclude them.
It searches all black scenes and writes the start timestamp and black scene duration to a new CSV file named <video_name>_cutpoints.txt
It also calculates cut points as shown: cutpoint = black_start + black_duration / 2
. Later, the video gets segmented at these timestamps.
The cutpoints.txt file for your sample video would show:
start end cut
00:03:56.908 00:04:02.247 00:03:59.578
00:08:02.525 00:08:10.233 00:08:06.379
After a run, you can manipulate the cut points manually if wished. If you run the script again, all old content gets overwritten. Be careful when manually editing and save your work elsewhere.
For the sample video the ffmpeg command to detect black scenes is
$ffmpeg -i "Tape_10_3b.mp4" -vf blackdetect=d=4:pic_th=0.98:pix_th=0.15 -an -f null
There are 3 important numbers which are editable in the script's option section
d=4
means only black scenes longer than 4 seconds are detected
pic_th=0.98
is the threshold for considering a picture as "black" (in percent)
pix=0.15
sets the threshold for considering a pixel as "black" (in luminance). Since you have old VHS videos, you don't have completely black scenes in your videos. The default value 10 won't work and I had to increase the threshold slightly
If anything goes wrong, check the corresponding logfile called <video_name>__ffmpeg.log
. If the following lines are missing, increase the numbers mentioned above until you detect all black scenes:
[blackdetect @ 0286ec80]
black_start:236.908 black_end:242.247 black_duration:5.33877
Second script to run: cut_black.ps1
### Options __________________________________________________________________________________________________________
$ffmpeg = ".\ffmpeg.exe" # Set path to your ffmpeg.exe; Build Version: git-45581ed (2014-02-16)
$folder = ".\Videos\*" # Set path to your video folder; '\*' must be appended
$filter = @("*.mov","*.mp4") # Set which file extensions should be processed
### Main Program ______________________________________________________________________________________________________
foreach ($video in dir $folder -include $filter -exclude "*_???.*" -r){
### Set path to logfile
$logfile = "$($video.FullName)_ffmpeg.log"
### Read in all cutpoints from *_cutpoints.csv; concat to string e.g "00:03:23.014,00:06:32.289,..."
$cuts = ( Import-Csv "$($video.FullName)_cutpoints.csv" | % {$_.cut} ) -join ","
### put together the correct new name, "%03d" is a generic number placeholder for ffmpeg
$output = $video.directory.Fullname + "\" + $video.basename + "_%03d" + $video.extension
### use ffmpeg to split current video in parts according to their cut points
& $ffmpeg -i $video -f segment -segment_times $cuts -c copy -map 0 $output 2> $logfile
}
How does it work
The second script iterates over all video files in the same way the first script has done. It reads in only the cut timestamps from the corresponding cutpoints.txt
of a video.
Next, it puts together a suitable filename for chapter files and tells ffmpeg to segment the video. Currently the videos are sliced without re-encoding (superfast and lossless). Due to this, there might be 1-2s inaccuracy with cut point timestamps because ffmpeg can only cut at key_frames. Since we just copy and don't re-encode, we cannot insert key_frames on our own.
The command for the sample video would be
$ffmpeg -i "Tape_10_3b.mp4" -f segment -segment_times "00:03:59.578,00:08:06.379" -c copy -map 0 "Tape_10_3b_(%03d).mp4"
If anything goes wrong, have a look at the corresponding ffmpeg.log
References
Todo
Ask OP if CSV format is better than a text file as cut point file, so you can edit them with Excel a little bit easier
» Implemented
Implement a way to format timestamps as [hh]:[mm]:[ss],[milliseconds] rather than only seconds
» Implemented
Implement a ffmpeg command to create mosaik png files for each chapter
» Implemented
Elaborate if -c copy
is enough for OP's scenario or of we need to fully re-encode.
Seems like Ryan is already on it.
What a thorough answer! I appreciate it! Unfortunately, I'm on a mac right now and will need to first figure out how to translate this to Bash. – Ryan – 2013-12-28T14:44:32.733
I haven't been able to get this to work in PowerShell at all. The log files remain blank. – Ryan – 2014-02-15T20:06:04.037
I will see what I can upload to be helpful. Stay tuned. Thanks for your interest! – Ryan – 2014-02-15T22:39:51.150
I added a sample mp4 to the question. Thanks for your help! – Ryan – 2014-02-16T00:48:04.990
For privacy purposes, I won't upload the true source MOV. I'd want to chop it and remove audio (like I did for mp4). So it wouldn't be a true original source anyway. Even so, it would be way too big to upload. And the scene-splitting step should probably happen on mp4 files instead of MOV files for disk space purposes anyway. Thanks! – Ryan – 2014-02-16T16:26:45.423
Cool. When I run the first script, I get the same cutpoints as you. When I try to runt the 2nd, I get an error:
Failed to open segment 'small_test_without_audio\Tape_10_3b_without_audio_000.mp4'
I'm trying to figure out why. – Ryan – 2014-02-17T02:59:40.003I had PowerShell 2 but just upgraded to PowerShell 4 on Win7. Same problem though. Running
/c/Program\ Files/ffmpeg/bin/ffmpeg.exe -i "Tape_10_3b_without_audio.mp4" -f segment -segment_times "239.577,486.379" -reset_timestamps 1 -c copy -map 0 "Tape_10_3b_(%03d).mp4"
in Git Bash seems to work, though. So I guess I'll try to write bash script to iterate over files and dynamically figure out filenames. – Ryan – 2014-02-17T03:40:58.770Yes I'd changed the value to an absolute folder path. It only seems to work if I rearrange my files/folders and then use a relative path like
$folder = ".\videos\*"
. I think that works. Do you think it's possible for the PowerShell script to live in 1 directory while the source videos live in a different directory and the output videos are in a third directory? Specifying those 3 absolute paths would be awesome. Thanks so much for your help. I'm very excited to split decades of home movies into chapters! – Ryan – 2014-02-17T16:50:06.503your ideas in the Todo sound good to me. Thanks! I'm very excited. – Ryan – 2014-02-18T18:41:49.693
Awesome! Yes, the reason is just to decrease file size. The MOV files are 20GB+ each. My dad is bringing me the original huge MOV files next Friday. I'm almost positive that re-encoding will be necessary because the company who created them (from our original VHS tapes) said they were uncompressed. – Ryan – 2014-02-22T01:06:07.620
That seems like an interesting idea! I'd love that! – Ryan – 2014-02-23T22:32:55.247
@Ryan I deleted all of my comments (mostly small talk). You could do the same if you want to. Also, I added a second answer for your bonus request – nixda – 2014-03-07T22:10:40.813