1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +08:00

drop go-silk

This commit is contained in:
wdvxdr 2021-01-08 12:14:09 +08:00
parent 1843bd6a4e
commit 2d020bc7f7
5 changed files with 126 additions and 19 deletions

View File

@ -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()

102
global/codec/codec.go Normal file
View File

@ -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)
}

14
global/codec/codec2.go Normal file
View File

@ -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")
}

1
go.mod
View File

@ -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

2
go.sum
View File

@ -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=