summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMkvAutoSubset <[email protected]>2021-10-17 20:17:58 +0800
committerKurenai <[email protected]>2021-10-17 20:17:58 +0800
commitc0f88cb080950879bc8af5cf498ac3aa8a8c68fb (patch)
treecaa193ee7f100a1b22d5fa7fa8dfe1120f7ae757
parent70a7c4edba281122c05ef46e9efffe5309ef8448 (diff)
Bump to 3.1.3
- 增加C函数导出 - 增加python调用的demo
-rw-r--r--c/exports.go87
-rw-r--r--c/go.mod18
-rw-r--r--c/main.go3
-rw-r--r--c/sdk.py35
-rw-r--r--cmd/main.go14
-rw-r--r--cmd/title.go2
-rw-r--r--lib/ass.go14
-rw-r--r--lib/mkv.go14
-rw-r--r--lib/shared.go20
9 files changed, 187 insertions, 20 deletions
diff --git a/c/exports.go b/c/exports.go
new file mode 100644
index 0000000..60b823a
--- /dev/null
+++ b/c/exports.go
@@ -0,0 +1,87 @@
+package main
+
+import (
+ "C"
+ "encoding/json"
+ "mkvlib"
+)
+
+var _instance = mkvlib.GetInstance()
+
+//export CheckInstance
+func CheckInstance() bool {
+ return _instance != nil
+}
+
+//export GetMKVInfo
+func GetMKVInfo(file *C.char) *C.char {
+ if !CheckInstance() {
+ return cs("")
+ }
+ obj := _instance.GetMKVInfo(gs(file))
+ data, _ := json.Marshal(obj)
+ return cs(string(data))
+}
+
+//export DumpMKV
+func DumpMKV(file, output *C.char, subset bool) bool {
+ if !CheckInstance() {
+ return false
+ }
+ return _instance.DumpMKV(gs(file), gs(output), subset)
+}
+
+type checkSubset_R struct {
+ Subseted bool `json:"subseted"`
+ Error bool `json:"error"`
+}
+
+//export CheckSubset
+func CheckSubset(file *C.char) *C.char {
+ if !CheckInstance() {
+ return cs("")
+ }
+ a, b := _instance.CheckSubset(gs(file))
+ data, _ := json.Marshal(checkSubset_R{a, b})
+ return cs(string(data))
+}
+
+//export CreateMKV
+func CreateMKV(file, tracks, attachments, output, slang, stitle *C.char, clean bool) bool {
+ if !CheckInstance() {
+ return false
+ }
+ a := make([]string, 0)
+ b := make([]string, 0)
+ err := json.Unmarshal([]byte(gs(tracks)), &a)
+ if err == nil {
+ _tracks := a
+ 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 false
+}
+
+//export ASSFontSubset
+func ASSFontSubset(files, fonts, output *C.char, dirSafe bool) 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 false
+}
+
+func cs(gs string) *C.char {
+ return C.CString(gs)
+}
+
+func gs(cs *C.char) string {
+ return C.GoString(cs)
+}
diff --git a/c/go.mod b/c/go.mod
new file mode 100644
index 0000000..1c0e378
--- /dev/null
+++ b/c/go.mod
@@ -0,0 +1,18 @@
+module mkvlib/c
+
+go 1.17
+
+require mkvlib v0.0.0
+
+require (
+ github.com/antchfx/xmlquery v1.3.8 // indirect
+ github.com/antchfx/xpath v1.2.0 // indirect
+ github.com/asticode/go-astikit v0.20.0 // indirect
+ github.com/asticode/go-astisub v0.19.0 // indirect
+ github.com/asticode/go-astits v1.8.0 // indirect
+ github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
+ golang.org/x/net v0.0.0-20200904194848-62affa334b73 // indirect
+ golang.org/x/text v0.3.2 // indirect
+)
+
+replace mkvlib => ../lib
diff --git a/c/main.go b/c/main.go
new file mode 100644
index 0000000..38dd16d
--- /dev/null
+++ b/c/main.go
@@ -0,0 +1,3 @@
+package main
+
+func main() {}
diff --git a/c/sdk.py b/c/sdk.py
new file mode 100644
index 0000000..a48a55e
--- /dev/null
+++ b/c/sdk.py
@@ -0,0 +1,35 @@
+from ctypes import *
+from json import *
+
+libpath="./mkvlib.so"
+lib=CDLL(libpath)
+
+def checkInstance():
+ call=lib.CheckInstance
+ return call()
+
+def getMKVInfo(file):
+ call=lib.GetMKVInfo
+ call.restype=c_char_p
+ return call(file.encode())
+
+def dumpMKV(file,output,subset,dirSafe):
+ call=lib.DumpMKV
+ return call(file.encode(),output.encode(),subset,dirSafe)
+
+def checkSubset(file):
+ call=lib.CheckSubset
+ call.restype=c_char_p
+ return call(file.encode())
+
+def createMKV(file,tracks,attachments,output,slang,stitle,clean):
+ call=lib.CreateMKV
+ _tracks=dumps(tracks)
+ _attachments=dumps(attachments)
+ return call(file.encode(),_tracks.encode(),_attachments.encode(),output.encode(),slang.encode(),stitle.encode(),clean)
+
+def assFontSubset(files,fonts,output,dirSafe):
+ call=lib.ASSFontSubset
+ _files=dumps(files)
+ return call(_files.encode(),fonts.encode(),output.encode(),dirSafe)
+
diff --git a/cmd/main.go b/cmd/main.go
index 509de29..abf0603 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -12,7 +12,7 @@ import (
)
const appName = "MKV Tool"
-const appVer = "3.1.1"
+const appVer = "v3.1.3"
const tTitle = appName + " " + appVer
var processer = mkvlib.GetInstance()
@@ -43,6 +43,7 @@ func main() {
q := false
v := false
clean := false
+ ans := false
sl, st := "", ""
af, ao := "", ""
asses := new(arrayArg)
@@ -55,10 +56,11 @@ func main() {
flag.Var(asses, "a", "ASS files. (multiple & join ass mode)")
flag.BoolVar(&n, "n", false, "Not do ass font subset. (dump mode only)")
flag.BoolVar(&clean, "clean", false, "Clean original file subtitles and fonts. (create mode only)")
- flag.StringVar(&sl, "sl", "chi", " Subtitle language. (create & make mode only)")
- flag.StringVar(&st, "st", "", " Subtitle title. (create & make mode only)")
- flag.StringVar(&af, "af", "", " ASS fonts folder. (ASS mode only)")
- flag.StringVar(&ao, "ao", "", " ASS output folder. (ASS mode only)")
+ flag.StringVar(&sl, "sl", "chi", "Subtitle language. (create & make mode only)")
+ flag.StringVar(&st, "st", "", "Subtitle title. (create & make mode only)")
+ flag.StringVar(&af, "af", "", "ASS fonts folder. (ass mode only)")
+ flag.StringVar(&ao, "ao", "", "ASS output folder. (ass mode only)")
+ flag.BoolVar(&ans, "ans", false, `ASS output not to the new "subseted" folder. (ass mode only)`)
flag.StringVar(&data, "data", "data", "Subtitles & Fonts folder (dump & make mode only)")
flag.StringVar(&dist, "dist", "dist", "Results output folder (make mode only)")
@@ -77,7 +79,7 @@ func main() {
}
if len(*asses) > 0 {
- if !processer.ASSFontSubset(*asses, af, ao) {
+ if !processer.ASSFontSubset(*asses, af, ao, !ans) {
ec++
}
return
diff --git a/cmd/title.go b/cmd/title.go
index b292eb5..b49043d 100644
--- a/cmd/title.go
+++ b/cmd/title.go
@@ -2,6 +2,8 @@
package main
+import "fmt"
+
func setWindowTitle(title string) {
fmt.Printf("\033]0;%s\007", title)
}
diff --git a/lib/ass.go b/lib/ass.go
index d1c114c..7482ac8 100644
--- a/lib/ass.go
+++ b/lib/ass.go
@@ -103,14 +103,14 @@ func (self *assProcessor) parse() bool {
str += string(_k)
}
str = strings.TrimSpace(str)
- str = reg.ReplaceAllString(str, "")
- str += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
- reg, _ = regexp.Compile("[1234567890]")
- if reg.MatchString(str) {
- str = reg.ReplaceAllString(str, "")
- str += "1234567890"
- }
if str != "" {
+ str = reg.ReplaceAllString(str, "")
+ str += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
+ reg, _ = regexp.Compile("[1234567890]")
+ if reg.MatchString(str) {
+ str = reg.ReplaceAllString(str, "")
+ str += "1234567890"
+ }
self.m[k] = new(fontInfo)
self.m[k].str = str
self.m[k].oldName = k
diff --git a/lib/mkv.go b/lib/mkv.go
index 6dd76c9..e5eeb85 100644
--- a/lib/mkv.go
+++ b/lib/mkv.go
@@ -90,7 +90,7 @@ func (self *mkvProcessor) DumpMKV(file, output string, subset bool) bool {
asses = append(asses, f)
}
if len(asses) > 0 {
- if !self.ASSFontSubset(asses, "", "") {
+ if !self.ASSFontSubset(asses, "", "", false) {
ec++
}
}
@@ -227,7 +227,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, "") {
+ if !self.ASSFontSubset(asses, fDir, "", false) {
ec++
} else {
__p := path.Join(p, "subsetted")
@@ -272,7 +272,7 @@ func (self *mkvProcessor) MakeMKVs(dir, data, output, slang, sttlte string) bool
return ec == 0
}
-func (self *mkvProcessor) ASSFontSubset(files []string, fonts, output string) bool {
+func (self *mkvProcessor) ASSFontSubset(files []string, fonts, output string, dirSafe bool) bool {
if len(files) == 0 {
return false
}
@@ -280,15 +280,17 @@ func (self *mkvProcessor) ASSFontSubset(files []string, fonts, output string) bo
obj.files = files
obj._fonts = fonts
obj.output = output
-
d, _, _, _ := splitPath(obj.files[0])
if obj._fonts == "" {
obj._fonts += path.Join(d, "fonts")
}
if obj.output == "" {
- obj.output += path.Join(d, "subsetted")
+ obj.output = d
+ dirSafe = true
+ }
+ if dirSafe {
+ obj.output = path.Join(obj.output, "subseted")
}
-
obj.fonts = findFonts(obj._fonts)
return obj.parse() && obj.matchFonts() && obj.createFontsSubset() && obj.changeFontsName() && obj.replaceFontNameInAss()
diff --git a/lib/shared.go b/lib/shared.go
index 215ce7a..b0cfb2f 100644
--- a/lib/shared.go
+++ b/lib/shared.go
@@ -2,11 +2,15 @@ package mkvlib
import (
"log"
+ "os"
"os/exec"
+ "path/filepath"
+ "runtime"
+ "strings"
)
const libName = "mkvlib"
-const libVer = "1.0.0"
+const libVer = "v1.0.2"
const LibFName = libName + " " + libVer
@@ -14,6 +18,20 @@ var _instance *mkvProcessor
func GetInstance() *mkvProcessor {
ec := 0
+ n := "PATH"
+ s := ":"
+ if runtime.GOOS == "windows" {
+ n = "path"
+ s = ";"
+ }
+ p := os.Getenv(n)
+ if !strings.HasSuffix(p, s) {
+ p += s
+ }
+ e, _ := os.Executable()
+ e, _ = filepath.Split(e)
+ p += e
+ _ = os.Setenv(n, p)
_, _ttx := exec.LookPath(ttx)
_, _pyftsubset := exec.LookPath(pyftsubset)
_, _mkvextract := exec.LookPath(mkvextract)