1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 19:43:49 +08:00
This commit is contained in:
wdvxdr 2021-01-13 22:56:28 +08:00
parent 7a9a021c54
commit 59cd986050
4 changed files with 27 additions and 26 deletions

View File

@ -15,7 +15,7 @@ var useSilkCodec = true
func InitCodec() { func InitCodec() {
log.Info("正在加载silk编码器...") log.Info("正在加载silk编码器...")
err := codec.Init("data/cache", "codec") err := codec.Init()
if err != nil { if err != nil {
log.Error(err) log.Error(err)
useSilkCodec = false useSilkCodec = false

View File

@ -13,16 +13,13 @@ import (
"runtime" "runtime"
) )
var ( const (
encoderPath string silkCachePath = "data/cache"
cachePath string encoderPath = "codec"
) )
func downloadCodec(url string, path string) (err error) { func downloadCodec(url string) (err error) {
resp, err := http.Get(url) resp, err := http.Get(url)
if runtime.GOOS == "windows" {
path = path + ".exe"
}
if err != nil { if err != nil {
return return
} }
@ -31,33 +28,37 @@ func downloadCodec(url string, path string) (err error) {
if err != nil { if err != nil {
return return
} }
err = ioutil.WriteFile(path, body, os.ModePerm) err = ioutil.WriteFile(getEncoderFilePath(), body, os.ModePerm)
return return
} }
func Init(cachePath, codecPath string) error { func getEncoderFilePath() string {
if !fileExist(codecPath) { encoderFile := path.Join(encoderPath, runtime.GOOS+"-"+runtime.GOARCH+"-encoder")
_ = os.MkdirAll(codecPath, os.ModePerm) if runtime.GOOS == "windows" {
encoderFile = encoderFile + ".exe"
} }
if !fileExist(cachePath) { return encoderFile
_ = os.MkdirAll(cachePath, os.ModePerm) }
func Init() error {
if !fileExist(silkCachePath) {
_ = os.MkdirAll(silkCachePath, os.ModePerm)
} }
encoderFile := runtime.GOOS + "-" + runtime.GOARCH + "-encoder"
encoderPath = path.Join(codecPath, encoderFile)
if !fileExist(encoderPath) { if !fileExist(encoderPath) {
if err := downloadCodec("https://cdn.jsdelivr.net/gh/wdvxdr1123/tosilk/codec/"+encoderFile, encoderPath); err != nil { _ = os.MkdirAll(encoderPath, os.ModePerm)
}
p := getEncoderFilePath()
if !fileExist(p) {
if err := downloadCodec("https://cdn.jsdelivr.net/gh/wdvxdr1123/tosilk/codec/" + runtime.GOOS + "-" + runtime.GOARCH + "-encoder"); err != nil {
return errors.New("下载依赖失败") return errors.New("下载依赖失败")
} }
} }
if runtime.GOOS == "windows" {
encoderPath = encoderPath + ".exe"
}
return nil return nil
} }
func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) { func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error) {
// 1. 写入缓存文件 // 1. 写入缓存文件
rawPath := path.Join(cachePath, tempName+".wav") rawPath := path.Join(silkCachePath, tempName+".wav")
err := ioutil.WriteFile(rawPath, record, os.ModePerm) err := ioutil.WriteFile(rawPath, record, os.ModePerm)
if err != nil { if err != nil {
return nil, err return nil, err
@ -65,7 +66,7 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error)
defer os.Remove(rawPath) defer os.Remove(rawPath)
// 2.转换pcm // 2.转换pcm
pcmPath := path.Join(cachePath, tempName+".pcm") pcmPath := path.Join(silkCachePath, tempName+".pcm")
cmd := exec.Command("ffmpeg", "-i", rawPath, "-f", "s16le", "-ar", "24000", "-ac", "1", pcmPath) cmd := exec.Command("ffmpeg", "-i", rawPath, "-f", "s16le", "-ar", "24000", "-ac", "1", pcmPath)
if err = cmd.Run(); err != nil { if err = cmd.Run(); err != nil {
return nil, err return nil, err
@ -73,8 +74,8 @@ func EncodeToSilk(record []byte, tempName string, useCache bool) ([]byte, error)
defer os.Remove(pcmPath) defer os.Remove(pcmPath)
// 3. 转silk // 3. 转silk
silkPath := path.Join(cachePath, tempName+".silk") silkPath := path.Join(silkCachePath, tempName+".silk")
cmd = exec.Command(encoderPath, pcmPath, silkPath, "-rate", "24000", "-quiet", "-tencent") cmd = exec.Command(getEncoderFilePath(), pcmPath, silkPath, "-rate", "24000", "-quiet", "-tencent")
if err = cmd.Run(); err != nil { if err = cmd.Run(); err != nil {
return nil, err return nil, err
} }

View File

@ -4,7 +4,7 @@ package codec
import "errors" import "errors"
func Init(cachePath, codecPath string) error { func Init() error {
return errors.New("Unsupport arch now") return errors.New("Unsupport arch now")
} }

View File

@ -4,7 +4,7 @@ package codec
import "errors" import "errors"
func Init(cachePath, codecPath string) error { func Init() error {
return errors.New("not support now") return errors.New("not support now")
} }