|
|
@@ -6,6 +6,7 @@ import (
|
|
|
"bytes"
|
|
|
"encoding/json"
|
|
|
"errors"
|
|
|
+ "fmt"
|
|
|
"github.com/google/uuid"
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
"io"
|
|
|
@@ -74,6 +75,7 @@ func AutoTranscoder(ctx *svc.ServiceContext) {
|
|
|
|
|
|
// Transcoder 转码
|
|
|
func Transcoder(svcCtx *svc.ServiceContext, req *types.CallbackRequest) (resp *types.OriginDataResponse, err error) {
|
|
|
+ fmt.Println("转码中...")
|
|
|
if req.Path == "" {
|
|
|
err = StopTranscoderError
|
|
|
return
|
|
|
@@ -85,26 +87,26 @@ func Transcoder(svcCtx *svc.ServiceContext, req *types.CallbackRequest) (resp *t
|
|
|
// 发起HTTP GET请求
|
|
|
resp1, err := http.Get(req.Path)
|
|
|
if err != nil {
|
|
|
- logx.Errorf("下载文件失败 error:", err.Error())
|
|
|
+ logx.Errorf("下载文件失败 error:%s", err.Error())
|
|
|
return
|
|
|
}
|
|
|
defer resp1.Body.Close()
|
|
|
if resp1.StatusCode != http.StatusOK {
|
|
|
err = errors.New("download file failed")
|
|
|
- logx.Errorf("下载文件失败 error:", resp1.StatusCode)
|
|
|
+ logx.Errorf("下载文件失败 error:%v", resp1.StatusCode)
|
|
|
return
|
|
|
}
|
|
|
// 读取响应体内容
|
|
|
inputPath := uuid.NewString() // 本地保存的文件路径
|
|
|
inputFile, err := os.Create(inputPath)
|
|
|
if err != nil {
|
|
|
- logx.Errorf("读取文件失败 error:", err.Error())
|
|
|
+ logx.Errorf("读取文件失败 error:%s", err.Error())
|
|
|
return
|
|
|
}
|
|
|
_, err = io.Copy(inputFile, resp1.Body)
|
|
|
inputFile.Close()
|
|
|
if err != nil {
|
|
|
- logx.Errorf("读取文件失败 error:", err.Error())
|
|
|
+ logx.Errorf("读取文件失败 error:%s", err.Error())
|
|
|
return
|
|
|
}
|
|
|
//判断文件类型,如果不是指定语音则返回
|
|
|
@@ -135,24 +137,8 @@ func Transcoder(svcCtx *svc.ServiceContext, req *types.CallbackRequest) (resp *t
|
|
|
} else if inputType == 2 {
|
|
|
wavPath, pcmPath = silk.TransMp3ToWav(inputPath)
|
|
|
}
|
|
|
- wavFile, err := os.Open(wavPath)
|
|
|
- if err != nil {
|
|
|
- logx.Errorf("读取wav文件失败 error:", err.Error())
|
|
|
- return
|
|
|
- }
|
|
|
- defer func() {
|
|
|
- wavFile.Close()
|
|
|
- silk.FileRemove(wavPath)
|
|
|
- }()
|
|
|
- pcmFile, err := os.Open(pcmPath)
|
|
|
- if err != nil {
|
|
|
- logx.Errorf("读取pcm文件失败 error:", err.Error())
|
|
|
- return
|
|
|
- }
|
|
|
- defer func() {
|
|
|
- pcmFile.Close()
|
|
|
- silk.FileRemove(pcmPath)
|
|
|
- }()
|
|
|
+ defer silk.FileRemove(wavPath)
|
|
|
+ defer silk.FileRemove(pcmPath)
|
|
|
//多线程处理上传录音和语音识别
|
|
|
var wg sync.WaitGroup
|
|
|
wg.Add(2)
|
|
|
@@ -161,10 +147,9 @@ func Transcoder(svcCtx *svc.ServiceContext, req *types.CallbackRequest) (resp *t
|
|
|
//上传录音
|
|
|
go func() {
|
|
|
defer wg.Done()
|
|
|
- bts, _ := io.ReadAll(wavFile)
|
|
|
- url, err := svcCtx.QiNiuSdk.Upload(bts, int64(len(bts)), wavPath)
|
|
|
+ url, err := svcCtx.QiNiuSdk.Upload(wavPath, wavPath)
|
|
|
if err != nil {
|
|
|
- logx.Errorf("上传七牛云失败 error:", err.Error())
|
|
|
+ logx.Errorf("上传七牛云失败 error:%s", err.Error())
|
|
|
return
|
|
|
}
|
|
|
err1 = err
|
|
|
@@ -173,10 +158,10 @@ func Transcoder(svcCtx *svc.ServiceContext, req *types.CallbackRequest) (resp *t
|
|
|
//识别录音
|
|
|
go func() {
|
|
|
defer wg.Done()
|
|
|
- bts, _ := io.ReadAll(pcmFile)
|
|
|
+ bts, _ := os.ReadFile(pcmPath)
|
|
|
asrResp, err := svcCtx.AsrSdk.Asr(bts, "pcm", 16000)
|
|
|
if err != nil {
|
|
|
- logx.Errorf("语音识别失败 error:", err.Error())
|
|
|
+ logx.Errorf("语音识别失败 error:%s", err.Error())
|
|
|
return
|
|
|
}
|
|
|
err2 = err
|
|
|
@@ -190,5 +175,6 @@ func Transcoder(svcCtx *svc.ServiceContext, req *types.CallbackRequest) (resp *t
|
|
|
if err2 != nil {
|
|
|
return nil, err2
|
|
|
}
|
|
|
+ fmt.Println("已转码...")
|
|
|
return
|
|
|
}
|