Find all MKV files and remove all subtitles

10,988

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:
10,988

Related videos on Youtube

David Custer
Author by

David Custer

Updated on September 18, 2022

Comments

  • David Custer
    David Custer almost 2 years

    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.

    • Jan Doggen
      Jan Doggen about 11 years
      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?
    • David Custer
      David Custer about 11 years
      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.
    • Jan Doggen
      Jan Doggen about 11 years
      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.
    • David Custer
      David Custer about 11 years
      I got this do mkvmerge -o ./newfiles/basename "$i" mkv.mkv --no-subtitles "$i" ; done ....but it doesn't seem to be working correctly.
    • evilsoup
      evilsoup about 11 years
      @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.
  • David Custer
    David Custer about 11 years
    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..
  • Karan
    Karan about 11 years
    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.
  • David Custer
    David Custer about 11 years
    OOps. I didn't know I could do that. Took me a minute to figure it out. Thanks soooooooo much!
  • Karan
    Karan about 11 years
    If you need more help with batch commands, I always refer people to this site.
  • David Custer
    David Custer about 11 years
    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.
  • Karan
    Karan about 11 years
    Everybody has to start somewhere, and IMO if you learn to do it yourself you never forget. :) Just run mkvextract on a single file and once you get the command to be used, insert it before the mkvmerge -S line that remuxes the file (use "%%a" for the source-filename parameter).
  • Karan
    Karan about 11 years
    I don't understand what you mean by saying it doesn't have the right command-line options. Did you see the "Track extraction mode" section of the docs? A simple Google search for help on subtitle extraction using mkvextract also turned up lots of tutorials...
  • David Custer
    David Custer about 11 years