Windows – Find all MKV files and remove all subtitles

batch filematroskasubtitleswindows

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.

Best Answer

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.

Related Question