Batch merge/mux .srt with .mkv files

19,549

Solution 1

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

Solution 2

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
} 

Solution 3

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.

Solution 4

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

Share:
19,549

Related videos on Youtube

Arete
Author by

Arete

Updated on September 18, 2022

Comments

  • Arete
    Arete almost 2 years

    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"
    
    • Hastur
      Hastur over 8 years
      Can you add a line with a working command line that you want to execute? It can help the answer.
  • Arete
    Arete over 8 years
    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" )