Find all MKV files and remove all subtitles

5

3

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.

David Custer

Posted 2013-05-24T08:51:53.857

Reputation: 373

1Which 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 – 2013-05-24T10:17:13.473

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 – 2013-05-24T10:36:56.783

1Then 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 – 2013-05-24T10:44:22.893

I got this do mkvmerge -o ./newfiles/basename "$i" mkv.mkv --no-subtitles "$i" ; done ....but it doesn't seem to be working correctly. – David Custer – 2013-05-24T11:17:43.313

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 – 2013-05-24T17:29:16.303

Answers

8

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.

Karan

Posted 2013-05-24T08:51:53.857

Reputation: 51 857

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 – 2013-05-24T22:13:53.000

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 – 2013-05-24T22:17:41.963

OOps. I didn't know I could do that. Took me a minute to figure it out. Thanks soooooooo much! – David Custer – 2013-05-24T22:19:53.723

If you need more help with batch commands, I always refer people to this site.

– Karan – 2013-05-24T22:26:57.200

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 – 2013-05-24T22:29:29.630

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 – 2013-05-24T22:31:53.780

mkvextract doesn't seem to have very many command options. Or more importantly the right ones. Still looking... – David Custer – 2013-05-24T22:44:11.040

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... – Karan – 2013-05-24T22:47:32.770

Yea. Its just not straight forward like "filebot -r -get-subtitles --lang en E:\Movies". I'm just a little confused that's all, I'll get it. – David Custer – 2013-05-24T23:03:02.870

let us continue this discussion in chat

– David Custer – 2013-05-24T23:07:13.370

0

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

jutzin

Posted 2013-05-24T08:51:53.857

Reputation: 1