Posted by Dave Thijssen Thursday, 18 September 2014

Say, you have about 160 mkv files with a 160 seperate subtitles. Matroska is a container format which can hold multiple content such as video, chapters, subtitles and audio tracks. So it's much neater to have a single file for each film or episode.

Doing this manually, even using the mkvmerge job queue, is mind numbing and can take hours. Here's a relatively quick way of automatizing this.

Old situation


New situation




Prerequisite

Download and install MKVToolNix.

Part 1


  1. Load the first mkv file and the srt file into mkvmerge GUI.
  2. Set the options as desired.
  3. Click Muxing.
  4. Click Show the command line.
  5. Modify the $mkvmergecommand variable in the PowerShell script below to math the parameters in the Current command line.


This is the command line you need to run in bulk with slightly different parameters, being the input and output files.


Part 2


  1. Modify the variables at the top of the script to reflect your situation.
  2. Run the script.

$directory = "M:\Downloads\Prison Break"
$mkvmergecommand = [String]"""""C:\Program Files\MKVToolNix\mkvmerge.exe"" -o ""%outputfile%""  ""--forced-track"" ""0:no"" ""-s"" ""0"" ""-D"" ""-A"" ""-T"" ""--no-global-tags"" ""--no-chapters"" ""("" ""%srtinputfile%"" "")"" ""--language"" ""0:eng"" ""--default-track"" ""0:yes"" ""--forced-track"" ""0:no"" ""--display-dimensions"" ""0:1280x720"" ""--language"" ""1:eng"" ""--default-track"" ""1:yes"" ""--forced-track"" ""1:no"" ""-a"" ""1"" ""-d"" ""0"" ""-S"" ""-T"" ""--no-global-tags"" ""--no-chapters"" ""("" ""%mkvinputfile%"" "")"" ""--track-order"" ""0:0,1:0,1:1"""""
$files = Get-ChildItem $directory -Filter "*.mkv" | Sort Name
Foreach ($file in $files) {
    $mkvinputfile = ($directory -replace "\\", "\\") + "\\" + ($file.Name)
    $srtinputfile = ($directory -replace "\\", "\\") + "\\" +($file.Name -replace ".mkv", ".srt")
    $outputfile =  ($directory -replace "\\", "\\") + "\\" +($file.Name -replace ".mkv", "Merged.mkv")
    $mergecommand = (($mkvmergecommand -replace "%outputfile%", $outputfile) -replace "%mkvinputfile%", $mkvinputfile) -replace "%srtinputfile%", $srtinputfile
    cmd /c $mergecommand
}

Download MergeMKVSRT.ps1.

3 comments... read them below or respond.

  1. Not working for me :(
    Powershell kicking out errors

    ReplyDelete
    Replies
    1. Try this: https://1drv.ms/u/s!AjBPRo9wXw9s2eg3AoRAwm2KGxJkwg

      To be copied to the folder containing the input files; you might need to tweak the part of the code that derives the filename of the .SRT from the .AVI's filename

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete

Copyright Dave Thijssen. Powered by Blogger.