瀏覽代碼

fix: audio mutli time (#145)

zijiren 8 月之前
父節點
當前提交
14fed79510
共有 1 個文件被更改,包括 13 次插入1 次删除
  1. 13 1
      core/common/audio/audio.go

+ 13 - 1
core/common/audio/audio.go

@@ -10,6 +10,7 @@ import (
 	"strings"
 
 	"github.com/labring/aiproxy/core/common/config"
+	log "github.com/sirupsen/logrus"
 )
 
 var (
@@ -75,6 +76,8 @@ func getAudioDurationFallback(audio io.Reader) (float64, error) {
 		return 0, err
 	}
 
+	log.Debugf("ffmpeg -i - -f null -\n%s", stderr.Bytes())
+
 	// Parse the time from ffmpeg output
 	// Example: size=N/A time=00:00:05.52 bitrate=N/A speed= 785x
 	return parseTimeFromFfmpegOutput(stderr.String())
@@ -129,13 +132,22 @@ func getAudioDurationFromFilePathFallback(filePath string) (float64, error) {
 		return 0, err
 	}
 
+	log.Debugf("ffmpeg -i %s -f null -\n%s", filePath, stderr.Bytes())
+
 	// Parse the time from ffmpeg output
 	return parseTimeFromFfmpegOutput(stderr.String())
 }
 
 // parseTimeFromFfmpegOutput extracts and converts time from ffmpeg output to seconds
 func parseTimeFromFfmpegOutput(output string) (float64, error) {
-	match := re.FindStringSubmatch(output)
+	// Find all matches of time pattern
+	matches := re.FindAllStringSubmatch(output, -1)
+	if len(matches) == 0 {
+		return 0, ErrAudioDurationNAN
+	}
+
+	// Get the last time match (as per the instruction)
+	match := matches[len(matches)-1]
 	if len(match) < 2 {
 		return 0, ErrAudioDurationNAN
 	}