mkvtoolnix批量删除音轨

时间:2013-12-16 21:34:27

标签: python audio mkv

我想在NAS上节省一些空间并删除不必要的曲目。 我发现this Script 但我的debian(nas os)不支持mkvtoolnix 6.6.0及更高版本

我的输出被描述为here at "old output"。 并且脚本使用新输出

我认为在剧本中必须重新定义“AUDIO_RE”以满足我的需要。

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

这就是我到目前为止所得到的......但是剧本不起作用

我能做什么?

1 个答案:

答案 0 :(得分:2)

我使用编写了一个脚本,这是一种旧的Unix脚本语言,也适用于Windows。我想,你可以得到一个Debian awk包。


用法:

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

的示例:

  • 保留英语和西班牙语音频,保留法语和德语字幕,无章节

    awk -f script.awk video.mkv :eng:spa :fre:ger
    
  • 保留英语音频,没有潜艇,保留章节

    awk -f script.awk video.mkv :eng "" chapters
    
  • 保持音频,没有潜艇,但章节

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

在每个语言代码前加上一个冒号:。一些有效的代码是:

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

请在变量PATH中为MKVToolnix发行版设置完整的MKVMerge


Windows和Unix中的不同引用需要注意:

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

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

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

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

如果你知道另一个/更好/正确的Unix引用,请告诉我。


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"\"!"
}