Search code examples
batch-filemkvmuxer

MkvToolNix: Bat file / Batch file to mux pairs of videos together


Struggling for some time in that problem. I have a list of videos going this way, by pairs :

magic.mkv magic-1.mkv

food.mkv food-2.mkv

wedding.mkv wedding-3.mkv

etc.

Each video has its pair, and the pair has a number that is incrementing every time. I want to mux each pair together. And for each pair : First video (without the numbers) keep everything except for chapters, Second video (with the incrementing number) to keep all subtitles only.

Now I know how to select tracks and so on, what I don't know, is how to make this .bat file mux those pairs, whiches are all in the same folder. (I know I could put each pair in a different folder, but I have more than 700 files so first I would like to see if there is a way to do this with code)

Opened to any suggestion (I'm not very tech saavy please be specific).

Thank you in advance.

Tried that, and it crashed ("error the file cannont be opened for reading" - open file error) :

for %%a & for %%a-1 in (*.mkv) do "\\\\\\MKV Tool Nix\mkvtoolnix\mkvmerge.exe" -o "tada\%%~a" --default-track "5:yes" --track-order 1:0,1:1,1:2,1:3,0:4,0:5,0:6,0:7,0:8,0:9,0:10 "%%~a"

Tried that, and it crashed ("error the file cannont be opened for reading" - open file error) :

for %%a in (*.mkv) do "C:\\MKV Tool Nix\mkvtoolnix\mkvmerge.exe" -o "tada\%%~a" '(' %%~na-1%%~xa ')' --default-track "5:yes" --track-order 1:0,1:1,1:2,1:3,0:4,0:5,0:6,0:7,0:8,0:9,0:10 "%%~a"

Tried that, and it does not crash, but it looks for the second video like that : videoone.mkv-1, as opposed to videoone-1.mkv. It means that %%a will look for the full name including the specified extension, so probably %%a is not the right thing to use since I need to specify a file name, then the extension :

for %%a in (*.mkv) do "C:\\MKV Tool Nix\mkvtoolnix\mkvmerge.exe" -o "tada\%%~a" '(' %%a %%a-1 ')' --default-track "5:yes" --track-order 1:0,1:1,1:2,1:3,0:4,0:5,0:6,0:7,0:8,0:9,0:10 "%%~a"

Solution

  • Does the following example script help you out?

    @Echo Off
    SetLocal EnableExtensions DisableDelayedExpansion
    
    For %%G In (
        *-1*.mkv
        *-2*.mkv
        *-3*.mkv
        *-4*.mkv
        *-5*.mkv
        *-6*.mkv
        *-7*.mkv
        *-8*.mkv
        *-9*.mkv
    ) Do (
        Set "filebasename=%%~nG"
        SetLocal EnableDelayedExpansion
        Set "filebasename=!filebasename:.=?!"
        For %%H In ("!filebasename:-=.!") Do (
            EndLocal
            Set "pairedfilebasename=%%~nH"
            SetLocal EnableDelayedExpansion
            Set "pairedfilebasename=!pairedfilebasename:.=-!"
            For %%I In ("!pairedfilebasename:?=.!") Do (
                EndLocal
                If Exist "%%~I%%~xG" (
                    Echo matching suffixed filename to "%%~I%%~xG" is "%%~G"
                )
            )
        )
    )
    
    Pause
    

    All you should need to do, if it works for you, is change the Echo command line to your particular mkvmerge.exe command line, remembering that "%%~I%%~xG" is the non suffixed filename and "%%G" is the suffixed one.

    Depending upon the makeup of your specific filenames you may wish to change:

        *-1*.mkv
        *-2*.mkv
        *-3*.mkv
        *-4*.mkv
        *-5*.mkv
        *-6*.mkv
        *-7*.mkv
        *-8*.mkv
        *-9*.mkv
    

    to:

        *-*0.mkv
        *-*1.mkv
        *-*2.mkv
        *-*3.mkv
        *-*4.mkv
        *-*5.mkv
        *-*6.mkv
        *-*7.mkv
        *-*8.mkv
        *-*9.mkv
    

    If your filenames are even more complex, you may be better advised to change to outer For loop to a For /F loop using the Dir command piped to findstr.exe filtering using something like /IR "\-[123456789][0123456789]*\.mkv$".