This commit is contained in:
2025-08-04 10:49:24 +08:00
parent 84ccaa56de
commit 4b1eb11986
11 changed files with 194 additions and 72 deletions

View File

@@ -2,12 +2,13 @@ package util
import (
"ZhenTuLocalPassiveAdapter/dto"
"ZhenTuLocalPassiveAdapter/logger"
"bytes"
"context"
"fmt"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"log"
"go.uber.org/zap"
"math/rand"
"os"
"os/exec"
@@ -36,7 +37,7 @@ func RunFfmpegTask(ctx context.Context, task *dto.FfmpegTask) bool {
}
// 先尝试方法1
if !result {
log.Printf("FFMPEG简易方法失败,尝试复杂方法转码")
logger.Warn("FFMPEG简易方法失败,尝试复杂方法转码")
// 不行再尝试方法二
result = runFfmpegForMultipleFile2(subCtx, task)
}
@@ -80,7 +81,7 @@ func runFfmpegForMultipleFile1(ctx context.Context, task *dto.FfmpegTask) bool {
tmpFile := path.Join(os.TempDir(), file.Name+strconv.Itoa(rand.Int())+".ts")
result, err := convertMp4ToTs(subCtx, *file, tmpFile)
if err != nil {
log.Printf("转码出错: %v", err)
logger.Error("转码出错", zap.Error(err))
mu.Lock()
notOk = true
mu.Unlock()
@@ -115,7 +116,7 @@ func runFfmpegForMultipleFile1(ctx context.Context, task *dto.FfmpegTask) bool {
// 步骤三:删除临时文件
for _, file := range taskClone.Files {
if err := os.Remove(file.Url); err != nil {
log.Printf("删除临时文件失败: %v", err)
logger.Error("删除临时文件失败", zap.Error(err))
}
}
if result {
@@ -185,7 +186,7 @@ func runFfmpegForSingleFile(ctx context.Context, task *dto.FfmpegTask) bool {
stat, err := os.Stat(task.OutputFile)
if err != nil {
span.SetStatus(codes.Error, "文件不存在")
log.Printf("文件不存在:%s", task.OutputFile)
logger.Error("文件不存在", zap.String("outputFile", task.OutputFile))
return false
}
span.SetAttributes(attribute.String("file.name", task.OutputFile))
@@ -219,7 +220,10 @@ func CheckFileCoverageAndConstructTask(ctx context.Context, fileList []dto.File,
defer span.End()
if fileList == nil || len(fileList) == 0 {
span.SetStatus(codes.Error, "无法根据要求找到对应录制片段")
log.Printf("无法根据要求找到对应录制片段!ID:【%s】,开始时间:【%s】,结束时间:【%s】", task.TaskID, beginDt, endDt)
logger.Error("无法根据要求找到对应录制片段",
zap.String("taskID", task.TaskID),
zap.String("beginTime", beginDt.Format("2006-01-02 15:04:05")),
zap.String("endTime", endDt.Format("2006-01-02 15:04:05")))
return nil, fmt.Errorf("无法根据要求找到对应录制片段")
}
// 按照 Create 的值升序排序
@@ -238,7 +242,11 @@ func CheckFileCoverageAndConstructTask(ctx context.Context, fileList []dto.File,
if file.StartTime.Sub(lastFile.EndTime).Seconds() > 2 {
// 片段断开
span.SetStatus(codes.Error, "FFMPEG片段断开")
log.Printf("分析FFMPEG任务失败:ID:【%s】,文件片段:【%s,%s】中间断开【%f】秒(超过2秒)", task.TaskID, lastFile.Name, file.Name, file.StartTime.Sub(lastFile.EndTime).Seconds())
logger.Error("分析FFMPEG任务失败,文件片段中间断开超过2秒",
zap.String("taskID", task.TaskID),
zap.String("lastFile", lastFile.Name),
zap.String("currentFile", file.Name),
zap.Float64("gapSeconds", file.StartTime.Sub(lastFile.EndTime).Seconds()))
return nil, fmt.Errorf("片段断开")
}
lastFile = &file
@@ -248,7 +256,10 @@ func CheckFileCoverageAndConstructTask(ctx context.Context, fileList []dto.File,
// 通过文件列表构造的任务仍然是缺失的
if fileList[len(fileList)-1].EndTime.Before(endDt) {
span.SetStatus(codes.Error, "FFMPEG片段断开")
log.Printf("分析FFMPEG任务失败:ID:【%s】,文件片段:【%s】,无法完整覆盖时间点【%s】", task.TaskID, fileList[len(fileList)-1].Name, endDt)
logger.Error("分析FFMPEG任务失败,无法完整覆盖时间点",
zap.String("taskID", task.TaskID),
zap.String("lastFile", fileList[len(fileList)-1].Name),
zap.String("endTime", endDt.Format("2006-01-02 15:04:05")))
return nil, fmt.Errorf("片段断开")
}
@@ -326,7 +337,7 @@ func QuickConcatVideoCut(ctx context.Context, inputFiles []dto.File, offset, len
if err != nil {
span.SetAttributes(attribute.String("error", err.Error()))
span.SetStatus(codes.Error, "创建临时文件失败")
log.Printf("创建临时文件失败:%s", tmpFile)
logger.Error("创建临时文件失败", zap.String("tmpFile", tmpFile))
return false, err
}
defer os.Remove(tmpFile)
@@ -337,7 +348,7 @@ func QuickConcatVideoCut(ctx context.Context, inputFiles []dto.File, offset, len
if err != nil {
span.SetAttributes(attribute.String("error", err.Error()))
span.SetStatus(codes.Error, "写入临时文件失败")
log.Printf("写入临时文件失败:%s", tmpFile)
logger.Error("写入临时文件失败", zap.String("tmpFile", tmpFile))
return false, err
}
}
@@ -401,7 +412,7 @@ func handleFfmpegProcess(ctx context.Context, ffmpegCmd []string) (bool, error)
defer func() {
span.SetAttributes(attribute.Int64("ffmpeg.duration", int64(time.Since(startTime).Seconds())))
}()
log.Printf("FFMPEG执行命令:【%s】", strings.Join(ffmpegCmd, " "))
logger.Info("FFMPEG执行命令", zap.String("command", strings.Join(ffmpegCmd, " ")))
cmd := exec.Command(ffmpegCmd[0], ffmpegCmd[1:]...)
var stderr bytes.Buffer
@@ -411,7 +422,9 @@ func handleFfmpegProcess(ctx context.Context, ffmpegCmd []string) (bool, error)
if err != nil {
span.SetAttributes(attribute.String("ffmpeg.stderr", stderr.String()))
span.SetStatus(codes.Error, "FFMPEG执行命令失败")
log.Printf("FFMPEG执行命令失败,错误信息:%s,命令:【%s】", stderr.String(), strings.Join(ffmpegCmd, " "))
logger.Error("FFMPEG执行命令失败",
zap.String("error", stderr.String()),
zap.String("command", strings.Join(ffmpegCmd, " ")))
return false, err
}
defer cmd.Process.Kill()
@@ -425,17 +438,21 @@ func handleFfmpegProcess(ctx context.Context, ffmpegCmd []string) (bool, error)
case <-time.After(1 * time.Minute):
span.SetAttributes(attribute.String("ffmpeg.stderr", stderr.String()))
span.SetStatus(codes.Error, "FFMPEG执行命令没有在1分钟内退出")
log.Printf("FFMPEG执行命令没有在1分钟内退出,命令:【%s】", strings.Join(ffmpegCmd, " "))
logger.Warn("FFMPEG执行命令超时", zap.String("command", strings.Join(ffmpegCmd, " ")))
return false, fmt.Errorf("ffmpeg command timed out")
case err := <-done:
if err != nil {
span.SetAttributes(attribute.String("ffmpeg.stderr", stderr.String()))
span.SetStatus(codes.Error, "FFMPEG执行命令失败")
log.Printf("FFMPEG执行命令失败,错误信息:%s,命令:【%s】", stderr.String(), strings.Join(ffmpegCmd, " "))
logger.Error("FFMPEG执行命令失败",
zap.String("error", stderr.String()),
zap.String("command", strings.Join(ffmpegCmd, " ")))
return false, err
}
endTime := time.Now()
log.Printf("FFMPEG执行命令结束,耗费时间:【%dms】,命令:【%s】", endTime.Sub(startTime).Milliseconds(), strings.Join(ffmpegCmd, " "))
logger.Info("FFMPEG执行命令结束",
zap.Int64("durationMs", endTime.Sub(startTime).Milliseconds()),
zap.String("command", strings.Join(ffmpegCmd, " ")))
return true, nil
}
}