From 2d020bc7f72ff3ee95e827e641a9dde1cb86a164 Mon Sep 17 00:00:00 2001 From: wdvxdr Date: Fri, 8 Jan 2021 12:14:09 +0800 Subject: [PATCH] drop go-silk --- global/codec.go | 26 ++++------- global/codec/codec.go | 102 +++++++++++++++++++++++++++++++++++++++++ global/codec/codec2.go | 14 ++++++ go.mod | 1 - go.sum | 2 - 5 files changed, 126 insertions(+), 19 deletions(-) create mode 100644 global/codec/codec.go create mode 100644 global/codec/codec2.go diff --git a/global/codec.go b/global/codec.go index bc9b9e0..90d51b4 100644 --- a/global/codec.go +++ b/global/codec.go @@ -4,31 +4,25 @@ import ( "crypto/md5" "errors" "fmt" + "github.com/Mrs4s/go-cqhttp/global/codec" + log "github.com/sirupsen/logrus" "io/ioutil" "path" - "sync" - - log "github.com/sirupsen/logrus" - "github.com/wdvxdr1123/go-silk/silk" ) -var codec silk.Encoder -var useCodec = true -var once sync.Once +var useSilkCodec = true func InitCodec() { - once.Do(func() { - log.Info("正在加载silk编码器...") - err := codec.Init("data/cache", "codec") - if err != nil { - log.Error(err) - useCodec = false - } - }) + log.Info("正在加载silk编码器...") + err := codec.Init("data/cache", "codec") + if err != nil { + log.Error(err) + useSilkCodec = false + } } func Encoder(data []byte) ([]byte, error) { - if useCodec == false { + if useSilkCodec == false { return nil, errors.New("no silk encoder") } h := md5.New() diff --git a/global/codec/codec.go b/global/codec/codec.go new file mode 100644 index 0000000..5f7d2d4 --- /dev/null +++ b/global/codec/codec.go @@ -0,0 +1,102 @@ +// +build linux windows darwin +// +build 386 amd64 arm + +package codec + +import ( + "errors" + "io/ioutil" + "net/http" + "os" + "os/exec" + "path" + "runtime" +) + +var ( + codecDir string + encoderPath string + cachePath string +) + +func downloadCodec(url string, path string) (err error) { + resp, err := http.Get(url) + if runtime.GOOS == "windows" { + path = path + ".exe" + } + if err != nil { + return + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return + } + err = ioutil.WriteFile(path, body, os.ModePerm) + return +} + +func Init(cachePath, codecPath string) error { + appPath, err := os.Executable() + appPath = path.Dir(appPath) + if err != nil { + return err + } + cachePath = path.Join(appPath, cachePath) + codecDir = path.Join(appPath, codecPath) + if !fileExist(codecDir) { + _ = os.MkdirAll(codecDir, os.ModePerm) + } + if !fileExist(cachePath) { + _ = os.MkdirAll(cachePath, os.ModePerm) + } + encoderFile := runtime.GOOS + "-" + runtime.GOARCH + "-encoder" + encoderPath = path.Join(codecDir, encoderFile) + if !fileExist(encoderPath) { + if err = downloadCodec("https://cdn.jsdelivr.net/gh/wdvxdr1123/tosilk/codec/"+encoderFile, encoderPath); err != nil { + return errors.New("下载依赖失败") + } + } + if runtime.GOOS == "windows" { + encoderPath = encoderPath + ".exe" + } + return nil +} + +func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) { + // 1. 写入缓存文件 + rawPath := path.Join(cachePath, tempName+".wav") + err := ioutil.WriteFile(rawPath, record, os.ModePerm) + if err != nil { + return nil, err + } + defer os.Remove(rawPath) + + // 2.转换pcm + pcmPath := path.Join(cachePath, tempName+".pcm") + cmd := exec.Command("ffmpeg", "-i", rawPath, "-f", "s16le", "-ar", "24000", "-ac", "1", pcmPath) + if err = cmd.Run(); err != nil { + return nil, err + } + defer os.Remove(pcmPath) + + // 3. 转silk + silkPath := path.Join(cachePath, tempName+".silk") + cmd = exec.Command(encoderPath, pcmPath, silkPath, "-rate", "24000", "-quiet", "-tencent") + if err = cmd.Run(); err != nil { + return nil, err + } + if useCache == false { + defer os.Remove(silkPath) + } + return ioutil.ReadFile(silkPath) +} + +// FileExist 检查文件是否存在 +func fileExist(path string) bool { + if runtime.GOOS == "windows" { + path = path + ".exe" + } + _, err := os.Lstat(path) + return !os.IsNotExist(err) +} diff --git a/global/codec/codec2.go b/global/codec/codec2.go new file mode 100644 index 0000000..e50133e --- /dev/null +++ b/global/codec/codec2.go @@ -0,0 +1,14 @@ +// +build !linux,!windows,!darwin +// +build !386,!amd64,!arm + +package codec + +import "errors" + +func Init() error { + return errors.New("not support now") +} + +func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) { + return nil, errors.New("not support now") +} diff --git a/go.mod b/go.mod index ea327d8..c2043a8 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,6 @@ require ( github.com/syndtr/goleveldb v1.0.0 github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816 github.com/tidwall/gjson v1.6.7 - github.com/wdvxdr1123/go-silk v0.0.0-20201210140933-bcdbcb2f1093 github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189 golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 diff --git a/go.sum b/go.sum index 2f1571a..3554dd5 100644 --- a/go.sum +++ b/go.sum @@ -110,8 +110,6 @@ github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= -github.com/wdvxdr1123/go-silk v0.0.0-20201210140933-bcdbcb2f1093 h1:t38EBwI2hFJz1sQJ402VqzdA3eMidkY1c3w/8z0SftA= -github.com/wdvxdr1123/go-silk v0.0.0-20201210140933-bcdbcb2f1093/go.mod h1:5q9LFlBr+yX/J8Jd/9wHdXwkkjFkNyQIS7kX2Lgx/Zs= github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189 h1:4UJw9if55Fu3HOwbfcaQlJ27p3oeJU2JZqoeT3ITJQk= github.com/yinghau76/go-ascii-art v0.0.0-20190517192627-e7f465a30189/go.mod h1:rIrm5geMiBhPQkdfUm8gDFi/WiHneOp1i9KjmJqc+9I= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=