mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 19:43:49 +08:00
commit
9beae584de
@ -354,7 +354,7 @@ func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}, autoEscape b
|
|||||||
var str string
|
var str string
|
||||||
if m, ok := i.(gjson.Result); ok {
|
if m, ok := i.(gjson.Result); ok {
|
||||||
if m.Type == gjson.JSON {
|
if m.Type == gjson.JSON {
|
||||||
elem := bot.ConvertObjectMessage(m, true)
|
elem := bot.ConvertObjectMessage(m, false)
|
||||||
mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem})
|
mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem})
|
||||||
if mid == -1 {
|
if mid == -1 {
|
||||||
return Failed(100, "SEND_MSG_API_ERROR", "请参考输出")
|
return Failed(100, "SEND_MSG_API_ERROR", "请参考输出")
|
||||||
|
@ -895,9 +895,7 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
|
|||||||
}
|
}
|
||||||
hasCacheFile:
|
hasCacheFile:
|
||||||
if video {
|
if video {
|
||||||
return &LocalVideoElement{
|
return &LocalVideoElement{File: cacheFile}, nil
|
||||||
File: cacheFile,
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
return &LocalImageElement{File: cacheFile}, nil
|
return &LocalImageElement{File: cacheFile}, nil
|
||||||
}
|
}
|
||||||
@ -917,21 +915,36 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if video {
|
if video {
|
||||||
goto videos
|
|
||||||
}
|
|
||||||
if info.Size() == 0 || info.Size() >= maxImageSize {
|
|
||||||
return nil, errors.New("invalid image size")
|
|
||||||
}
|
|
||||||
return &LocalImageElement{File: fu.Path}, nil
|
|
||||||
videos:
|
|
||||||
if info.Size() == 0 || info.Size() >= maxVideoSize {
|
if info.Size() == 0 || info.Size() >= maxVideoSize {
|
||||||
return nil, errors.New("invalid video size")
|
return nil, errors.New("invalid video size")
|
||||||
}
|
}
|
||||||
return &LocalVideoElement{File: fu.Path}, nil
|
return &LocalVideoElement{File: fu.Path}, nil
|
||||||
}
|
}
|
||||||
|
if info.Size() == 0 || info.Size() >= maxImageSize {
|
||||||
|
return nil, errors.New("invalid image size")
|
||||||
|
}
|
||||||
|
return &LocalImageElement{File: fu.Path}, nil
|
||||||
|
}
|
||||||
rawPath := path.Join(global.IMAGE_PATH, f)
|
rawPath := path.Join(global.IMAGE_PATH, f)
|
||||||
if video {
|
if video {
|
||||||
goto video
|
rawPath = path.Join(global.VIDEO_PATH, f)
|
||||||
|
if !global.PathExists(rawPath) {
|
||||||
|
return nil, errors.New("invalid video")
|
||||||
|
}
|
||||||
|
if path.Ext(rawPath) == ".video" {
|
||||||
|
b, _ := ioutil.ReadFile(rawPath)
|
||||||
|
r := binary.NewReader(b)
|
||||||
|
return &LocalVideoElement{ShortVideoElement: message.ShortVideoElement{ // todo 检查缓存是否有效
|
||||||
|
Md5: r.ReadBytes(16),
|
||||||
|
ThumbMd5: r.ReadBytes(16),
|
||||||
|
Size: r.ReadInt32(),
|
||||||
|
ThumbSize: r.ReadInt32(),
|
||||||
|
Name: r.ReadString(),
|
||||||
|
Uuid: r.ReadAvailable(),
|
||||||
|
}}, nil
|
||||||
|
} else {
|
||||||
|
return &LocalVideoElement{File: rawPath}, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(f, "base64") {
|
if strings.HasPrefix(f, "base64") {
|
||||||
b, err := base64.StdEncoding.DecodeString(strings.ReplaceAll(f, "base64://", ""))
|
b, err := base64.StdEncoding.DecodeString(strings.ReplaceAll(f, "base64://", ""))
|
||||||
@ -1012,25 +1025,6 @@ func (bot *CQBot) makeImageOrVideoElem(d map[string]string, video, group bool) (
|
|||||||
return rsp, nil
|
return rsp, nil
|
||||||
}
|
}
|
||||||
return nil, errors.New("invalid image")
|
return nil, errors.New("invalid image")
|
||||||
video:
|
|
||||||
rawPath = path.Join(global.VIDEO_PATH, f)
|
|
||||||
if !global.PathExists(rawPath) {
|
|
||||||
return nil, errors.New("invalid video")
|
|
||||||
}
|
|
||||||
if path.Ext(rawPath) == ".video" {
|
|
||||||
b, _ := ioutil.ReadFile(rawPath)
|
|
||||||
r := binary.NewReader(b)
|
|
||||||
return &LocalVideoElement{ShortVideoElement: message.ShortVideoElement{ // todo 检查缓存是否有效
|
|
||||||
Md5: r.ReadBytes(16),
|
|
||||||
ThumbMd5: r.ReadBytes(16),
|
|
||||||
Size: r.ReadInt32(),
|
|
||||||
ThumbSize: r.ReadInt32(),
|
|
||||||
Name: r.ReadString(),
|
|
||||||
Uuid: r.ReadAvailable(),
|
|
||||||
}}, nil
|
|
||||||
} else {
|
|
||||||
return &LocalVideoElement{File: rawPath}, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//makeShowPic 一种xml 方式发送的群消息图片
|
//makeShowPic 一种xml 方式发送的群消息图片
|
||||||
|
@ -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
|
||||||
|
@ -13,17 +13,13 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
const (
|
||||||
codecDir string
|
silkCachePath = "data/cache"
|
||||||
encoderPath string
|
encoderPath = "codec"
|
||||||
cachePath string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
@ -32,40 +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 {
|
||||||
appPath, err := os.Executable()
|
encoderFile := path.Join(encoderPath, runtime.GOOS+"-"+runtime.GOARCH+"-encoder")
|
||||||
appPath = path.Dir(appPath)
|
if runtime.GOOS == "windows" {
|
||||||
if err != nil {
|
encoderFile = encoderFile + ".exe"
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
cachePath = path.Join(appPath, cachePath)
|
return encoderFile
|
||||||
codecDir = path.Join(appPath, codecPath)
|
}
|
||||||
if !fileExist(codecDir) {
|
|
||||||
_ = os.MkdirAll(codecDir, os.ModePerm)
|
func Init() error {
|
||||||
|
if !fileExist(silkCachePath) {
|
||||||
|
_ = os.MkdirAll(silkCachePath, 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 !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
|
||||||
@ -73,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
|
||||||
@ -81,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
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user