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