Created
December 31, 2025 04:19
-
-
Save david-jarman/dcf1d9f925be63700999085a4d10b6ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # PowerShell script to split MKV file by chapters using ffmpeg and ffprobe | |
| param( | |
| [string]$InputFile, | |
| [string]$OutputPrefix = "Chapter" | |
| ) | |
| # Get chapter information from the file in JSON format | |
| # ffprobe extracts metadata; we're specifically requesting chapter information | |
| $chapterJson = ffprobe -v quiet -print_format json -show_chapters $InputFile | ConvertFrom-Json | |
| # Check if chapters were found | |
| if ($null -eq $chapterJson.chapters -or $chapterJson.chapters.Count -eq 0) { | |
| Write-Host "No chapters found in the file!" | |
| exit | |
| } | |
| Write-Host "Found $($chapterJson.chapters.Count) chapters. Starting extraction..." | |
| $chapterNum = 1 | |
| # Loop through each chapter | |
| foreach ($chapter in $chapterJson.chapters) { | |
| # Format chapter number with leading zeros (e.g., 01, 02, 03) | |
| $chapterNumFormatted = "{0:D2}" -f $chapterNum | |
| # Get start and end times for this chapter | |
| $startTime = $chapter.start_time | |
| $endTime = $chapter.end_time | |
| # Create output filename | |
| $outputFile = "$($OutputPrefix)_$($chapterNumFormatted).mkv" | |
| # Display progress | |
| Write-Host "Extracting Chapter $chapterNum ($startTime to $endTime) -> $outputFile" | |
| # Run ffmpeg to extract this chapter | |
| # -i: input file | |
| # -c copy: copy streams without re-encoding (fast!) | |
| # -ss: start time | |
| # -to: end time | |
| ffmpeg -i $InputFile -c copy -ss $startTime -to $endTime $outputFile | |
| $chapterNum += 1 | |
| } | |
| Write-Host "Done! Extracted $($chapterJson.chapters.Count) chapters." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment