You've already forked VptPassiveAdapter
common p
This commit is contained in:
@@ -76,14 +76,14 @@ func OssUpload(ctx context.Context, url, filePath string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func UploadPreviewFile(ctx context.Context, taskId string, previewFilePath string) error {
|
||||
func UploadPreviewFile(ctx context.Context, taskId string, previewFilePath string, resolution string) error {
|
||||
subCtx, span := tracer.Start(ctx, "UploadPreviewFile")
|
||||
defer span.End()
|
||||
url, err := QueryPreviewUploadUrl(subCtx, taskId)
|
||||
url, err := QueryPreviewUploadUrl(subCtx, taskId, resolution)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger.Info("开始上传预览文件", zap.String("url", url))
|
||||
logger.Info("开始上传预览文件", zap.String("url", url), zap.String("resolution", resolution))
|
||||
if err := OssUpload(subCtx, url, previewFilePath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -127,10 +127,10 @@ func ReportTaskSuccess(ctx context.Context, taskId string, file *dto.FileObject)
|
||||
}
|
||||
}
|
||||
|
||||
func QueryPreviewUploadUrl(ctx context.Context, taskId string) (string, error) {
|
||||
func QueryPreviewUploadUrl(ctx context.Context, taskId string, resolution string) (string, error) {
|
||||
_, span := tracer.Start(ctx, "QueryPreviewUploadUrl")
|
||||
defer span.End()
|
||||
url := config.Config.Api.BaseUrl + "/" + taskId + "/previewUploadUrl"
|
||||
url := config.Config.Api.BaseUrl + "/" + taskId + "/previewUploadUrl?resolution=" + resolution
|
||||
span.SetAttributes(attribute.String("http.url", url))
|
||||
span.SetAttributes(attribute.String("http.method", "GET"))
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
@@ -160,10 +160,10 @@ func QueryPreviewUploadUrl(ctx context.Context, taskId string) (string, error) {
|
||||
return string(body), nil
|
||||
}
|
||||
|
||||
func ReportPreviewSuccess(ctx context.Context, taskId string) bool {
|
||||
func ReportPreviewSuccess(ctx context.Context, taskId string, resolution string) bool {
|
||||
_, span := tracer.Start(ctx, "ReportPreviewSuccess")
|
||||
defer span.End()
|
||||
url := config.Config.Api.BaseUrl + "/" + taskId + "/previewSuccess"
|
||||
url := config.Config.Api.BaseUrl + "/" + taskId + "/previewSuccess?resolution=" + resolution
|
||||
span.SetAttributes(attribute.String("http.url", url))
|
||||
span.SetAttributes(attribute.String("http.method", "POST"))
|
||||
|
||||
|
||||
48
main.go
48
main.go
@@ -102,32 +102,48 @@ func uploadPreview(taskID string, copyFile string) {
|
||||
defer span.End()
|
||||
defer os.Remove(copyFile)
|
||||
|
||||
previewFile := strings.TrimSuffix(copyFile, "_copy.mp4") + "_preview.mp4"
|
||||
ok, _ := util.CompressTo720p(ctx, copyFile, previewFile)
|
||||
if !ok {
|
||||
span.SetStatus(codes.Error, "生成预览文件失败")
|
||||
logger.Error("生成预览文件失败", zap.String("taskID", taskID))
|
||||
return
|
||||
baseName := strings.TrimSuffix(copyFile, "_copy.mp4")
|
||||
|
||||
type previewSpec struct {
|
||||
resolution string
|
||||
height int
|
||||
}
|
||||
specs := []previewSpec{
|
||||
{"720p", 720},
|
||||
{"1080p", 1080},
|
||||
}
|
||||
|
||||
err := api.UploadPreviewFile(ctx, taskID, previewFile)
|
||||
for _, spec := range specs {
|
||||
previewFile := fmt.Sprintf("%s_preview_%s.mp4", baseName, spec.resolution)
|
||||
ok, _ := util.CompressVideo(ctx, copyFile, previewFile, spec.height)
|
||||
if !ok {
|
||||
logger.Error("生成预览文件失败",
|
||||
zap.String("taskID", taskID),
|
||||
zap.String("resolution", spec.resolution))
|
||||
continue
|
||||
}
|
||||
|
||||
err := api.UploadPreviewFile(ctx, taskID, previewFile, spec.resolution)
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, "上传预览文件失败")
|
||||
logger.Error("上传预览文件失败",
|
||||
zap.String("taskID", taskID),
|
||||
zap.String("resolution", spec.resolution),
|
||||
zap.Error(err))
|
||||
os.Remove(previewFile)
|
||||
return
|
||||
continue
|
||||
}
|
||||
result := api.ReportPreviewSuccess(ctx, taskID)
|
||||
if !result {
|
||||
span.SetStatus(codes.Error, "上报预览成功失败")
|
||||
if !api.ReportPreviewSuccess(ctx, taskID, spec.resolution) {
|
||||
logger.Error("上报预览成功失败",
|
||||
zap.String("taskID", taskID))
|
||||
return
|
||||
zap.String("taskID", taskID),
|
||||
zap.String("resolution", spec.resolution))
|
||||
continue
|
||||
}
|
||||
span.SetStatus(codes.Ok, "预览上传成功")
|
||||
logger.Info("预览上传成功", zap.String("taskID", taskID))
|
||||
logger.Info("预览上传成功",
|
||||
zap.String("taskID", taskID),
|
||||
zap.String("resolution", spec.resolution))
|
||||
}
|
||||
|
||||
span.SetStatus(codes.Ok, "预览处理完成")
|
||||
}
|
||||
|
||||
func copyFileOnDisk(src, dst string) error {
|
||||
|
||||
@@ -470,15 +470,17 @@ func handleFfmpegProcess(ctx context.Context, ffmpegCmd []string) (bool, error)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func CompressTo720p(ctx context.Context, inputFile, outputFile string) (bool, error) {
|
||||
subCtx, span := tracer.Start(ctx, "CompressTo720p")
|
||||
func CompressVideo(ctx context.Context, inputFile, outputFile string, height int) (bool, error) {
|
||||
subCtx, span := tracer.Start(ctx, "CompressVideo")
|
||||
defer span.End()
|
||||
span.SetAttributes(attribute.Int("height", height))
|
||||
scaleFilter := fmt.Sprintf("scale=-2:%d", height)
|
||||
ffmpegCmd := []string{
|
||||
FfmpegExec,
|
||||
"-hide_banner",
|
||||
"-y",
|
||||
"-i", inputFile,
|
||||
"-vf", "scale=-2:720",
|
||||
"-vf", scaleFilter,
|
||||
"-c:v", "libx264",
|
||||
"-preset", "fast",
|
||||
"-crf", "28",
|
||||
|
||||
Reference in New Issue
Block a user