支持浮点偏移

This commit is contained in:
2025-04-13 15:23:58 +08:00
parent cdd1358d45
commit cf3c518d13
2 changed files with 19 additions and 19 deletions

View File

@ -105,7 +105,7 @@ func runFfmpegForMultipleFile1(ctx context.Context, task *dto.FfmpegTask) bool {
}
// 步骤二:使用concat协议拼接裁切
result, err := QuickConcatVideoCut(subCtx, taskClone.Files, int64(taskClone.Offset), int64(taskClone.Length), taskClone.OutputFile)
result, err := QuickConcatVideoCut(subCtx, taskClone.Files, taskClone.Offset, taskClone.Length, taskClone.OutputFile)
if err != nil {
span.SetAttributes(attribute.String("error", err.Error()))
span.SetStatus(codes.Error, "FFMPEG多文件concat协议转码失败")
@ -146,7 +146,7 @@ func runFfmpegForMultipleFile2(ctx context.Context, task *dto.FfmpegTask) bool {
subCtx, span := tracer.Start(ctx, "runFfmpegForMultipleFile2")
defer span.End()
// 多文件,方法二:使用计算资源编码
result, err := SlowVideoCut(subCtx, task.Files, int64(task.Offset), int64(task.Length), task.OutputFile)
result, err := SlowVideoCut(subCtx, task.Files, task.Offset, task.Offset, task.OutputFile)
if err != nil {
return false
}
@ -177,7 +177,7 @@ func runFfmpegForMultipleFile2(ctx context.Context, task *dto.FfmpegTask) bool {
func runFfmpegForSingleFile(ctx context.Context, task *dto.FfmpegTask) bool {
subCtx, span := tracer.Start(ctx, "runFfmpegForSingleFile")
defer span.End()
result, err := QuickVideoCut(subCtx, task.Files[0].Url, int64(task.Offset), int64(task.Length), task.OutputFile)
result, err := QuickVideoCut(subCtx, task.Files[0].Url, task.Offset, task.Length, task.OutputFile)
if err != nil {
span.SetStatus(codes.Error, "FFMPEG单个文件裁切失败")
return false
@ -235,7 +235,7 @@ func CheckFileCoverageAndConstructTask(ctx context.Context, fileList []dto.File,
lastFile = &file
continue
}
if file.StartTime.Sub(lastFile.EndTime).Milliseconds() > 2000 {
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())
@ -255,13 +255,13 @@ func CheckFileCoverageAndConstructTask(ctx context.Context, fileList []dto.File,
// 构造FfmpegTaskPo
ffmpegTask := &dto.FfmpegTask{
Files: fileList,
Length: int(endDt.Sub(beginDt).Seconds()),
Offset: int(beginDt.Sub(fileList[0].StartTime).Seconds()),
Length: endDt.Sub(beginDt).Seconds(),
Offset: beginDt.Sub(fileList[0].StartTime).Seconds(),
OutputFile: path.Join(os.TempDir(), task.TaskID+".mp4"),
}
span.SetAttributes(attribute.String("task.files", ToJson(ffmpegTask.Files)))
span.SetAttributes(attribute.Int("task.offset", ffmpegTask.Offset))
span.SetAttributes(attribute.Int("task.length", ffmpegTask.Length))
span.SetAttributes(attribute.Float64("task.offset", ffmpegTask.Offset))
span.SetAttributes(attribute.Float64("task.length", ffmpegTask.Length))
span.SetStatus(codes.Ok, "FFMPEG任务构造成功")
return ffmpegTask, nil
}
@ -298,7 +298,7 @@ func convertHevcToTs(ctx context.Context, file dto.File, outFileName string) (bo
return handleFfmpegProcess(subCtx, ffmpegCmd)
}
func QuickVideoCut(ctx context.Context, inputFile string, offset, length int64, outputFile string) (bool, error) {
func QuickVideoCut(ctx context.Context, inputFile string, offset, length float64, outputFile string) (bool, error) {
subCtx, span := tracer.Start(ctx, "QuickVideoCut")
defer span.End()
ffmpegCmd := []string{
@ -309,8 +309,8 @@ func QuickVideoCut(ctx context.Context, inputFile string, offset, length int64,
"-c:v", "copy",
"-an",
"-reset_timestamps", "1",
"-ss", strconv.FormatInt(offset, 10),
"-t", strconv.FormatInt(length, 10),
"-ss", strconv.FormatFloat(offset, 'f', 2, 64),
"-t", strconv.FormatFloat(length, 'f', 2, 64),
"-fflags", "+genpts",
"-f", "mp4",
outputFile,
@ -318,7 +318,7 @@ func QuickVideoCut(ctx context.Context, inputFile string, offset, length int64,
return handleFfmpegProcess(subCtx, ffmpegCmd)
}
func QuickConcatVideoCut(ctx context.Context, inputFiles []dto.File, offset, length int64, outputFile string) (bool, error) {
func QuickConcatVideoCut(ctx context.Context, inputFiles []dto.File, offset, length float64, outputFile string) (bool, error) {
subCtx, span := tracer.Start(ctx, "QuickConcatVideoCut")
defer span.End()
tmpFile := fmt.Sprintf("tmp%.10f.txt", rand.Float64())
@ -351,15 +351,15 @@ func QuickConcatVideoCut(ctx context.Context, inputFiles []dto.File, offset, len
"-i", tmpFile,
"-c:v", "copy",
"-an",
"-ss", strconv.FormatInt(offset, 10),
"-t", strconv.FormatInt(length, 10),
"-ss", strconv.FormatFloat(offset, 'f', 2, 64),
"-t", strconv.FormatFloat(length, 'f', 2, 64),
"-f", "mp4",
outputFile,
}
return handleFfmpegProcess(subCtx, ffmpegCmd)
}
func SlowVideoCut(ctx context.Context, inputFiles []dto.File, offset, length int64, outputFile string) (bool, error) {
func SlowVideoCut(ctx context.Context, inputFiles []dto.File, offset, length float64, outputFile string) (bool, error) {
subCtx, span := tracer.Start(ctx, "SlowVideoCut")
defer span.End()
ffmpegCmd := []string{
@ -384,8 +384,8 @@ func SlowVideoCut(ctx context.Context, inputFiles []dto.File, offset, length int
"-map", "[v]",
"-preset:v", "fast",
"-an",
"-ss", strconv.FormatInt(offset, 10),
"-t", strconv.FormatInt(length, 10),
"-ss", strconv.FormatFloat(offset, 'f', 2, 64),
"-t", strconv.FormatFloat(length, 'f', 2, 64),
"-f", "mp4",
outputFile,
)