Saturday, August 30, 2014

Setting default audio & subtitle in MKV files using a batch file.

Matroska (mkv) files are video files that can contain multiple tracks, allowing for multiple subtitles and audios, and one can set a default audio & subtitle for those.

mkvtoolnix is a collection of tools (mkvmerge, mkvmerge gui [mmg], mkvinfo, mkvextract, and mkvpropedit) for matroska (mkv) files.

The batch file below allows one to view the default flag for the tracks in a mkv file and then toggle the default flag on/off. Copy the text block below and save it to a batch file, say MKV.BAT and put that bat file somewhere in your path (e.g. C:\WINDOWS).

@echo off
if '%1==' goto ?
::change code page to utf8 (mkvinfo always writes utf8 when redirected)
set track=
set mkvtoolnix=%ProgramFiles%\mkvtoolnix
%windir%\system32\chcp 65001>nul
"%mkvtoolnix%\mkvinfo.exe" %1>%temp%\mkvinfo.txt
if '%2==' goto info

set flag=unknown
for /f "tokens=3-9,11*" %%a in (%temp%\mkvinfo.txt) do call :flag %2 %%a %%b %%c %%d %%e %%f %%g %%h %%i
echo Track %2 (%name% %type%) default flag is %flag%
if %flag%==1 goto unset
echo Now setting flag to 1
"%mkvtoolnix%\mkvpropedit.exe" %1 --edit track:%2 --set flag-default=1
"%mkvtoolnix%\mkvinfo.exe" %1>%temp%\mkvinfo.txt
goto info

:unset
echo Now setting flag to 0
"%mkvtoolnix%\mkvpropedit.exe" %1 --edit track:%2 --set flag-default=0
"%mkvtoolnix%\mkvinfo.exe" %1>%temp%\mkvinfo.txt

:info
for /f "tokens=3-9,11*" %%a in (%temp%\mkvinfo.txt) do call :filter %%a %%b %%c %%d %%e %%f %%g %%h %%i
if not "%track%"=="" echo %track%
goto :eof

:filter
if '%2==' goto :eof
for %%v in (Content A Pixel Display (size: Lacing CodecPrivate size head Timecode Muxing Writing Segment Sampling Channels version: read maximum type type: Duration: Date: MinCache: Video Audio Algorithm: Max Order: Scope: Type: Interlaced: Enabled: Forced) do if %1==%%v goto :eof
for %%v in (EbmlVoid Chapters EditionEntry EditionFlagHidden: EditionFlagDefault: EditionUID: ChapterAtom ChapterUID: ChapterTimeStart: ChapterFlagHidden: ChapterFlagEnabled: ChapterDisplay ChapterString: ChapterLanguage: Cluster) do if %1==%%v goto :eof
for %%v in (UID: duration: decode) do if %2==%%v goto :eof
if %2==number: goto :track
if "%track%"=="" goto :eof
if %2==type: (set track=%track% %3) & goto :eof
if %2==ID: (set track=%track%, %3) & goto :eof
set track=%track%, %*
goto :eof

:track
if not "%track%"=="" echo %track%
if %3 LSS 10 (set track=Track %3: ) else set track=Track %3:
goto :eof

:flag
if '%3==' goto :eof
if %3==number: if '%1=='%4 (set track=1) else set track=
if '%track%==' goto :eof
if %2==Name: set name=%3
if %2==Default if %3==flag: set flag=%4
if %2==Track if %3==type: set type=%4
goto :eof

:?
echo MKV mkvfile [track number]
echo.
echo If track number not specified, the tracks will be listed
echo.
echo If track number specified, the default flag will be toggled for that track.

You will need to set the path to mkvtoolnix appropriately (edit the line set mkvtoolnix=...) to point to the mkvtoolnix folder.

Remember if you set a default flag on for one audio track, remember to unset the default flag for any other audio track that had the default flag on.