If you deal with MKV files, you sooner or later make contact with MKVtoolnixGUI. This little tool allows you to do almost anything with a MKV file. Tagging, remuxing, setting chapters etc.
Now imagine you have ripped a tv show and the software of your choice has
- embedded nonsensical things into the ‘title’ tags
- missed to mark ‘forced’ subtitles as such
- added titles like ‘Surround 5.1’ to audio tracks (which is redundant, if you’re using Plex)
Dragging every single episode file into MKVtoolnix and setting all those things right is certainly possible, but repetitive and annoying.
Luckily, MKVtoolnix brings some command-line tools with it, which we will put to work in a batch file to process all our episodes at once. Attention: the method described hereafter only works if all files in the current folder are identical. In terms of number of tracks, type of tracks, and the order of tracks. Make sure to always use a copy of your original video files for experimenting with this script.
- In case you haven’t already, download and install MKVtoolnix
- open a text editor (like notepad.exe. Microsoft Word or similar is not suitable!)
- copy the following into it
for %%f in (*.mkv) do ( echo %%~nf "C:\Program Files\MKVToolNix\mkvpropedit" --tags all:"" --delete title --edit track:v1 --set language=jpn --set flag-forced=0 --delete name --edit track:a1 --set language=ger --set flag-forced=0 --delete name --edit track:a2 --set language=jpn --set flag-forced=0 --delete name --edit track:s1 --set language=ger --set flag-forced=1 --delete name --edit track:s2 --set language=ger --set flag-forced=0 --set name=full "%%~nf.mkv" )
please forgive me if the above script is not very elegantly written or contains redundant stuff. I just grabbed it from somewhere on the net and adapted it. Tips to improve it are very welcome!
- save the file as
- now copy it to the folder where your MKV files are waiting to be processed
The first two and the last line are only there to make the script iterate through every file in the current folder.
The line beginning with
"C:\Program Files... is where the magic happens.
##Let’s dissect this command line a bit:
mkvpropedit modifies the Header of a MKV file directly, i.e. without the file getting remuxed. This happens very fast. (But this also means that you better use only copies of your media files for testing it out )
--tags all:"" removes all types of XML-based tags (Handbrake infamously adds these)
--delete title will remove the ‘Title’ tag of the MKV file
--edit track:v1 marks the beginning of the commands for editing the (first) video track
(It is not usual to have more than one video track in a file. Nonetheless, we are using
--set language=jpn sets the language tag to ‘japanese’ for only the video track. (In this example, I chose japanese, because in Anime usually all text, signs and credits in the video picture appear in japanese. If you have a video where the subtitles have been 'burned in’to the picture, you should rather set the language of those subtitles here. This is inconsequential for Plex.)
Language codes must be 3-letter ISO639-2 (bibliographic) (which means ‘pt-BR’ is not a valid code)
--set flag-forced=0 removes the ‘forced’ flag from the video track.
This is rather ‘cosmetic’, because the ‘forced’ flag has practically no use for video tracks.
--delete name Removes the name of this individual track.
Then we select the first audio track:
--edit track:a1 (a for Audio)
Again, we set the language to ‘japanese’ and erase the ‘forced’ flag. The name of the track is removed as well.
--edit track:a1 --set language=jpn --set flag-forced=0 --delete name
it follows the second audio track, which is german in our example:
--edit track:a2 --set language=ger --set flag-forced=0 --delete name
Then we treat the subtitle tracks. There are several of these in this MKV file.
The first subtitle track s1 (s for ‘subtitle’) contains the ‘forced’ subtitles in german. (‘forced’ is to be used only for translations of ‘text/signs’ on screen and spoken language in a 3rd language (meaning when a character speaks e.g. russian and the user has activated the german audio track)
--edit track:s1 --set language=ger --set flag-forced=1 --delete name
The second subtitle track s2 contains the ‘complete’ subtitles, which translate every line of spoken dialogue into german. (For users who prefer to listen to the original japanese audio.) The name of this track is set to ‘full’
--edit track:s2 --set language=ger --set flag-forced=0 --set name=full
Using the above explanations, you must adapt the script to the files you want to process.
If your files only have one audio track, remove the instruction for the second audio track from the command line.
If they have 3 subtitle tracks instead, add instructions for subtitle track
s3, and so on.
I have attached the sample file to this posting: setlangs.zip (426 Bytes)
The script will only touch files which are in the same folder. It will not dive down into subfolders. (This is intentional, to reduce the potentially devastating consequences of firing it off unintentionally.)
Post Scriptum regarding ‘forced’ subtitles: The name ‘forced’ is very misleading and often leads to a wrong use of the '‘forced’ flag. The function of the flag is better described by “subtitles for foreign-language dialogue and signage”. Set the ‘forced’ flag according to this description in your files. Use it only for this type of subtitles. Never for ‘complete’ subtitles. And yes, it is allowed to have several subtitle tracks as ‘forced’. (Although this usually only makes sense if each ‘forced’ track has a different language.)
Now you may ask, “How do I know the make up of my files? e.g. Which audio track is first in my files?” Here is another little helper which is very useful. It will tell you the properties of your video files upon a right-click: download mediainfo here (pay attention during installation, it may come with a ‘piggybacked’ installer which asks to install additional crapware. Make sure to set/clear the right checkboxes!)
I have attached a second file, which you can use to remux mp4 or avi files into MKV containers. remux_to_mkv.zip (292 Bytes)
Here you must adapt the ‘language’ parameter as well and the type of file name extension you want to process.