Kaynağa Gözat

fix:音频时长

Roc 1 ay önce
ebeveyn
işleme
761359ad11
2 değiştirilmiş dosya ile 33 ekleme ve 12 silme
  1. 14 12
      controllers/report_chapter.go
  2. 19 0
      utils/common.go

+ 14 - 12
controllers/report_chapter.go

@@ -8,8 +8,6 @@ import (
 	"eta/eta_mobile/services"
 	"eta/eta_mobile/services/data"
 	"eta/eta_mobile/utils"
-	"fmt"
-	"github.com/kgiannakakis/mp3duration/src/mp3duration"
 	"html"
 	"os"
 	"path"
@@ -1144,15 +1142,19 @@ func (this *ReportController) VoiceUpload() {
 		return
 	}
 
-	var playSeconds float64
-	playSeconds, err = mp3duration.Calculate(fPath)
-	if playSeconds <= 0 {
-		playSeconds, err = utils.GetVideoPlaySeconds(fPath)
-		if err != nil {
-			br.Msg = "获取音频时间失败"
-			br.ErrMsg = "获取音频时间失败,Err:" + err.Error()
-			return
-		}
+	//var playSeconds float64
+	//playSeconds, err = mp3duration.Calculate(fPath)
+	//if playSeconds <= 0 {
+	//	playSeconds, err = utils.GetVideoPlaySeconds(fPath)
+	//	if err != nil {
+	//		br.Msg = "获取音频时间失败"
+	//		br.ErrMsg = "获取音频时间失败,Err:" + err.Error()
+	//		return
+	//	}
+	//}
+	playSecondsStr, err := utils.GetDuration(fPath) //mp3duration.Calculate(fPath)
+	if err != nil {
+		utils.FileLog.Info("获取音频时间失败,Err:" + err.Error())
 	}
 
 	fileBody, err := os.ReadFile(fPath)
@@ -1172,7 +1174,7 @@ func (this *ReportController) VoiceUpload() {
 
 		reportChapterInfo.VideoUrl = resourceUrl
 		reportChapterInfo.VideoName = videoName
-		reportChapterInfo.VideoPlaySeconds = fmt.Sprint(playSeconds)
+		reportChapterInfo.VideoPlaySeconds = playSecondsStr
 		reportChapterInfo.VideoSize = sizeStr
 		reportChapterInfo.VideoKind = 1
 		reportChapterInfo.LastModifyAdminId = this.SysUser.AdminId

+ 19 - 0
utils/common.go

@@ -2543,3 +2543,22 @@ func GetCurrentTime() string {
 func IsAdminRole(roleTypeCode string) bool {
 	return roleTypeCode == ROLE_TYPE_CODE_ADMIN
 }
+
+func GetDuration(filePath string) (duration string, err error) {
+	// 构建 FFmpeg 命令,使用 ffprobe 来获取媒体文件信息
+	cmd := exec.Command("ffprobe", "-i", filePath, "-show_entries", "format=duration", "-v", "quiet", "-of", "csv=p=0")
+	var out bytes.Buffer
+	cmd.Stdout = &out
+
+	// 执行命令并捕获输出
+	err = cmd.Run()
+	if err != nil {
+		return "", err
+	}
+
+	// 使用正则表达式匹配输出中的时长信息
+	re := regexp.MustCompile(`\d+\.\d+`)
+	duration = re.FindString(out.String())
+
+	return duration, nil
+}