5

I have a folder with:

  • TVShow - Episode 01.mkv
  • TVShow - Episode 01.srt
  • TVShow - Episode 02.mkv
  • TVShow - Episode 02.srt
  • TVShow - Episode 03.mkv
  • TVShow - Episode 03.srt

and so on...

I already know that I can do this with MKVMerge one by one. This would take a considerable amount of time so... I want to make a batch script that can do this for all the files in the folder with MKVToolNix, mkvmerge or similar.

I also need the batch script to be relative so that I can apply the script at a later use when the file names are different.The file name might not allways be "TVShow" but e.g "Second TV Show".

1. I want to mux the external subtitle file (srt) into the respective movie file (mkv).

2. I also want to make the subtitle the default and forced subtitle of the respective movie file.

How can I make a batch script like this? I have some knowledge of batch scripting and I have googled this for a couple of hours without finding any useful info.

I started out with this but it does not work and also it does not set the subtitles to default or forced:

"B:\OneDrive\Portable applications\mkvtoolnix\mkvmerge.exe" "FOR %%A IN (*.mkv) DO mkvmerge -o "remux-%%~nA.mkv" "%%~A" "%%~dpnA.srt"
1
  • Can you add a line with a working command line that you want to execute? It can help the answer.
    – Hastur
    Feb 4, 2016 at 11:32

6 Answers 6

6
for %%A IN (*.mkv) do (
"B:\OneDrive\Portable applications\mkvtoolnix\mkvmerge.exe" -o "remux-%%~nxA" "%%~A" "%%~nA.srt"
echo  Replace this Line with your othr commands!
)

And see mkvpropedit -- Modify properties of existing Matroska and Change Default Language/Subtitles in MKV Files

1
  • 3
    Thanks. This works. However it does not make the subtitle forced. With your help I added some properties. for %%A IN (*.mkv) do ( "B:\OneDrive\Portable applications\mkvtoolnix\mkvmerge.exe" -o "remux-%%~nxA" "%%~A" --forced-track "0:yes" --default-track "0:yes" --track-name "0:English" --language "0:eng" "%%~nA.srt" )
    – Arete
    Feb 4, 2016 at 12:03
1

I find a good example in another answer[1], use the general sketch below:

for %%f in (*.mkv) do (
        echo %%~nf
         mkvmerge -o "%%~nf_New.mkv" "%%~nf_New.mkv" --language 0:eng "%%~nf.srt"

)

The example more similar I found on the mkvmerge site [2]

mkvmerge -o with-lang-codes.mkv --language 2:ger --language 3:dut \
--default-track 3 without-lang-codes.mkv --language 0:eng english.srt \
--default-track 0 --language 0:fre french.srt

It's enough that you put inside the for cycle, the command line that works in your single case, changing the mkv file with %%~nf.mkv and the srt with %%~nf.srt and all the options in the correct place.

1

I had the same problem as yourself and put together the below PowerShell Script to achieve the batch muxing of subtitles.

#Batch Merge Subtitles with MKVMerge - Iain McCain

#Set MKVMerge.exe Path
$MKVMerge = 'C:\Program Files\MKVToolNix\mkvmerge.exe'
#Set Target
$Directory = "Z:\Films\"
#Set Subtitle Extension
$SubExtension = 'eng.srt'

#Process
$Subs = Get-ChildItem $Directory -Filter "*.$SubExtension" -Recurse | % { $_.FullName } | Sort-Object
$Count = $Subs.count
Write-Host "$Count MKV's to be processed."

Foreach ($Sub in $Subs) 
{
#Get File Name
$FormatName = $Sub.ToString()
$Name = $FormatName.TrimEnd(".$SubExtension")
$MKV = $Name + '.mkv'

#Set Output File Name
$Output = $Name + '___MERGED' + '.mkv'

#Execute
& $MKVMerge -o "$Output" --default-track "0" --language "0:eng" "$Sub" "$MKV"

#Clean Up
Remove-Item $MKV
Remove-Item $Sub
Rename-Item $Output -NewName $MKV
} 
1

Old post, but in case others are looking, I created a program that leverages MKVToolNix and is able to do what's being asked and process entire directories in a single batch.

https://github.com/iPzard/mkvtoolnix-batch-tool

MKVToolNix Batch Tool

0

All credit to Ian McCain above for this edit of their code.

TrimEnd is letter by letter instead of strings, so any file ending with a letter in the extension name will cause failures. Because the script doesn't check for errors, the sub and mkv files are deleted even if errors occur that prevented the muxed one from being created. This can cause loss of data.

I needed to use the script to mux idx/sub files into my MKVs instead of srt files, and they are named the same as the video file with a .sub and .idx extension instead of .mkv. I also added error checking so no files are deleted/renamed if the ___MERGED file isn't created due to some error. This is my completed solution.

    #Batch Merge Subtitles with MKVMerge - Iain McCain

#Set MKVMerge.exe Path
$MKVMerge = 'C:\Program Files\MKVToolNix\mkvmerge.exe'
#Set Target
$Directory = "Z:\Movies\"
#Set Subtitle Extension
$SubExtension = 'idx'
$SecondSubExtension = "sub"

#Process
$Subs = Get-ChildItem $Directory -Filter "*.$SubExtension" -Recurse | % { $_.FullName } | Sort-Object
$Count = $Subs.count
Write-Host "$Count MKV's to be processed."

Foreach ($Sub in $Subs) {
    #Get File Name
    $FormatName = $Sub.ToString()
    #$Name = $FormatName.TrimEnd(".$SubExtension")
    $Name = $FormatName.Substring(0,$FormatName.Length-($SubExtension.Length+1))
    $MKV = $Name + '.mkv'
    $OtherSub = $Name + '.' + $SecondSubExtension

    #Set Output File Name
    $Output = $Name + '___MERGED' + '.mkv'

    #Execute
    & $MKVMerge -o "$Output" --default-track "0" --language "0:eng" "$Sub" "$MKV"

    If (Test-Path $Output) {
        #Clean Up
        Remove-Item $MKV
        Remove-Item $Sub
        Remove-Item $OtherSub
        Rename-Item $Output -NewName $MKV
    } Else {
        write-host "NON-EXISTANT - $Output" -foreground "red"
        "NON-EXISTANT - $Output" | Out-File $Directory + "Errors.txt" -Append
    }
}
0

Many thanks to @Ian, @Hastur and others. Here is a script which corrects the mistakes in the others posted here, and moves the modified file back into the original filename. (replace "path" with the path to the executable)

for %%f in (*.mkv) do (
    echo %%~nf
    <path>mkvmerge.exe -o "%%~nf_New.mkv" "%%~nf.mkv" --language 0:eng "%%~nf.srt"
    move /Y "%%~nf_New.mkv"  "%%~nf.mkv"

)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .