Skip to content

Instantly share code, notes, and snippets.

@DessertArbiter
Last active December 5, 2021 05:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DessertArbiter/870fa17afeb4e63941cb1403f5e90a25 to your computer and use it in GitHub Desktop.
Save DessertArbiter/870fa17afeb4e63941cb1403f5e90a25 to your computer and use it in GitHub Desktop.
PowerShell script to run mkvmerge on all mkv and mp4 files in a directory, supports all mkvmerge parameters. Creates new mkv files in "mkvmerge" subdirectory of current folder without overwriting or deleting the originals.
$parameters = Read-Host "Enter parameters, leave blank for defaults"
$parameterArray = $parameters.Split(" ")
Write-Host "For single subtitle, base name must match video file name (without extension).`nIf files have multiple external subtitles, they must be named like so: videoFileName_Track[0 - 99].[ext]"
$numSubs = Read-Host "Number of external subtitles to merge? (0 for none)"
if ($numSubs -gt 0) {
Write-Host "External subtitles:`n`tass,pgs,vobsub,srt,etc."
$subsExt = Read-Host "Subtitle file type?"
}
$subsName = @()
Get-ChildItem -Path '*.mkv', '*.mp4' | ForEach-Object {
Write-Verbose "Processing file: $_"
$mkvFullName = [System.IO.Path]::ChangeExtension($_.FullName, "mkv")
$mkvFileName = [System.IO.Path]::GetFileName($mkvFullName)
$mkvBaseNameNoExt = [System.IO.Path]::GetFileNameWithoutExtension($_.FullName)
if ($numSubs -gt 1) {
for ($i = 0; $i -lt $numSubs; $i++) {
$subsName = $subsName + "$mkvBaseNameNoExt`_Track$i.$subsExt"
}
}
elseif ($numSubs -eq 1) {
$subsName = "$mkvBaseNameNoExt.$subsExt"
}
& mkvmerge -o ".\mkvmerge\$mkvFileName" $parameterArray $_.FullName $subsName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment