summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorb5f0d6c3 <[email protected]>2022-03-10 00:24:55 +0800
committerb5f0d6c3 <[email protected]>2022-03-10 00:24:55 +0800
commit4ab65022538472627dba5819b2e62d431b37fe6d (patch)
treec66bb57a899afb294cc8acc0eea6be49405687f9
parentebfdeb65631a0681373c3c2e6ce9661eb8c7aefa (diff)
update mkvlib: add ass to psg support.
-rw-r--r--mkvlib/ass.go2
-rw-r--r--mkvlib/ass2pgs.go7
-rw-r--r--mkvlib/ass2pgs_windows.go68
-rw-r--r--mkvlib/mkv.go31
-rw-r--r--mkvlib/shared.go8
-rw-r--r--mkvlib/spp2pgs.exebin0 -> 3914240 bytes
6 files changed, 112 insertions, 4 deletions
diff --git a/mkvlib/ass.go b/mkvlib/ass.go
index 823e174..4af797d 100644
--- a/mkvlib/ass.go
+++ b/mkvlib/ass.go
@@ -33,6 +33,7 @@ type fontInfo struct {
type assProcessor struct {
files []string
+ _files []string
_fonts string
output string
m map[string]*fontInfo
@@ -449,6 +450,7 @@ func (self *assProcessor) replaceFontNameInAss() bool {
ok := false
if os.WriteFile(fn, []byte(s), os.ModePerm) == nil {
ok = true
+ self._files = append(self._files, fn)
} else {
ec++
}
diff --git a/mkvlib/ass2pgs.go b/mkvlib/ass2pgs.go
new file mode 100644
index 0000000..33c1b19
--- /dev/null
+++ b/mkvlib/ass2pgs.go
@@ -0,0 +1,7 @@
+//go:build !windows || (windows && 386)
+
+package mkvlib
+
+func ass2Pgs(input []string, resolution, frameRate int, fontsDir string, output string, lcb logCallback) bool {
+ return false
+}
diff --git a/mkvlib/ass2pgs_windows.go b/mkvlib/ass2pgs_windows.go
new file mode 100644
index 0000000..8068e31
--- /dev/null
+++ b/mkvlib/ass2pgs_windows.go
@@ -0,0 +1,68 @@
+//go:build windows && amd64
+
+package mkvlib
+
+import (
+ "fmt"
+ "path"
+ "strconv"
+ "syscall"
+ "unsafe"
+)
+
+func ass2Pgs(input []string, resolution, frameRate int, fontsDir string, output string, lcb logCallback) bool {
+ fonts := findFonts(fontsDir)
+ r := addFontResource(fonts, lcb)
+ if r {
+ for _, item := range input {
+ _, _, _, _f := splitPath(item)
+ fn := path.Join(output, _f+".pgs")
+ args := make([]string, 0)
+ args = append(args, "-x0")
+ args = append(args, "-v144")
+ args = append(args, "-s", strconv.Itoa(resolution))
+ args = append(args, "-r", strconv.Itoa(frameRate))
+ args = append(args, "-i", item)
+ args = append(args, fn)
+ if p, err := newProcess(nil, nil, nil, "", spp2pgs, args...); err == nil {
+ s, err := p.Wait()
+ r = err == nil && s.ExitCode() == 1
+ if !r {
+ printLog(lcb, fmt.Sprintf(`Failed to Ass2Pgs:"%s"`, item))
+ }
+ }
+ }
+ }
+ removeFontResource(fonts, lcb)
+ return r
+}
+
+var gdi32 = syscall.NewLazyDLL("gdi32.dll")
+var addFontResourceW = gdi32.NewProc("AddFontResourceW")
+var removeFontResourceW = gdi32.NewProc("RemoveFontResourceW")
+
+func addFontResource(fonts []string, lcb logCallback) bool {
+ ec := 0
+ for _, item := range fonts {
+ p, _ := syscall.UTF16FromString(item)
+ r, _, _ := addFontResourceW.Call(uintptr(unsafe.Pointer(&p[0])))
+ if r == 0 {
+ printLog(lcb, fmt.Sprintf(`Failed to load font:"%s"`, item))
+ ec++
+ }
+ }
+ return ec == 0
+}
+
+func removeFontResource(fonts []string, lcb logCallback) bool {
+ ec := 0
+ for _, item := range fonts {
+ p, _ := syscall.UTF16FromString(item)
+ r, _, _ := removeFontResourceW.Call(uintptr(unsafe.Pointer(&p[0])))
+ if r == 0 {
+ printLog(lcb, fmt.Sprintf(`Failed to unload font:"%s"`, item))
+ ec++
+ }
+ }
+ return ec == 0
+}
diff --git a/mkvlib/mkv.go b/mkvlib/mkv.go
index f782b24..0c65094 100644
--- a/mkvlib/mkv.go
+++ b/mkvlib/mkv.go
@@ -7,12 +7,14 @@ import (
"os"
"path"
"regexp"
+ "runtime"
"strings"
)
const (
mkvmerge = `mkvmerge`
mkvextract = `mkvextract`
+ spp2pgs = `spp2pgs`
)
type mkvInfo struct {
@@ -33,7 +35,12 @@ type mkvInfo struct {
}
}
-type mkvProcessor bool
+type mkvProcessor struct {
+ a2p bool
+ apc bool
+ pr int
+ pf int
+}
func (self *mkvProcessor) GetMKVInfo(file string) *mkvInfo {
buf := bytes.NewBufferString("")
@@ -265,7 +272,7 @@ func (self *mkvProcessor) MakeMKVs(dir, data, output, slang, stitle string, lcb
d, n, _, f := splitPath(p)
p = path.Join(data, d, f)
_p := path.Join(p, "subsetted")
- subs, _ := findPath(p, `\.sub`)
+ subs, _ := findPath(p, `\.(sub)|(pgs)`)
asses, _ := findPath(_p, `\.ass$`)
attachments := findFonts(_p)
tracks := append(subs, asses...)
@@ -301,5 +308,23 @@ func (self *mkvProcessor) ASSFontSubset(files []string, fonts, output string, di
}
obj.fonts = findFonts(obj._fonts)
- return obj.parse() && obj.matchFonts() && obj.createFontsSubset() && obj.changeFontsName() && obj.replaceFontNameInAss()
+ r := obj.parse() && obj.matchFonts() && obj.createFontsSubset() && obj.changeFontsName() && obj.replaceFontNameInAss()
+ if self.a2p {
+ r = self.ass2Pgs(obj._files, self.pr, self.pf, obj.output, d, lcb)
+ if r && !self.apc {
+ _ = os.RemoveAll(obj.output)
+ }
+ }
+ return r
+}
+
+func (self *mkvProcessor) A2P(a2p, apc bool, pr, pf int) {
+ self.a2p = runtime.GOOS == "windows" && runtime.GOARCH == "amd64" && a2p
+ self.apc = apc
+ self.pr = pr
+ self.pf = pf
+}
+
+func (self *mkvProcessor) ass2Pgs(input []string, resolution, frameRate int, fontsDir string, output string, lcb logCallback) bool {
+ return self.a2p && ass2Pgs(input, resolution, frameRate, fontsDir, output, lcb)
}
diff --git a/mkvlib/shared.go b/mkvlib/shared.go
index 8b8a3af..99f1b9a 100644
--- a/mkvlib/shared.go
+++ b/mkvlib/shared.go
@@ -11,7 +11,7 @@ import (
)
const libName = "mkvlib"
-const libVer = "v1.1.8"
+const libVer = "v1.1.9"
const LibFName = libName + " " + libVer
@@ -51,6 +51,7 @@ func (self *processorGetter) InitProcessorInstance(lcb logCallback) bool {
_, _pyftsubset := exec.LookPath(pyftsubset)
_, _mkvextract := exec.LookPath(mkvextract)
_, _mkvmerge := exec.LookPath(mkvmerge)
+ _, _spp2pgs := exec.LookPath(spp2pgs)
if _ttx != nil || _pyftsubset != nil {
printLog(lcb, `Missing dependency: fonttools (need "%s" & "%s").`, ttx, pyftsubset)
ec++
@@ -60,6 +61,11 @@ func (self *processorGetter) InitProcessorInstance(lcb logCallback) bool {
ec++
}
+ if runtime.GOOS == "windows" && runtime.GOARCH == "amd64" && _spp2pgs != nil {
+ printLog(lcb, `Missing dependency: spp2pgs.`, spp2pgs)
+ ec++
+ }
+
r := ec == 0
if r {
self.checked = true
diff --git a/mkvlib/spp2pgs.exe b/mkvlib/spp2pgs.exe
new file mode 100644
index 0000000..a07e6a8
--- /dev/null
+++ b/mkvlib/spp2pgs.exe
Binary files differ