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;
}
@jwallet
Copy link
Author

jwallet commented Mar 8, 2019

Functions that collect recursively all videos in a folder to set them to mkv with a new language (default track (first track) only)

PLEX

Use it for your PLEX server too, so all your tv shows and movies will have more data

Requirements

  • mkvtoolnix installed (to program files (x86) path (see the script))

Set the script file

  1. Go to your Documents\WindowsPowerShell and paste this file there.
    • or Open Powershell and type notepad $PROFILE, then copy paste the content and save.
  2. Open Powershell and type Set-ExecutionPolicy RemoteSigned -Scope CurrentUser to enable custom script

Functions

  • Run this one to set only the language for your .mkv files
    mkv-set-language mkv-set-language eng (english: eng (default), spanish: spa, german: ger, fre: french, ...)
    test

  • Run this one to copy and convert your .avi and .mp4 to .mkv and then set the default language:
    to-mkv to-mkv eng
    test2

Before using it

Change in the functions the paths related to mkvtoolnix tools to yours

Warnings

  • THESE FUNCTIONS ARE RECURSIVE, SO IT WILL GET ALL FILES AND ITS CHILD IN A FOLDER
  • TRY THOSE FUNCTIONS WITH ONE FILE FIRST, ISOLATE IT IN A SINGLE FOLDER

Troubleshoothing

  • If you get a warning that your file could not be opened for reading: open file error. that means the function failed to switch the readonly property to false you will have to run the terminal as an administrator or make the change yourself before running the function

Know which plex files that don't have the language set yet

looping

Feel free to edit the functions as you wish

@UnhealthyKraken
Copy link

Any chance to make it so it only changes the language on files that report back as "und"?

@jartas
Copy link

jartas commented Feb 14, 2022

Hello, thx for this PS, if possible help me with how get func change track name. Thank you

@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