diff options
| author | b5f0d6c3 <[email protected]> | 2021-10-20 19:58:44 +0800 |
|---|---|---|
| committer | b5f0d6c3 <[email protected]> | 2021-10-20 19:58:44 +0800 |
| commit | 9d9068cf4a6eb8d8b8f48005a9c6db0dea135c80 (patch) | |
| tree | 0b5359438ae8745cf04e5c36a4f98174fa76e33e /mkvlib | |
| parent | 4c4bb91ece9ceb8841e45910c9ffa49cd64c9c22 (diff) | |
update mkvlib:add logcallback
Diffstat (limited to 'mkvlib')
| -rw-r--r-- | mkvlib/ass.go | 28 | ||||
| -rw-r--r-- | mkvlib/c/exports.go | 29 | ||||
| -rw-r--r-- | mkvlib/c/lcb.c | 3 | ||||
| -rw-r--r-- | mkvlib/c/lcb.h | 2 | ||||
| -rw-r--r-- | mkvlib/mkv.go | 44 | ||||
| -rw-r--r-- | mkvlib/shared.go | 51 |
6 files changed, 100 insertions, 57 deletions
diff --git a/mkvlib/ass.go b/mkvlib/ass.go index 7482ac8..00ff7f1 100644 --- a/mkvlib/ass.go +++ b/mkvlib/ass.go @@ -6,7 +6,6 @@ import ( "github.com/antchfx/xmlquery" "github.com/asticode/go-astisub" "io" - "log" "os" "path" "regexp" @@ -39,6 +38,7 @@ type assProcessor struct { fonts []string sFonts []string subtitles map[string]string + lcb logCallback } func (self *assProcessor) parse() bool { @@ -58,7 +58,7 @@ func (self *assProcessor) parse() bool { } } if ec > 0 { - log.Printf(`Failed to read the ass file: "%s"`, file) + printLog(self.lcb, `Failed to read the ass file: "%s"`, file) } } if ec == 0 { @@ -68,7 +68,7 @@ func (self *assProcessor) parse() bool { subtitle, err := astisub.ReadFromSSA(strings.NewReader(v)) if err != nil { ec++ - log.Printf(`Failed to read the ass file: "%s"`, k) + printLog(self.lcb, `Failed to read the ass file: "%s"`, k) continue } for _, item := range subtitle.Items { @@ -118,7 +118,7 @@ func (self *assProcessor) parse() bool { } } if len(self.m) == 0 { - log.Printf(`Not Found item in the ass file(s): "%d"`, len(self.files)) + printLog(self.lcb, `Not Found item in the ass file(s): "%d"`, len(self.files)) } return ec == 0 } @@ -142,7 +142,7 @@ func (self *assProcessor) dumpFont(file string, full bool) bool { if strings.HasSuffix(file, ".ttc") { count = self.getTTCCount(file) if count < 1 { - log.Printf(`Failed to get the ttc font count: "%s".`, n) + printLog(self.lcb, `Failed to get the ttc font count: "%s".`, n) return ok } } @@ -162,7 +162,7 @@ func (self *assProcessor) dumpFont(file string, full bool) bool { ok = err == nil && s.ExitCode() == 0 } if !ok { - log.Printf(`Failed to dump font(%t): "%s"[%d].`, full, n, i) + printLog(self.lcb, `Failed to dump font(%t): "%s"[%d].`, full, n, i) } } return ok @@ -260,7 +260,7 @@ func (self *assProcessor) matchFonts() bool { for _, v := range self.m { if v.file == "" { ok = false - log.Printf(`Missing the font: "%s".`, v.oldName) + printLog(self.lcb, `Missing the font: "%s".`, v.oldName) } } return ok @@ -274,7 +274,7 @@ func (self *assProcessor) createFontSubset(font *fontInfo) bool { e = ".ttf" } if os.MkdirAll(self.output, os.ModePerm) != nil { - log.Println("Failed to create the output folder.") + printLog(self.lcb, "Failed to create the output folder.") return false } if os.WriteFile(fn, []byte(font.str), os.ModePerm) == nil { @@ -291,13 +291,13 @@ func (self *assProcessor) createFontSubset(font *fontInfo) bool { ok = err == nil && s.ExitCode() == 0 } if !ok { - log.Printf(`Failed to subset font: "%s"[%s].`, n, font.index) + printLog(self.lcb, `Failed to subset font: "%s"[%s].`, n, font.index) } else { font.sFont = _fn } } else { - log.Printf(`Failed to write the font text: "%s".`, n) + printLog(self.lcb, `Failed to write the font text: "%s".`, n) } return ok } @@ -305,7 +305,7 @@ func (self *assProcessor) createFontSubset(font *fontInfo) bool { func (self *assProcessor) createFontsSubset() bool { err := os.RemoveAll(self.output) if !(err == nil || err == os.ErrNotExist) { - log.Println("Failed to clean the output folder.") + printLog(self.lcb, "Failed to clean the output folder.") return false } ok := 0 @@ -366,11 +366,11 @@ func (self *assProcessor) changeFontName(font *fontInfo) bool { if !ok { ec++ _, n, _, _ := splitPath(font.sFont) - log.Printf(`Failed to compile the font: "%s".`, n) + printLog(self.lcb, `Failed to compile the font: "%s".`, n) } } } else { - log.Printf(`Faild to change the font name: "%s".`, font.oldName) + printLog(self.lcb, `Faild to change the font name: "%s".`, font.oldName) } } } @@ -438,7 +438,7 @@ func (self *assProcessor) replaceFontNameInAss() bool { ec++ } if !ok { - log.Printf(`Failed to write the new ass file: "%s".`, fn) + printLog(self.lcb, `Failed to write the new ass file: "%s".`, fn) } } } diff --git a/mkvlib/c/exports.go b/mkvlib/c/exports.go index 1f22703..38cde1a 100644 --- a/mkvlib/c/exports.go +++ b/mkvlib/c/exports.go @@ -1,16 +1,23 @@ package main +// #include "lcb.h" +import "C" import ( - "C" "encoding/json" "github.com/KurenaiRyu/MkvAutoSubset/mkvlib" ) -var _instance = mkvlib.GetInstance() +var processor = mkvlib.GetProcessorGetterInstance() + +func _lcb(lcb C.logCallback) func(string) { + return func(str string) { + C.makeLogCallback(cs(str), lcb) + } +} //export CheckInstance func CheckInstance() bool { - return _instance != nil + return processor.GetProcessorInstance() != nil } //export GetMKVInfo @@ -18,17 +25,17 @@ func GetMKVInfo(file *C.char) *C.char { if !CheckInstance() { return cs("") } - obj := _instance.GetMKVInfo(gs(file)) + obj := processor.GetProcessorInstance().GetMKVInfo(gs(file)) data, _ := json.Marshal(obj) return cs(string(data)) } //export DumpMKV -func DumpMKV(file, output *C.char, subset bool) bool { +func DumpMKV(file, output *C.char, subset bool, lcb C.logCallback) bool { if !CheckInstance() { return false } - return _instance.DumpMKV(gs(file), gs(output), subset) + return processor.GetProcessorInstance().DumpMKV(gs(file), gs(output), subset, _lcb(lcb)) } type checkSubset_R struct { @@ -37,11 +44,11 @@ type checkSubset_R struct { } //export CheckSubset -func CheckSubset(file *C.char) *C.char { +func CheckSubset(file *C.char, lcb C.logCallback) *C.char { if !CheckInstance() { return cs("") } - a, b := _instance.CheckSubset(gs(file)) + a, b := processor.GetProcessorInstance().CheckSubset(gs(file), _lcb(lcb)) data, _ := json.Marshal(checkSubset_R{a, b}) return cs(string(data)) } @@ -59,21 +66,21 @@ func CreateMKV(file, tracks, attachments, output, slang, stitle *C.char, clean b err = json.Unmarshal([]byte(gs(attachments)), &b) if err == nil { _attachments := b - return _instance.CreateMKV(gs(file), _tracks, _attachments, gs(output), gs(slang), gs(stitle), clean) + return processor.GetProcessorInstance().CreateMKV(gs(file), _tracks, _attachments, gs(output), gs(slang), gs(stitle), clean) } } return false } //export ASSFontSubset -func ASSFontSubset(files, fonts, output *C.char, dirSafe bool) bool { +func ASSFontSubset(files, fonts, output *C.char, dirSafe bool, lcb C.logCallback) bool { if !CheckInstance() { return false } obj := make([]string, 0) if json.Unmarshal([]byte(gs(files)), &obj) == nil { _files := obj - return _instance.ASSFontSubset(_files, gs(fonts), gs(output), dirSafe) + return processor.GetProcessorInstance().ASSFontSubset(_files, gs(fonts), gs(output), dirSafe, _lcb(lcb)) } return false } diff --git a/mkvlib/c/lcb.c b/mkvlib/c/lcb.c new file mode 100644 index 0000000..ffb8439 --- /dev/null +++ b/mkvlib/c/lcb.c @@ -0,0 +1,3 @@ +#include "lcb.h" + +void makeLogCallback(char* s, logCallback lcb){ lcb(s); }
\ No newline at end of file diff --git a/mkvlib/c/lcb.h b/mkvlib/c/lcb.h new file mode 100644 index 0000000..8a2e2cc --- /dev/null +++ b/mkvlib/c/lcb.h @@ -0,0 +1,2 @@ +typedef void (*logCallback)(char*); +void makeLogCallback(char*, logCallback);
\ No newline at end of file diff --git a/mkvlib/mkv.go b/mkvlib/mkv.go index 268cd49..d1aa2c3 100644 --- a/mkvlib/mkv.go +++ b/mkvlib/mkv.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "fmt" - "log" "os" "path" "regexp" @@ -48,11 +47,11 @@ func (self *mkvProcessor) GetMKVInfo(file string) *mkvInfo { return nil } -func (self *mkvProcessor) DumpMKV(file, output string, subset bool) bool { +func (self *mkvProcessor) DumpMKV(file, output string, subset bool, lcb logCallback) bool { ec := 0 obj := self.GetMKVInfo(file) if obj == nil { - log.Printf(`Failed to get the mkv file info: "%s".`, file) + printLog(lcb, `Failed to get the mkv file info: "%s".`, file) return false } attachments := make([]string, 0) @@ -97,7 +96,7 @@ func (self *mkvProcessor) DumpMKV(file, output string, subset bool) bool { asses = append(asses, f) } if len(asses) > 0 { - if !self.ASSFontSubset(asses, "", "", false) { + if !self.ASSFontSubset(asses, "", "", false, lcb) { ec++ } } @@ -110,15 +109,15 @@ func (self *mkvProcessor) DumpMKV(file, output string, subset bool) bool { ec++ } } else { - log.Printf(`This mkv file is not has the subtitles & attachments: "%s"`, file) + printLog(lcb, `This mkv file is not has the subtitles & attachments: "%s"`, file) } return ec == 0 } -func (self *mkvProcessor) CheckSubset(file string) (bool, bool) { +func (self *mkvProcessor) CheckSubset(file string, lcb logCallback) (bool, bool) { obj := self.GetMKVInfo(file) if obj == nil { - log.Printf(`Failed to get the mkv file info: "%s".`, file) + printLog(lcb, `Failed to get the mkv file info: "%s".`, file) return false, true } ass := false @@ -175,7 +174,7 @@ func (self *mkvProcessor) CreateMKV(file string, tracks, attachments []string, o return false } -func (self *mkvProcessor) DumpMKVs(dir, output string, subset bool) bool { +func (self *mkvProcessor) DumpMKVs(dir, output string, subset bool, lcb logCallback) bool { ec := 0 files := findMKVs(dir) l := len(files) @@ -183,33 +182,33 @@ func (self *mkvProcessor) DumpMKVs(dir, output string, subset bool) bool { p := strings.TrimPrefix(item, dir) d, _, _, f := splitPath(p) p = path.Join(output, d, f) - if !self.DumpMKV(item, p, subset) { + if !self.DumpMKV(item, p, subset, lcb) { ec++ - log.Printf(`Failed to dump the mkv file "%s".`, item) + printLog(lcb, `Failed to dump the mkv file "%s".`, item) } - log.Printf("Dump (%d/%d) done.", i+1, l) + printLog(lcb, "Dump (%d/%d) done.", i+1, l) } return ec == 0 } -func (self *mkvProcessor) QueryFolder(dir string) []string { +func (self *mkvProcessor) QueryFolder(dir string, lcb logCallback) []string { ec := 0 lines := make([]string, 0) files := findMKVs(dir) l := len(files) for i, file := range files { - a, b := self.CheckSubset(file) + a, b := self.CheckSubset(file, lcb) if b { ec++ } else if !a { lines = append(lines, file) } - log.Printf("Query (%d/%d) done.", i+1, l) + printLog(lcb, "Query (%d/%d) done.", i+1, l) } return lines } -func (self *mkvProcessor) CreateMKVs(vDir, sDir, fDir, tDir, oDir string, slang, stitle string, clean bool) bool { +func (self *mkvProcessor) CreateMKVs(vDir, sDir, fDir, tDir, oDir string, slang, stitle string, clean bool, lcb logCallback) bool { ec := 0 if tDir == "" { tDir = os.TempDir() @@ -237,7 +236,7 @@ func (self *mkvProcessor) CreateMKVs(vDir, sDir, fDir, tDir, oDir string, slang, tracks := make([]string, 0) if len(asses) > 0 { _ = os.RemoveAll(tDir) - if !self.ASSFontSubset(asses, fDir, "", false) { + if !self.ASSFontSubset(asses, fDir, "", false, lcb) { ec++ } else { __p := path.Join(p, "subsetted") @@ -251,15 +250,15 @@ func (self *mkvProcessor) CreateMKVs(vDir, sDir, fDir, tDir, oDir string, slang, ec++ } if ec > 0 { - log.Printf(`Failed to create the mkv file: "%s".`, item) + printLog(lcb, `Failed to create the mkv file: "%s".`, item) } - log.Printf("Create (%d/%d) done.", i+1, l) + printLog(lcb, "Create (%d/%d) done.", i+1, l) } _ = os.RemoveAll(tDir) return ec == 0 } -func (self *mkvProcessor) MakeMKVs(dir, data, output, slang, sttlte string) bool { +func (self *mkvProcessor) MakeMKVs(dir, data, output, slang, sttlte string, lcb logCallback) bool { ec := 0 files := findMKVs(dir) l := len(files) @@ -275,14 +274,14 @@ func (self *mkvProcessor) MakeMKVs(dir, data, output, slang, sttlte string) bool fn := path.Join(output, d, n) if !self.CreateMKV(item, tracks, attachments, fn, slang, sttlte, true) { ec++ - log.Printf(`Faild to make the mkv file: "%s".`, item) + printLog(lcb, `Faild to make the mkv file: "%s".`, item) } - log.Printf("Make (%d/%d) done.", i+1, l) + printLog(lcb, "Make (%d/%d) done.", i+1, l) } return ec == 0 } -func (self *mkvProcessor) ASSFontSubset(files []string, fonts, output string, dirSafe bool) bool { +func (self *mkvProcessor) ASSFontSubset(files []string, fonts, output string, dirSafe bool, lcb logCallback) bool { if len(files) == 0 { return false } @@ -290,6 +289,7 @@ func (self *mkvProcessor) ASSFontSubset(files []string, fonts, output string, di obj.files = files obj._fonts = fonts obj.output = output + obj.lcb = lcb d, _, _, _ := splitPath(obj.files[0]) if obj._fonts == "" { obj._fonts += path.Join(d, "fonts") diff --git a/mkvlib/shared.go b/mkvlib/shared.go index 5deaf68..397e667 100644 --- a/mkvlib/shared.go +++ b/mkvlib/shared.go @@ -1,6 +1,7 @@ package mkvlib import ( + "fmt" "log" "os" "os/exec" @@ -10,13 +11,27 @@ import ( ) const libName = "mkvlib" -const libVer = "v1.0.4" +const libVer = "v1.1.0" const LibFName = libName + " " + libVer -var _instance *mkvProcessor +type logCallback func(string) + +type processorGetter struct { + checked bool + instance *mkvProcessor +} + +var _instance = new(processorGetter) + +func GetProcessorGetterInstance() *processorGetter { + return _instance +} + +func (self *processorGetter) InitProcessorInstance(lcb logCallback) bool { + self.checked = false + self.instance = nil -func GetInstance() *mkvProcessor { ec := 0 n := "PATH" s := ":" @@ -37,18 +52,34 @@ func GetInstance() *mkvProcessor { _, _mkvextract := exec.LookPath(mkvextract) _, _mkvmerge := exec.LookPath(mkvmerge) if _ttx != nil || _pyftsubset != nil { - log.Printf(`Missing dependency: fonttools (need "%s" & "%s").`, ttx, pyftsubset) + printLog(lcb, `Missing dependency: fonttools (need "%s" & "%s").`, ttx, pyftsubset) ec++ } if _mkvextract != nil || _mkvmerge != nil { - log.Printf(`Missing dependency: mkvtoolnix (need "%s" & "%s").`, mkvextract, mkvmerge) + printLog(lcb, `Missing dependency: mkvtoolnix (need "%s" & "%s").`, mkvextract, mkvmerge) ec++ } - if ec > 0 { - return nil + + r := ec == 0 + if r { + self.checked = true + self.instance = new(mkvProcessor) } - if _instance == nil { - _instance = new(mkvProcessor) + + return r +} + +func (self *processorGetter) GetProcessorInstance() *mkvProcessor { + if self.checked { + return self.instance + } + return nil +} + +func printLog(lcb logCallback, f string, v ...interface{}) { + if lcb != nil { + lcb(fmt.Sprintf(f, v...)) + } else { + log.Printf(f, v...) } - return _instance } |
