mkvtoolnix bulk remove audio tracks

4.1k views Asked by At

i want to save some space on my NAS and remove unnecessary Tracks. i found this Script but my debian (nas os) doesnt support mkvtoolnix 6.6.0 and higher

my output is descriped here at "old output". and the script uses the new output

i think in the script "AUDIO_RE" has to be redefined to fit my needs.

AUDIO_RE    = re.compile(r"Track ID (\d+): audio \([A-Z0-9_/]+\) [language:[a-z]{3}?")

thats what i got so far... but the script doesnt work

what can i do ?

1

There are 1 answers

0
Endoro On BEST ANSWER

I scripted it with , an old Unix scripting language, available also for Windows. I guess, you can get a Debian awk package.


usage:

awk -f script.awk [video.mkv] [audio-langs-to-keep] [subtitle-langs-to-keep] [keep-chapters]

examples:

  • keep English and Spain audio, keep French and German subtitles, no chapters

     awk -f script.awk video.mkv :eng:spa :fre:ger
    
  • keep English audio, no subs, keep the chapters

     awk -f script.awk video.mkv :eng "" chapters
    
  • keep no audio, no subs but the chapters

    awk -f script.awk video.mkv "" "" chapters
    

Put a colon : in front of every language code. Some valid codes are:

English language name                         | ISO639-2 code
----------------------------------------------+---------------
Afrikaans                                     | afr
Arabic                                        | ara
Australian languages                          | aus
Baltic languages                              | bat
Catalan; Valencian                            | cat
Caucasian languages                           | cau
Chinese                                       | chi
Corsican                                      | cos
Croatian                                      | hrv
Czech                                         | cze
Dutch; Flemish                                | dut
English                                       | eng
French                                        | fre
Georgian                                      | geo
German                                        | ger
Greek, Modern (1453-)                         | gre
Hebrew                                        | heb
Hindi                                         | hin
Hungarian                                     | hun
Irish                                         | gle
Italian                                       | ita
Japanese                                      | jpn
Javanese                                      | jav
Korean                                        | kor
Kurdish                                       | kur
Norwegian                                     | nor
Persian                                       | per
Polish                                        | pol
Portuguese                                    | por
Romanian; Moldavian; Moldovan                 | rum
Russian                                       | rus
Serbian                                       | srp
Spanish; Castillan                            | spa
Swedish                                       | swe
Thai                                          | tha
Turkish                                       | tur
Ukrainian                                     | ukr
Vietnamese                                    | vie
Walloon                                       | wln
Yiddish                                       | yid

Please set the full PATH to your MKVToolnix distribution in the variable MKVMerge.


Attention is needed for the different quoting in Windows and Unix:

  • Windows

    Result=("\""MKVMerge"\" --ui-language en --identify-verbose \""MKVVideo"\"" | getline Line);
    
  • Unix(?)

    Result=("'MKVMerge' --ui-language en --identify-verbose 'MKVVideo'" | getline Line);
    

  • Windows

    Result=system("\"" MKVMerge "\" -o \"" NewVideo "\" " CommandLine " \"" MKVVideo "\"")
    
  • Unix(?)

    Result=system("'MKVMerge' -o 'NewVideo' CommandLine 'MKVVideo'")
    

If you know another/better/correct Unix quoting, please let me know.


script.awk

BEGIN {
    MKVMerge="/bin/MKVMerge" # for Linux
    MKVMerge="C:\\Program Files\\mkvtoolnix\\MKVMerge.exe" # for Win32
    MKVMerge="C:\\Program Files (x86)\\mkvtoolnix\\MKVMerge.exe" # for Win64
    FS="[\t\n: ]"
    IGNORECASE=1
    MKVVideo=ARGV[1]
    AudioKeep=ARGV[2]
    SubsKeep=ARGV[3]
    ChaptersKeep=ARGV[4]
    NewVideo=substr(MKVVideo, 1, length(MKVVideo)-4)".new.mkv"
    do {
        Result=("\""MKVMerge"\" --ui-language en --identify-verbose \""MKVVideo"\"" | getline Line);
        if (Result>0) {
            FieldCount=split(Line, Fields)
            if (Fields[1]=="Track") {
                NoTr++
                Track[NoTr, "id"]=Fields[3]
                Track[NoTr, "typ"]=Fields[5]
                for (i=6; i<=FieldCount; i++) {
                    if (Fields[i]=="language") Track[NoTr, "lang"]=Fields[++i]
                }
            }
        }
    }   while (Result>0)
    if (NoTr==0) {
        print "Error! No tracks found in \""MKVVideo"\"."
        exit
    } else {print "\""MKVVideo"\":", NoTr, "tracks found."}
    for (i=1; i<=NoTr; i++) {
        if (Track[i, "typ"]=="audio") {
            if (AudioKeep~Track[i, "lang"]) {
                print "Keep", Track[i, "typ"], "Track", Track[i, "id"],  Track[i, "lang"]
                if (AudioCommand=="") {AudioCommand=Track[i, "id"]
            } else AudioCommand=AudioCommand","Track[i, "id"]
            } else {
                print "\tRemove", Track[i, "typ"], "Track", Track[i, "id"],  Track[i, "lang"]
            }
        } else {
            if (Track[i, "typ"]=="subtitles") {
                if (SubsKeep~Track[i, "lang"]) {
                    print "Keep", Track[i, "typ"], "Track", Track[i, "id"],  Track[i, "lang"]
                    if (SubsCommand=="") {SubsCommand=Track[i, "id"]
                    } else SubsCommand=SubsCommand","Track[i, "id"]
                } else {
                    print "\tRemove", Track[i, "typ"], "Track", Track[i, "id"],  Track[i, "lang"]
                }
            }
        }
    }
    if (AudioCommand=="") {CommandLine="-A"
    } else {CommandLine="-a "AudioCommand}
    if (SubsCommand=="") {CommandLine=CommandLine" -S"
    } else {CommandLine=CommandLine" -s "SubsCommand}
    if (!ChaptersKeep) CommandLine=CommandLine" --no-chapters"
    print "\"" MKVMerge "\" -o \"" NewVideo "\" " CommandLine " \"" MKVVideo "\""
    Result=system("\"" MKVMerge "\" -o \"" NewVideo "\" " CommandLine " \"" MKVVideo "\"")
    if (Result>1) print "Error "Result" muxing \""MKVVideo"\"!"
}