Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm currently looking at a Windows program called mkvmerge. I would like to create a batch file to find all MKV files recursively from a specified path, and remove all subtitles from the MKV files found (if the MKV found contains subtitles), finally deleting all the original MKV files that had the subtitles removed.

I've done about 2 hours of googling, and I'm finding that you have to be able to write things like this:

FOR /F "delims=*" %%A IN ('dir /b *.MKV') DO "C:\mkvmerge.exe" -o "fixed_%%A" -a 4 -s 7 --compression -1:none "%%A"

I'm still trying, but if someone can give me a bit of help, I'd really appreciate it.

share|improve this question
1  
Which part is not working, what are the errors you get? Did you first test the for...do part, just echoing %%A? Did you then try one single DO... on one of the names coming out of the first test? –  Jan Doggen May 24 '13 at 10:17
    
That was just an example from the internet. I have know idea what I'm doing. This is the last part to setting up my automated media center for all the legal videos that I purchase. I'm now trying to teach myself this code stuff, so I can write this batch file. –  David Custer May 24 '13 at 10:36
1  
Then learn yourself "batch programming" or "batch file programming" (these are your search terms, together with maybe "tutorial") and split your effort into the two steps I suggested. –  Jan Doggen May 24 '13 at 10:44
    
Thanks soo much! –  David Custer May 24 '13 at 10:53
1  
@DavidCuster that's an incomplete line of bash - a scripting language primarily used on Linux and OSX. It is possible to get it working on Windows, but probably more effort than it's worth. Look for Windows batch script or Windows batch for loop. Hopefully someone will post a good explanation as an answer - I would do it myself, but I'd have to look up a reference since my Batch scripting is very shaky. –  evilsoup May 24 '13 at 17:29

2 Answers 2

up vote 4 down vote accepted

Save the following as something like DelMKVSubs.bat in the same directory mkvmerge.exe is in, edit the rootfolder variable as per your requirements and run the batch file:

@echo off
cls
set rootfolder=C:\
echo Enumerating all MKVs under %rootfolder%
echo.
for /r %rootfolder% %%a in (*.mkv) do (
    for /f %%b in ('mkvmerge -i "%%a" ^| find /c /i "subtitles"') do (
        if [%%b]==[0] (
            echo "%%a" has no subtitles
        ) else (
            echo.
            echo "%%a" has subtitles
            mkvmerge -q -o "%%~dpna (No Subs)%%~xa" -S "%%a"
            if errorlevel 1 (
                echo Warnings/errors generated during remuxing, original file not deleted
            ) else (
                del /f "%%a"
                echo Successfully remuxed to "%%~dpna (No Subs)%%~xa", original file deleted
            )
            echo.
        )
    )
)

The batch file should be easy enough to understand, but here's an overview nevertheless:

  1. It uses for /r to recursively search %rootfolder% for all MKVs

  2. It then runs mkvmerge -i on each MKV to check whether a subtitle track exists

  3. If the MKV does contain subtitle tracks, it runs mkvmerge -S to remux the file while skipping all such tracks

  4. Finally it checks the exit code of mkvmerge and if this (i.e. errorlevel) is 0 indicating success with no warnings/errors, it deletes the original file

For more see the mkvmerge documentation and also for /?, if /? etc. at the command prompt.

share|improve this answer
    
This works! I put MKVToolNix in my environment variables path. I can not thank you enough. I now am trying to learn for myself what you just did, I would like to be able to do what you did on my own. It's pretty amazing! THANK YOU! THANK YOU! THANK YOU! ;) I do have one question. If when you run mkvmerge -i it has english subtitles, would it be possible to tell it to extract them to that directory before remuxing? I am currently looking into this and trying to figure this out myself. I just thought I might ask you.. –  David Custer May 24 '13 at 22:13
    
You're welcome, and don't forget to accept the answer if it helped you! If you want to extract (subtitle) tracks from an MKV, take a look at the documentation for the aptly named mkvextract. –  Karan May 24 '13 at 22:17
    
OOps. I didn't know I could do that. Took me a minute to figure it out. Thanks soooooooo much! –  David Custer May 24 '13 at 22:19
    
If you need more help with batch commands, I always refer people to this site. –  Karan May 24 '13 at 22:26
    
Thanks! I'm reading it over. I'll try and edit yours do use mkvextract. I'll post it when I'm done. Might not be today lol I'm not as fast as you. –  David Custer May 24 '13 at 22:29

Thanks, Karan, this was very useful to me, too. I've modified your script so that you can now pick a language (specified by 3LC (eng, ned, swe etc.)) for the subtitles you would like to keep. Also, I removed the part where the input files get deleted and chose to add a suffix to the output file.

Here's my version:

:: remux all mkvs under a certain subfolder with all subitles
:: but those as specified by %language% parameter removed.

@echo off
setlocal enabledelayedexpansion

:: set your variables here
set rootfolder="C:\temp\New folder"
set language=eng
set suffix= (eng sub)

:: do the remuxing
echo Remuxing all mkvs in %rootfolder% and its subfolders.
for /r %rootfolder% %%a in (*.mkv) do (
    set subs=
    set mkv=%%a
    for /f "tokens=3 delims=: " %%b in ('mkvmerge -I "%%a" ^| findstr /i /r ".*subtitles.*language:%language%.*"') do (
        set subs=!subs!,%%b
    )
    for /f "tokens=*delims=," %%c in ("!subs!") do (
        set subs=%%c
    )
    if not exist "%%~dpna%suffix%%%~xa" if not "!subs!"=="" (
        mkvmerge -q -o "%%~dpna%suffix%%%~xa" -s !subs! "%%a"
        if errorlevel 1 (
            echo Warnings/errors generated during remuxing of "%%a".
        ) else (
            echo Successfully remuxed to "%%~dpna (eng subs)%%~xa".
        )
    ) else (
        echo Input file "%%a" has no subtitles in %language% or output file "%%~dpna%suffix%%%~xa" already exists. Skipping this file.
    )
)
pause
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.