Every time a .mkv file is needed to be muxed it comes with random amount of subtitles, and each subtitle is different language code.
So e.g. Scenario 1:
video.mkv
eng.srt
fre.srt
ger.srt
Scenario 2:
video.mkv
rus.srt
spa.srt
The goal is to everytime running the same batch file make it pick it up amount of subtitles and mux it correctly with correct language codes, etc.
So far we got here:
EnableDelayedExpansion
for %%A in ("*.srt") do (
set subtitle=%%~nA
)
for %%A in ("*.mkv") do (
mkvmerge.exe -o "%%~nA.mkv" --default-track 0:yes "%%A"^
--language 0:!subtitle! --default-track 0:no "!subtitle!.srt"^
--language 0:!subtitle! --default-track 0:no "!subtitle!.srt"^
--language 0:!subtitle! --default-track 0:no "!subtitle!.srt"^
)
So far this is fixed amount of subtitles approach, not dynamic, so e.g. scenario 1. It almost works except it picks up every subtitle as last variable, so from scenario 1 each subtitle is muxed three times and named as ger
for language and ger.srt
used as source all three times because it's last one in the list by alphabet.
Goal: Each !subtitle!
should be correct language and subtitle, as I understand by alphabet.
I don't have installed mkvmerge.exe
and I don't have read the documentation of mkvmerge and so don't know if the options provided in question are really correct.
The batch file below creates the command line with variable number of --language
and --default-track
options depending on .srt
files found in current directory.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "SubtitleOptions="
setlocal EnableDelayedExpansion
for %%I in (*.srt) do set "SubtitleOptions=!SubtitleOptions! --language 0:%%~nI --default-track 0:no "%%I""
endlocal & set "SubtitleOptions=%SubtitleOptions%"
for %%I in (*.mkv) do mkvmerge.exe -o "%%I" --default-track 0:yes "%%I"%SubtitleOptions%
endlocal
Please read this answer for details about the commands SETLOCAL and ENDLOCAL and single line with multiple commands using Windows batch file for an explanation of operator &
used on command line with ENDLOCAL to set the variable SubtitleOptions
in initial environment of the batch file with delayed expansion not enable to process correct also .mkv
files with !
in file name.
The string assigned to environment variable SubtitleOptions
starts always with a space character if defined at all which is the reason why %SubtitleOptions%
is directly after "%%I"
on command line executing mkvmerge.exe
.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
endlocal /?
for /?
set /?
setlocal /?