How to remove tag spam and set language in several MKV files at once

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)
  • missed to tag the language of audio and/or subtitle tracks

Dragging every single episode file into MKVtoolnix and setting all those things right is certainly possible, but repetitive and annoying.

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.
If you don’t have it yet, download and install it. We will be using it below.

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 works only if all files in the current folder are identical in terms of number of tracks, type of tracks, and the order of tracks.

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!)

Make sure to always use a copy of your original video files for experimenting with this script.

Instructions for Windows:

(see below for Linux and OSX)

  • 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 setlangs.bat
  • now copy it to the folder where your MKV files are waiting to be processed

Before you can use the script,

you must adapt the script to the makeup of 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.
The following explanations will enable you to do it.

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 :wink: )

--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 v1.)

The following --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

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.)


I have created a second file, which you can use to remux mp4 or avi files into MKV containers. Copy the complete content and paste it into a text editor. Save the file as e.g. remux_to_mkv.bat

for %%f in (*.avi) do (
        echo %%~nf
         "C:\Program Files\MKVToolNix\mkvmerge" -o "%%~nf.mkv" --language 0:ger --language 1:jpn "%%~nf.avi"

)

If your source files are mp4 instead of avi, replace all instances of the string .avi with .mp4.

You must adapt the ‘language’ parameter as well.


Version notes:
- 2018-11-18 reordered some parts of the text to improve the structure and readability
- 2021-09-07 replaced the “remuxer” script with a copy&paste text box, because the forum regularly purges file attachments

7 Likes

Instructions for Linux

if you use the ‘bourne’ or ‘bourne again shell’ (bash)

after you have installed the mkvtoolnix package, you open the editor of your choice and
copy this into it:

#!/bin/sh
for f in *.mkv
do
 echo -n $f  ' '  
 mkvpropedit --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 "${f}"
done

save it as setlangs.sh

then set the execute flag on the script:
chmod +x setlangs.sh

The file is now ready for use.
Copy it into the folder with your MKV files, modify it according to the above guide, then execute it.

Thanks to @ChuckPa for these instructions!

1 Like

Instructions for OSX

(are practically identical to those for Linux, with the exception of the first line of the script.)

after you have installed the mkvtoolnix package, you open the editor of your choice and
copy this into it:

#!/usr/bin/env bash
for f in *.mkv
do
 echo -n $f  ' '  
 mkvpropedit --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 "${f}"
done

save it as setlangs.sh

then set the ‘execute’ flag on the script in terminal:
chmod +x setlangs.sh

The file is now ready for use.
Copy it into the folder with your MKV files, modify it according to the above guide, then execute it.

For users of a single language, here is a solution which first looks for ‘undetermined’ language tags and replaces them with a fixed language setting.

For many users this might be already sufficient

Just for more clarification, the script will look at all audio tracks(not just one or the first) so it will also work for multiple, single language tracks within a mkv file (MainMovie, DirectorCommentary, ActorCommentary, etc…).

Anyone know how to make this scan through all the folders and sub-folders and apply all the settings?
I have something that kind of does it but it seems to not be applying any of the settings, if anyone can help i would appreciate it.

@echo off
for /r %%g in (*.mkv) do (mkvpropedit "%%~g" --tags all:"" --delete title --edit track:v1 --set flag-forced=0 --set codec-name="" --delete name --edit track:a1 --set flag-forced=0 --delete name --edit track:a2 --set flag-forced=0 --delete name --delete-attachment 1 --delete-attachment 2 --delete-attachment 3 --delete-attachment 4 --delete-attachment 5)
pause

If your command line addresses a third audio track, but the file has only one or two, then the command is doing nothing to the file.

That’s why I mentioned above that the script has to be modified and adapted to the exact make up of the files and that all files must have the same number and order of tracks.
I don’t know if this behaviour can be changed. You’d have to read the documentation of mkvpropedit.

1 Like

