Skip to content

Instantly share code, notes, and snippets.

@gabbsmo
Last active December 16, 2015 05:58
Show Gist options
  • Save gabbsmo/5387773 to your computer and use it in GitHub Desktop.
Save gabbsmo/5387773 to your computer and use it in GitHub Desktop.
Burn in srt subtitles into a mkv-file
#COPYRIGHT: Gabriel Smoljar (2012)
#WEBSITE: http://twitter.com/gabbsmo
#DESCRIPTION: This PowerShell script process all mkv- and srt-files in a directory by
# first converting the srt subtitles to ssa and then muxing them with their
# corresponding mkv container in a tmp directory. The muxed mkv-files are then
# reencoded with the subtitles burned in.
#
#NOTES: HandbrakeCLI, FFMPEG and MKVMerge must be added to PATH for this script to work.
# Apply all parameters according to the HandbrakeCLI docs: https://trac.handbrake.fr/wiki/CLIGuide
#DEPENDENCIES: HanbrakeCLI, http://handbrake.fr; FFMPEG, http://www.ffmpeg.org;
# MKVMerge (included in MKVToolNix) http://www.bunkus.org/videotools/mkvtoolnix/
Param(
[parameter(Mandatory=$true)]
[alias("i")]
$InputDir,
[parameter(Mandatory=$true)]
[alias("o")]
$OutputDir,)
$items = Get-ChildItem -Path $InputDir
#Convert SRT subs to SSA
foreach ($item in $items)
{
if ($items.Attributes -ne "Directory")
{
$InputFile = $InputDir + "\" + $item.name
$OutputFile = $InputDir + "\" + $item.BaseName + ".ssa"
if ($item.Extension -ieq ".srt")
{
ffmpeg -i $InputFile $OutputFile
}
}
}
#Mux SSA into MKV
foreach ($item in $items)
{
if ($items.Attributes -ne "Directory")
{
$InputFile = $InputDir + "\" + $item.name
$SubtitleFile = $InputDir + "\" +$item.BaseName + ".ssa"
$OutputFile = $OutputDir + "\tmp\" + $item.BaseName + ".mkv"
if ($item.Extension -ieq ".mkv")
{
mkvmerge -o $OutputFile $InputFile --language "0:eng" $SubtitleFile -s "0" -D -A
}
}
}
#Burn subs
$TmpDir = $OutputDir + "\tmp"
$tmp = Get-ChildItem -Path $TmpDir
foreach ($item in $tmp)
{
if ($tmp.Attributes -ne "Directory")
{
$InputFile = $TmpDir + "\" + $item.name
$OutputFile = $OutputDir + "\" + $item.BaseName + " - SD.m4v"
Write-Host $InputFile
if ($item.Extension -ieq ".mkv")
{
handbrakecli --preset "Normal" --input $InputFile --output $OutputFile --audio "1" --mixdown "stereo" --width "720" --loose-anamorphic --subtitle "1" --subtitle-burned "1"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment