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