优化切割逻辑,提前判断是否成功

This commit is contained in:
Jerry Yan 2025-04-13 14:41:29 +08:00
parent a478902f98
commit 37da5abad0
2 changed files with 70 additions and 14 deletions

View File

@ -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,

View File

@ -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
}