From 37da5abad066a3e2a99ac309b7d8d07ed3491bdd Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Sun, 13 Apr 2025 14:41:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=88=87=E5=89=B2=E9=80=BB?= =?UTF-8?q?=E8=BE=91=EF=BC=8C=E6=8F=90=E5=89=8D=E5=88=A4=E6=96=AD=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/task.go | 14 ---------- util/ffmpeg.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 14 deletions(-) diff --git a/core/task.go b/core/task.go index 0dd908f..87595d0 100644 --- a/core/task.go +++ b/core/task.go @@ -11,7 +11,6 @@ import ( "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/codes" "log" - "os" "path" ) @@ -55,19 +54,6 @@ func HandleTask(ctx context.Context, device config.DeviceMapping, task dto.Task) span.SetStatus(codes.Error, "ffmpeg任务执行失败") return nil, fmt.Errorf("ffmpeg任务执行失败") } - outfile, err := os.Stat(constructTask.OutputFile) - if err != nil { - span.SetAttributes(attribute.String("error", err.Error())) - span.SetStatus(codes.Error, "文件不存在") - return nil, fmt.Errorf("文件不存在:%s", constructTask.OutputFile) - } - span.SetAttributes(attribute.String("file.name", outfile.Name())) - span.SetAttributes(attribute.Int64("file.size", outfile.Size())) - if outfile.Size() < 4096 { - span.SetAttributes(attribute.String("error", "文件大小过小")) - span.SetStatus(codes.Error, "文件大小过小") - return nil, fmt.Errorf("文件大小过小:%s", constructTask.OutputFile) - } return &dto.FileObject{ CreateTime: task.EndTime, EndTime: task.EndTime, diff --git a/util/ffmpeg.go b/util/ffmpeg.go index 7e6733e..08603b9 100644 --- a/util/ffmpeg.go +++ b/util/ffmpeg.go @@ -20,6 +20,8 @@ import ( ) const FfmpegExec = "ffmpeg" +const minimalSize = 8192 +const minimalLengthRatio = 0.5 func RunFfmpegTask(ctx context.Context, task *dto.FfmpegTask) bool { subCtx, span := tracer.Start(ctx, "RunFfmpegTask") @@ -40,6 +42,16 @@ func RunFfmpegTask(ctx context.Context, task *dto.FfmpegTask) bool { log.Printf("FFMPEG简易方法失败,尝试复杂方法转码") // 不行再尝试方法二 result = runFfmpegForMultipleFile2(subCtx, task) + duration, err := GetVideoDuration(subCtx, task.OutputFile) + if err != nil { + span.SetAttributes(attribute.String("error", err.Error())) + span.SetStatus(codes.Error, "获取视频时长失败") + return false + } + if duration < (minimalLengthRatio * float64(task.Length)) { + span.SetStatus(codes.Error, "视频长度不达标") + return false + } if result { span.SetStatus(codes.Ok, "FFMPEG复杂方法成功") return true @@ -102,6 +114,22 @@ func runFfmpegForMultipleFile1(ctx context.Context, task *dto.FfmpegTask) bool { log.Printf("删除临时文件失败: %v", err) } } + if result { + outfile, err := os.Stat(task.OutputFile) + if err != nil { + span.SetAttributes(attribute.String("error", err.Error())) + span.SetStatus(codes.Error, "文件不存在") + result = false + } else { + span.SetAttributes(attribute.String("file.name", outfile.Name())) + span.SetAttributes(attribute.Int64("file.size", outfile.Size())) + if outfile.Size() < minimalSize { + span.SetAttributes(attribute.String("error", "文件大小过小")) + span.SetStatus(codes.Error, "文件大小过小") + result = false + } + } + } if result { span.SetStatus(codes.Ok, "FFMPEG多文件concat协议转码成功") } else { @@ -118,6 +146,27 @@ func runFfmpegForMultipleFile2(ctx context.Context, task *dto.FfmpegTask) bool { if err != nil { return false } + if result { + outfile, err := os.Stat(task.OutputFile) + if err != nil { + span.SetAttributes(attribute.String("error", err.Error())) + span.SetStatus(codes.Error, "文件不存在") + result = false + } else { + span.SetAttributes(attribute.String("file.name", outfile.Name())) + span.SetAttributes(attribute.Int64("file.size", outfile.Size())) + if outfile.Size() < minimalSize { + span.SetAttributes(attribute.String("error", "文件大小过小")) + span.SetStatus(codes.Error, "文件大小过小") + result = false + } + } + } + if result { + span.SetStatus(codes.Ok, "FFMPEG多文件转码成功") + } else { + span.SetStatus(codes.Error, "FFMPEG多文件转码失败") + } return result } @@ -137,6 +186,27 @@ func runFfmpegForSingleFile(ctx context.Context, task *dto.FfmpegTask) bool { } span.SetAttributes(attribute.String("file.name", task.OutputFile)) span.SetAttributes(attribute.Int64("file.size", stat.Size())) + if result { + outfile, err := os.Stat(task.OutputFile) + if err != nil { + span.SetAttributes(attribute.String("error", err.Error())) + span.SetStatus(codes.Error, "文件不存在") + result = false + } else { + span.SetAttributes(attribute.String("file.name", outfile.Name())) + span.SetAttributes(attribute.Int64("file.size", outfile.Size())) + if outfile.Size() < minimalSize { + span.SetAttributes(attribute.String("error", "文件大小过小")) + span.SetStatus(codes.Error, "文件大小过小") + result = false + } + } + } + if result { + span.SetStatus(codes.Ok, "FFMPEG单个文件裁切成功") + } else { + span.SetStatus(codes.Error, "FFMPEG单个文件裁切失败") + } return result }