I’d be cautious with deleting attachments.
If you have an anime fansub, those attachments are often special fonts which are referenced in the ASS subtitles.

really? good call, thanks :smiley:

This version seems to work :smiley: it did pretty well when i put it in my movie directory and it was able to go through all the folders in that directory.

@echo off
for /r %%g in (*.mkv) do (mkvpropedit "%%~g" --tags all:"" --delete title --edit track:v1 --set flag-forced=0 --set codec-name="" --delete name --edit track:a1 --set flag-forced=0 --delete name)
pause

Luckily mkvtoolkit can use track headers to identify(modify) a particular track no matter the order. This makes it much easier to edit files with a common amount of tracks(mostly video, audio, and maybe sub’s) regardless of their order.

1 Like

You may want to set the language of your audio while you are modifying the files. This will help with displaying subtitles when needed(if set correctly).
track:a1 --set flag-default=1 --set flag-forced=0 --set language=eng
This assumes english(eng).

Though I strongly suggest you use caution with the command --delete name on tracks cause you could be removing detailed descriptions of the tracks. Things like a “commentary track” or “Subtitles - SDH”, or similar.
More on what I am trying to say.

While Plex doesn’t support this(yet). I hope someday it will.

A far easier way to do all of this is grab a freebie named “JMkvpropedit” off Github and use that.It’s basically a GUI for these same command line scripts and yes, you can make changes to many files at once, including changing the movie title to match the file’s name. They have a Windows exe and a jar version that works great on macOS and, I would assume, Linux.

Just to be clear… JMkpropedit does not remux the files it processes. I’ve used it to re-title my media files; adjust the track names; fix the audio language flag; add chapters (XML format only); and strip out attachments like embedded fonts. Processing typically is under a second, maybe a couple seconds if batching 30+ files or so.

Honestly, I have never found out how to even launch it. Even though I have the Java runtime on my machine.

https://github.com/BrunoReX/jmkvpropedit/releases/download/v1.4.3/jmkvpropedit-1.4.3-bin.zip
It has two text files, one jar file, and one executable file.
If you are on Windows then you should be able to launch the executable but I still find the batch files better/faster/easier to update and maintain.

1 Like

Agreed. The UI confuses me more than making it more accessible. :smiley:

Easier is certainly in the eye of the beholder. Back in the good old days, some guy was insisting to me that Macs were so much easier and friendlier to use, but I insisted back that I liked DOS and could jump around in Lotus 1-2-3 faster by plugging in a cell number than using a mouse to drag around looking for it.

If my needs were extremely straight forward, then having scripts available as an Automator action that I can run off my context menu would be easier and faster. I have one that strips out the _t0x that MakeMKV adds to a file name when ripping a disc, for example.

Come to think of it, my usage of JMkvpropedit actually is pretty basic for the most part. Maybe I will set up a couple context menu scripts to do most of what I use it for.

That’s exactly what I do. Someday when I get off my lazy ass I will create a program or two that will do everything I want just by running the app. Have it “auto-detect” all my needs.

First time poster, so apologies if this has been covered elsewhere. I know the original posting is from many years ago, but recently updated. For those of us who are rubbish at coding, I include myself at that, Mkvtoolnix seems to have added a new function that makes the use of some coding redundant. There is a new (or at least to me as I only noticed it recently) feature that allows batch converting of Mp4 and Avi files in the GUI. Simply select Multiplexer at the top > this gives a drop down list offering an option for > Actions for all tabs. By dragging multiple file into the GUI as individual items, Mkvtoolnix will batch create mkv for each file. This might not seem much, but for me saved hours of repetitious clicking.
Anyway, I hope this helps someone, as mkvtoolnix is a great product.

Windows Server in Powershell:

Windows Powershell functions to convert video files with mkvmerge from mp4 and avi to mkv and set all mkv default audio and video language to your desired language with mkvpropedit all in batch to be used for your PLEX server or something else.

1 Like