Skip to content

Instantly share code, notes, and snippets.

@jwallet
Last active December 9, 2023 00:46
Show Gist options
  • Save jwallet/52834b7a1e293e481ebe9ab1870f25b3 to your computer and use it in GitHub Desktop.
Save jwallet/52834b7a1e293e481ebe9ab1870f25b3 to your computer and use it in GitHub Desktop.
Windows Powershell functions to convert video files with mkvmerge from mp4 and avi to mkv and set all mkv default audio and video language to your desired language with mkvpropedit all in batch to be used for your PLEX server or something else.
# Credit: https://www.github.com/jwallet
function mkv-set-language([string]$lang="eng") {
Get-ChildItem -Path "." -Name -Recurse -File -Include *.mkv | ForEach { echo ">>> Getting file: $_"; Set-ItemProperty $_ -name IsReadOnly -value $false | & 'C:\Program Files (x86)\MKVToolNix\mkvpropedit.exe' --tags all:'' --delete title --edit track:v1 --set language=$lang --edit track:a1 --set language=$lang "$_" }
}
function to-mkv([string]$lang="eng") {
Get-ChildItem -Path "." -Name -Recurse -File -Include *.avi,*.mp4 | ForEach { echo ">>> Getting file: $_"; $_ -match '(?<filename>.*)[.]'; $output = $matches["filename"] + ".mkv"; Set-ItemProperty $_ -name IsReadOnly -value $false | & 'C:\Program Files (x86)\MKVToolNix\mkvmerge.exe' -o $output --default-language $lang "$_" }; mkv-set-language $lang;
}
@MojoCreator
Copy link

Here is the script if you want to do the same thing but leverage the Handbrake CLI. Open Powershell as Administrator then run the script with PowerShell.exe -ExecutionPolicy Bypass -File C:\Users\Username\Documents\WindowsPowerShell\Microsoft.PowerShell_HandBrake_profile.ps1

`$sourcePath = "C:\Video-File-Directory"
$handbrakePath = "C:\Program Files\Handbrake\tools\HandBrakeCLI.exe"

$videofiles = Get-ChildItem $sourcePath -Filter *.mp4 -Recurse
$filecount = $videofiles.Count
$i = 0

ForEach ($file in $videofiles) {
$i++
$origfile = $file.FullName
$newfile = $file.DirectoryName + "" + $file.BaseName + ".mkv"

$progress = ($i / $filecount) * 100
$progress = [Math]::Round($progress, 2)

Clear-Host
Write-Host "-------------------------------------------------------------------------------"
Write-Host "Handbrake Batch Encoding"
Write-Host "Processing - $origfile"
Write-Host "File $i of $filecount - $progress%"
Write-Host "-------------------------------------------------------------------------------"

$args = "-i `"$origfile`" -t 1 -o `"$newfile`" -f mkv -O --decomb -e x264 -q 22 --pfr -a 1 -E av_aac -B 160 -R Auto --x264-preset=fast --x264-profile=main --h264-level=`"4.0`" --verbose=0"
Start-Process $handbrakePath -ArgumentList $args -Wait -NoNewWindow

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment