From 52a9a688bd404d42da137b68ddbfcd177b002b55 Mon Sep 17 00:00:00 2001 From: Kurenai Date: Sun, 10 Oct 2021 21:27:38 +0800 Subject: =?UTF-8?q?=E5=A2=9E=E5=8A=A0-q=E6=A8=A1=E5=BC=8F,=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=8C=87=E5=AE=9A=E7=9B=AE=E5=BD=95=E9=87=8C=E7=9A=84?= =?UTF-8?q?mkv=E6=96=87=E4=BB=B6=E6=98=AF=E5=90=A6=E8=A2=AB=E5=AD=90?= =?UTF-8?q?=E9=9B=86=E5=8C=96,=E5=B9=B6=E8=BE=93=E5=87=BA=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E8=A2=AB=E5=AD=90=E9=9B=86=E5=8C=96=E7=9A=84mkv?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 13 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 66f4fc6..951882e 100644 --- a/main.go +++ b/main.go @@ -18,9 +18,10 @@ const ( type mkv struct { Attachments []struct { - ID int `json:"id"` - FileName string `json:"file_name"` - Size int `json:"size"` + ID int `json:"id"` + FileName string `json:"file_name"` + Size int `json:"size"` + ContentType string `json:"content_type"` } `json:"attachments"` Tracks []struct { ID int `json:"id"` @@ -34,22 +35,28 @@ type mkv struct { } func main() { - setWindowTitle("MKV Tool v2.0.5") + setWindowTitle("MKV Tool v2.1.0") s := "" c := false d := false m := false n := false + q := false sl, st := "", "" flag.StringVar(&s, "s", "", "Source folder.") flag.BoolVar(&c, "c", false, "Create mode.") flag.BoolVar(&d, "d", false, "Dump mode.") flag.BoolVar(&m, "m", false, "Make mode.") + flag.BoolVar(&q, "q", false, "Query mode.") flag.BoolVar(&n, "n", false, "Not do ass font subset. (dump mode only)") flag.StringVar(&sl, "sl", "chi", " Subtitle language. (create mode only)") flag.StringVar(&st, "st", "", " Subtitle title. (create mode only)") flag.Parse() if s != "" { + if q { + queryFolder(s) + return + } if c { if sl != "" { createMKVs(s, sl, st) @@ -73,6 +80,15 @@ func main() { flag.PrintDefaults() } +func getMKVInfo(path string) *mkv { + buf := bytes.NewBufferString("") + p, _ := newProcess(nil, buf, nil, "", mkvmerge, "-J", path) + _, _ = p.Wait() + obj := new(mkv) + _ = json.Unmarshal(buf.Bytes(), obj) + return obj +} + func dumpMKVs(dir string, subset bool) { files, _ := findPath(dir, `\.mkv$`) arr := strings.Split(dir, `\`) @@ -80,11 +96,7 @@ func dumpMKVs(dir string, subset bool) { l := len(files) for i, item := range files { tmp := strings.Replace(item, dir, p, 1) - buf := bytes.NewBufferString("") - p, _ := newProcess(nil, buf, nil, "", mkvmerge, "-J", item) - _, _ = p.Wait() - obj := new(mkv) - _ = json.Unmarshal(buf.Bytes(), obj) + obj := getMKVInfo(item) attachments := make([]string, 0) tracks := make([]string, 0) for _, _item := range obj.Attachments { @@ -109,7 +121,7 @@ func dumpMKVs(dir string, subset bool) { args = append(args, attachments...) args = append(args, "tracks") args = append(args, tracks...) - p, _ = newProcess(nil, nil, nil, "", mkvextract, args...) + p, _ := newProcess(nil, nil, nil, "", mkvextract, args...) _, _ = p.Wait() if subset { asses := make([]string, 0) @@ -125,7 +137,7 @@ func dumpMKVs(dir string, subset bool) { } } } - fmt.Print(fmt.Sprintf("\rDump (%d/%d) done.", i+1, l)) + fmt.Printf("\rDump (%d/%d) done.", i+1, l) } } @@ -162,7 +174,7 @@ func makeMKVs(dir, dir2 string) { } p, _ := newProcess(nil, nil, nil, "", mkvmerge, args...) _, _ = p.Wait() - fmt.Print(fmt.Sprintf("\rMake (%d/%d) done.", i+1, l)) + fmt.Printf("\rMake (%d/%d) done.", i+1, l) } } @@ -226,6 +238,45 @@ func createMKVs(dir string, slang, stitle string) { } _p, _ := newProcess(nil, nil, nil, "", mkvmerge, args...) _, _ = _p.Wait() - fmt.Print(fmt.Sprintf("\rCreate (%d/%d) done.", i+1, l)) + fmt.Printf("\rCreate (%d/%d) done.", i+1, l) + } +} + +func checkSubset(path string) bool { + obj := getMKVInfo(path) + ass := false + ok := false + reg, _ := regexp.Compile(`\.[A-Z0-9]{8}\.\S+$`) + for _, track := range obj.Tracks { + ass = track.Type == "subtitles" && track.Codec == "SubStationAlpha" + if ass { + break + } + } + for _, attachment := range obj.Attachments { + ok = !ass || (strings.HasPrefix(attachment.ContentType, "font/") && reg.MatchString(attachment.FileName)) + if ok { + break + } + } + return !ass || (ass && ok) +} + +func queryFolder(dir string) { + lines := make([]string, 0) + files, _ := findPath(dir, `\.mkv$`) + l := len(files) + for i, file := range files { + if !checkSubset(file) { + lines = append(lines, file) + } + fmt.Printf("\rQuery (%d/%d) done.", i+1, l) + } + if len(lines) > 0 { + fmt.Print("\rHas item(s).") + data := []byte(strings.Join(lines, "\n")) + _ = os.WriteFile("list.txt", data, os.ModePerm) + } else { + fmt.Print("\rNo item.") } } -- cgit v1.2.1