I made a script in pwsh to split video files into separate files for each chapter:
Get-ChildItem -Recurse -Include *.mp4, *.mkv, *.avi, *.mov, *.ts | ForEach-Object {
$ffmpegOutput = & ffmpeg -i $_.FullName -hide_banner 2>&1
$chapterCount = ($ffmpegOutput -split "`n" | Where-Object { $_ -match "Chapter \d+" }).Count
if ($chapterCount -gt 1) {
Write-Output "File: $($_.FullName) has $chapterCount chapters."
for ($i = 0; $i -lt $chapterCount; $i++) {
$parentFolder = Split-Path $_.Directory.Name -Leaf
$paddedCounter = "{0:D2}" -f ($i + 1)
$newFileName = Join-Path -Path $outputFolderPath -ChildPath "$paddedCounter. $parentFolder - Chapter $i.mp4"
& ffmpeg -i $_.FullName -map 0 -map "-0:v:$i" -c copy $newFileName
}
}
}
}
I don't think most of the script matters for troubleshooting except this line:
& ffmpeg -i $_.FullName -map 0 -map "-0:v:$i" -c copy $newFileName
For some reason, the number of video files created correspond to the number of chapters in the video but they are all the entire video instead of corresponding to each chapter and I don't know what I am doing wrong.
Terminal log of the command: https://pastebin.com/M2rFZLtS