VideoHelp Forum
+ Reply to Thread
Results 1 to 9 of 9
Thread
  1. Hi All
    There is an old thread (https://forum.videohelp.com/threads/365907-MKVMERGE-Batch-Muxing-multiple-audio-and-subtitle-tracks) that is close to what I am looking for but I lack the DOS skills necessary to mod it so this is a plea for assistance in that area.

    I have a directory with video files and sub-directories
    For each video file, there is a subdirectory of the same name (excluding extension)
    The subdirectory contains a number of subtitles (usually .srt) in a variety of languages. The subtitle file name is the name of the language of the subtitle preceded by a sequence number - for example 2-English.srt

    I would like a batch file to
    scan the video directory (typically the directory in which the batch file resides)
    For each video file
    --find any associated subdirectory
    --merge the subtitles from that subdirectory with the video file into a new video file using the subtitle file name as the name of the subtitle track in the output file

    MKV format for the output video file format is fune
    It would be an added bonus if the subtitle files used could be limited to a specified list of languages
    Thanks in advance for any suggestions.
    Quote Quote  
  2. I gave it a shot, from my admittedly limited but slowly progressing knowledge of that stuff...
    Code:
    chcp 1252
    setlocal enabledelayedexpansion
    FOR %%F in (*.mp4, *.mkv) DO (
        set name=%%~nF
        set subs=
        FOR %%G in (".\!name!\*.srt") DO (
            set subname=%%~nG
            set subs=!subs! --language 0:und --track-name 0:!subname! "(" ".\!name!\!subname!.srt" ")"
        )
        mkvmerge --output "%%~nF - remux.mkv" --language 0:und --default-track 0:yes --language 1:eng --default-track 1:yes "(" "%%F" ")"!subs!
    )
    pause
    Seems to work as expected. An extra refinement would be to set the correct subtitle language for each subtitle stream (instead of “und”), but that would be much more complicated, unless some scripting wizard can come up with a clever trick I'm not aware of. Likewise, I wouldn't know how to restrict the inclusion of subtitles to a specified list of languages. Also, the audio language may not be english for all video files, in which case replace “--language 1:eng” by “--language 1:und”.
    This would process MP4 and MKV files, you can add other extensions if needed to process other types of video files.
    The “chcp” command (change codepage) is required to process paths containing accentuated characters, codepage 1252 is suitable for french language, other codepages are suitable for other languages ; this should not be necessary if all paths / names contain only regular english characters.

    (Luckily it didn't work right away, because, right before launching the first draft of the script I told myself : “If it works right away... I'll s**k my own d**k...” — and I injured my lower back a few days ago as I was squatting with 115kg so now is not a good time to pull that off, so to speak...)
    Quote Quote  
  3. Pretty safe bet it wouldn't work right away. I don't think any 'computer thing' I have done as ever worked 1st time - it occasionally appears to have worked but on closer inspection . . . . .But that's probably just my incompetence!.

    Anyway thanks for your efforts - when I've finished typing this, I'll give it a go.

    Language - with a slew of subtitles in different languages its not really usable unless the subtitle stream has a Title that says what the language is .
    My knowledge of DOS batch files stopped 00s of years ago but I believe there are now string manipulation functions (https://www.dostips.com/DtTipsStringManipulation.php or https://www.dostips.com/DtTipsStringOperations.php)

    So one could extract the subtitle language from the file name and presumably use MKVMERGE to add it as a title to the subtitle stream?
    Even better if the lead characters can be used to determine the stream number - though I presume the subtitles will by default get stream numbers (and hence order of appearance) sequentially in the order processed?

    Thanks again for your help
    Quote Quote  
  4. Update: Seems to work fine on a single video file and multiple subs. Also subtitle stream is given the file name of the subtitle which is great.
    So onto multiple files - which also work fine..

    The subtitle files are generally named in the format nn_language where nn is a 2 digit number used to sequence
    for /F "tokens=2 delims=_" %%a in ("!subname!") DO set slang=%%a&set sjunk=%%b
    splits the filename using the delimiter character (though why it puts the second part which holds the language into the first variable is a mystery to me!) so I can name the subtitle streams accurately
    And I put the output files into a different directory
    And it all works quite beautifully
    Thank you
    Last edited by Rincewindwiz; 12th Nov 2021 at 00:49.
    Quote Quote  
  5. Well, glad I could be useful to somebody ! (Doesn't happen much these days.)

    As for why the FOR /F command doesn't work as expected, I'm not sure what you mean, since you're mentioning a different naming scheme, but if I test it, only the first variable gets attributed to sumpting (tested with “subname=01_french” and obtained the value “french” for %%a, while %%b remained %%b) ; if using “tokens=1,2”, then parts 1 and 2 are processed (and only parts 1 and 2 if there are more based on the delimiter(s) used), with %%a being attributed the value in 1, %%b being attributed the value in 2 ; so if you only need the second value, you shouldn't need an extra variable %%b.
    Quote Quote  
  6. Only too glad to have been of sufficient service to allow you to be of service . . . or something like that . At least it kept you away from those dangerous 250lb weights!

    Naming scheme is as previously mentioned (with _ instead of -)

    FOR /f - yes my fault. Being unfamiliar with the conventions (%%a, %A%, !A!) etc I spent quite some time experimenting with FOR trying to work out how it actually worked
    When I finally cracked enough of it to make it work, I had failed to work out that Tokens=2 means take the second part (and therefore there is nothing to assign to the second variable)
    The following morning, slightly clearer headed and all coffee'd up I came across https://renenyffenegger.ch/notes/Windows/dirs/Windows/System32/cmd_exe/commands/for/f/tokens which made everything much clearer.

    The only remaining Q (which is an mkvmerge issue) is how to set it up so that the default is not to show subtitles (as opposed to showing the first one)

    Thanks again
    Quote Quote  
  7. With MKVToolNix you can play with the various options well laid out in the GUI (with embedded explanations appearing when hovering over almost any spot for 1s) in order to get the intended result, and then display the corresponding mkvmerge command line (Multiplexer > Show command line), and then you can modify the batch script accordingly. Easier than sifting through a huge list of commands. In this case, selecting “no” from the drop-down menu for the “‘Default track’ flag” option simply adds “ --default-track 0:no” before the name of the corresponding SRT file. So this should work :
    Code:
    chcp 1252
    setlocal enabledelayedexpansion
    FOR %%F in (*.mp4, *.mkv) DO (
        set name=%%~nF
        set subs=
        FOR %%G in (".\!name!\*.srt") DO (
            set subname=%%~nG
            set subs=!subs! --language 0:und --track-name 0:!subname! --default-track 0:no "(" ".\!name!\!subname!.srt" ")"
        )
        mkvmerge --output "%%~nF - remux.mkv" --language 0:und --default-track 0:yes --language 1:eng --default-track 1:yes "(" "%%F" ")"!subs!
    )
    pause
    (Non tested — I don't want to make another silly bet with myself today !)

    As for the “dangerous weights”, well, sometimes you win, sometimes you fail misebarly, as the great Tom Platz said... (Watch until at least 30min50s. And if you're wondering what was so special in the 1970s, watch this afterwards. I read somewhere that Arnold Schwarzenegger once said that if he had had to train at the insane level of intensity practiced and professed by Tom Platz, he would have quit early on.)
    Last edited by abolibibelot; 12th Nov 2021 at 09:51.
    Quote Quote  
  8. Thank you.

    Interesting insight into mkvtoolnix - I'll maybe go play some more

    As to Tom PLatz: Looks like a complicated (and painful!) way of saying if you try you will fail some (even most) of the time but if you don't try you will never succeed.

    And as to the second link - well why would anyone want muscles like that? Good job we are all different

    Basically I'm with Arnie!!
    Quote Quote  
  9. PS: Anywhere you can recommend on an intro to the naming conventions in these DOS scripts? As earlier I get really confused with %%name, %name%, !name! etc
    Last time I did this was back in the 60s when there were only a few commands and no loops
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!