-1

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

2
  • Hello, not too sure what you're trying to do with that command and why you think that's any close to solving the problem. There are bunch of googlable solutions too, e.g.: stackoverflow.com/questions/30305953/… Including a PowerShell version.
    – Destroy666
    Commented Jun 20 at 3:34
  • What do you find confusing? A video file can have multiple chapters (in concerts that would be a way to delineate each song). I want to convert a single video file with multiple chapters into multiple videos (one for each chapter.)
    – Lord Lance
    Commented 2 days ago

0

You must log in to answer this question.

Browse other questions tagged .