Find all MKV files and remove all subtitles
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.
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 offclsset 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]== ( 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:
It uses for /r to recursively search %rootfolder% for all MKVs
It then runs mkvmerge -i on each MKV to check whether a subtitle track exists
If the MKV does contain subtitle tracks, it runs mkvmerge -S to remux the file while skipping all such tracks
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.
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 offsetlocal enabledelayedexpansion:: set your variables hereset rootfolder=”C:tempNew folder”set language=engset suffix= (eng sub):: do the remuxingecho 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