transcoderlogic.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package logic
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/google/uuid"
  6. "io"
  7. "net/http"
  8. "os"
  9. "silk2audio/silk"
  10. "strings"
  11. "sync"
  12. "audio_transcoder/internal/svc"
  13. "audio_transcoder/internal/types"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type TranscoderLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewTranscoderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TranscoderLogic {
  22. return &TranscoderLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx,
  26. }
  27. }
  28. func (l *TranscoderLogic) Transcoder(req *types.Request) (resp *types.Response, err error) {
  29. if req.Path == "" {
  30. err = errors.New("path is empty")
  31. return
  32. }
  33. // 发起HTTP GET请求
  34. resp1, err := http.Get(req.Path)
  35. if err != nil {
  36. logx.Errorf("下载文件失败 error:", err.Error())
  37. return
  38. }
  39. defer resp1.Body.Close()
  40. if resp1.StatusCode != http.StatusOK {
  41. err = errors.New("download file failed")
  42. logx.Errorf("下载文件失败 error:", resp1.StatusCode)
  43. return
  44. }
  45. // 读取响应体内容
  46. silkPath := uuid.NewString() + ".silk" // 本地保存的文件路径
  47. silkFile, err := os.Create(silkPath)
  48. if err != nil {
  49. logx.Errorf("读取silk文件失败 error:", err.Error())
  50. return
  51. }
  52. defer func() {
  53. silkFile.Close()
  54. silk.FileRemove(silkPath)
  55. }()
  56. _, err = io.Copy(silkFile, resp1.Body)
  57. if err != nil {
  58. logx.Errorf("读取文件失败 error:", err.Error())
  59. return
  60. }
  61. // 转码
  62. wavPath, pcmPath := silk.TransSilkToWav(silkPath)
  63. wavFile, err := os.Open(wavPath)
  64. if err != nil {
  65. logx.Errorf("读取wav文件失败 error:", err.Error())
  66. return
  67. }
  68. defer func() {
  69. wavFile.Close()
  70. silk.FileRemove(wavPath)
  71. }()
  72. pcmFile, err := os.Open(pcmPath)
  73. if err != nil {
  74. logx.Errorf("读取pcm文件失败 error:", err.Error())
  75. return
  76. }
  77. defer func() {
  78. pcmFile.Close()
  79. silk.FileRemove(pcmPath)
  80. }()
  81. //多线程处理上传录音和语音识别
  82. var wg sync.WaitGroup
  83. wg.Add(2)
  84. //构建返回数据
  85. resp = &types.Response{}
  86. var err1, err2 error
  87. //上传录音
  88. go func() {
  89. defer wg.Done()
  90. bts, _ := io.ReadAll(wavFile)
  91. url, err := l.svcCtx.QiNiuSdk.Upload(bts, int64(len(bts)), wavPath)
  92. if err != nil {
  93. logx.Errorf("上传七牛云失败 error:", err.Error())
  94. return
  95. }
  96. err1 = err
  97. resp.Path = url
  98. }()
  99. //识别录音
  100. go func() {
  101. defer wg.Done()
  102. bts, _ := io.ReadAll(pcmFile)
  103. asrResp, err := l.svcCtx.AsrSdk.Asr(bts, "pcm", 16000)
  104. if err != nil {
  105. logx.Errorf("语音识别失败 error:", err.Error())
  106. return
  107. }
  108. err2 = err
  109. resp.Message = strings.Join(asrResp.Result, "\n")
  110. }()
  111. wg.Wait()
  112. //返回数据
  113. if err1 != nil {
  114. return nil, err1
  115. }
  116. if err2 != nil {
  117. return nil, err2
  118. }
  119. return
  120. }