I was wondering how I can batch merge many mp4 video files with several subtitles into a single mkv?
namevideo01.mp4 [This mp4 has 1 video + 1 audio]
namevideo02.mp4 [This mp4 has 1 video + 1 audio]
I could use this:
FOR %%A IN (*.mp4) DO "C:\Program Files\MKVToolNix\mkvmerge.exe" -o "%%~nA.mkv" "%%~A" "%%~nA.en-US.cc.srt" "%%~nA.es-419.forced.srt" "%%~nA.es-419.srt"
but I'd like something more generic, something that will pick up as many subtitles as possible. Also I'd like to get the right name, like English / Spanish (Forced) / Spanish, and make the forced subs activated by default.
Any help would be appreciated. Thanks
+ Reply to Thread
Results 1 to 5 of 5
metafox doesn't rename the tracks correctly. Only creates track 1 track 2 and track 3, it doesn't pick up the language named in the file name.
Perhaps you could use a batch script to create the mkvmerge scripts themselves. Not sure how to do it precisely, but I've seen such suggestions in the past (from master “jagabo” if I'm not mistaken — indeed, here).
Something like :
FOR %%A in (*.mp4) DO ( echo FOR %%%%A IN (*.mp4) DO ^"C:^\Program Files^\MKVToolNix^\mkvmerge.exe^" -o ^"%%%%~nA.mkv^" ^"%%%%~A" >"%%~nA.bat" IF EXIST "%%~nA.en-US.srt" echo ^"%%%%~nB.en-US.cc.srt^"" >>"%%~nA.bat" IF EXIST "%%~nA.es-419.forced.srt" echo ^"%%%%~nB.es-419.forced.srt"" >>"%%~nA.bat" IF EXIST "%%~nA.es-419.srt" echo " ^"%%%%~nB.es-419.srt^"" >>"%%~nA.bat" ... )
So I tried this instead :
chcp 1252 setlocal enabledelayedexpansion FOR %%A in (*.mp4) DO ( SET name=%%~nA SET merge=^"C:\Program Files\MKVToolNix\mkvmerge.exe^" -o ^"!name!.mkv^" ^"!name!.mp4^" FOR %%B in ("!name!.*.srt") DO ( set srt=%%B FOR /F "tokens=2 delims=." %%C in ("!srt!") DO set lang=%%C set merge=!merge! ^"!name!.!lang!.srt^" ) echo !merge!>>"%%~nA mkvmerge.bat" ) pause
Disclaimer : My grasp of these scripting things is cursory at best (in this case I had a hard time figuring out how each special character ought to be “escaped” -- this helped), what I came up with may contain egregious mistakes, so test thoroughly before commiting to any of the above.
Note : I put « chcp 1252 » to deal with names containing accentuated characters (as the name of the file I tested the script with), it should not be required with english names.
Note : It would be a bit more streamlined, and more convenient in general, to put a copy of mkvmerge.exe (or better yet a hard link) somewhere in your system's “path”. That way you would only have to type « mkvmerge » instead of « "C:\Program Files\MKVToolNix\mkmerge.exe" ».
Note : You could add all commands to process all MP4 files in a folder to the same script by specifying a single name, with no variable, for instance « echo !merge!>>"mkvmerge.bat" », but then there would be a risk of overwriting a previous script with the same name left in the same folder which you may want to keep for future reference (although the « >> » redirection appends new data to an existing file, as opposed to « > » which does overwrite).
Note : It might be possible to improve the above script to achieve the last of your queries, i.e. set explicit names and have “forced” subtitles activated by default. Enough for today as far as I'm concerned, but someone may chime in.
Last edited by abolibibelot; 2nd Oct 2021 at 14:24.