You've already forked VptPassiveAdapter
Compare commits
2 Commits
030d8e9e6d
...
83bfe34394
| Author | SHA1 | Date | |
|---|---|---|---|
| 83bfe34394 | |||
| 5972ba1948 |
@@ -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"))
|
||||
|
||||
|
||||
@@ -32,3 +32,8 @@ disconnectAction:
|
||||
enabled: false
|
||||
thresholdMinutes: 5
|
||||
command: ""
|
||||
preview:
|
||||
enabled: true
|
||||
resolutions:
|
||||
- 720
|
||||
- 1080
|
||||
|
||||
@@ -64,6 +64,11 @@ type DisconnectActionConfig struct {
|
||||
Command string `mapstructure:"command"`
|
||||
}
|
||||
|
||||
type PreviewConfig struct {
|
||||
Enabled bool `mapstructure:"enabled"`
|
||||
Resolutions []int `mapstructure:"resolutions"`
|
||||
}
|
||||
|
||||
type MainConfig struct {
|
||||
Api ApiConfig `mapstructure:"api"`
|
||||
Record RecordConfig `mapstructure:"record"`
|
||||
@@ -71,4 +76,5 @@ type MainConfig struct {
|
||||
FileName FileNameConfig `mapstructure:"fileName"`
|
||||
Viid ViidConfig `mapstructure:"viid"`
|
||||
DisconnectAction DisconnectActionConfig `mapstructure:"disconnectAction"`
|
||||
Preview PreviewConfig `mapstructure:"preview"`
|
||||
}
|
||||
|
||||
78
main.go
78
main.go
@@ -53,12 +53,16 @@ func startTask(device config.DeviceMapping, task dto.Task) {
|
||||
zap.String("deviceNo", task.DeviceNo))
|
||||
|
||||
// 复制主文件用于异步生成预览,避免阻塞主上传流程
|
||||
previewEnabled := config.Config.Preview.Enabled && len(config.Config.Preview.Resolutions) > 0
|
||||
copyFile := strings.TrimSuffix(fo.URL, ".mp4") + "_copy.mp4"
|
||||
copyErr := copyFileOnDisk(fo.URL, copyFile)
|
||||
if copyErr != nil {
|
||||
logger.Warn("复制文件用于预览失败",
|
||||
zap.String("taskID", task.TaskID),
|
||||
zap.Error(copyErr))
|
||||
var copyErr error
|
||||
if previewEnabled {
|
||||
copyErr = copyFileOnDisk(fo.URL, copyFile)
|
||||
if copyErr != nil {
|
||||
logger.Warn("复制文件用于预览失败",
|
||||
zap.String("taskID", task.TaskID),
|
||||
zap.Error(copyErr))
|
||||
}
|
||||
}
|
||||
|
||||
err = api.UploadTaskFile(ctx, task, *fo)
|
||||
@@ -69,7 +73,7 @@ func startTask(device config.DeviceMapping, task dto.Task) {
|
||||
zap.String("deviceNo", task.DeviceNo),
|
||||
zap.Error(err))
|
||||
api.ReportTaskFailure(ctx, task.TaskID)
|
||||
if copyErr == nil {
|
||||
if previewEnabled && copyErr == nil {
|
||||
os.Remove(copyFile)
|
||||
}
|
||||
return
|
||||
@@ -81,7 +85,7 @@ func startTask(device config.DeviceMapping, task dto.Task) {
|
||||
zap.String("taskID", task.TaskID),
|
||||
zap.String("deviceNo", task.DeviceNo),
|
||||
zap.Error(err))
|
||||
if copyErr == nil {
|
||||
if previewEnabled && copyErr == nil {
|
||||
os.Remove(copyFile)
|
||||
}
|
||||
return
|
||||
@@ -91,8 +95,8 @@ func startTask(device config.DeviceMapping, task dto.Task) {
|
||||
zap.String("taskID", task.TaskID),
|
||||
zap.String("deviceNo", task.DeviceNo))
|
||||
|
||||
// 异步:从副本压缩720p预览 → 上传 → 上报
|
||||
if copyErr == nil {
|
||||
// 异步:从副本压缩预览 → 上传 → 上报
|
||||
if previewEnabled && copyErr == nil {
|
||||
go uploadPreview(task.TaskID, copyFile)
|
||||
}
|
||||
}
|
||||
@@ -102,32 +106,40 @@ 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")
|
||||
|
||||
for _, height := range config.Config.Preview.Resolutions {
|
||||
resolution := fmt.Sprintf("%dp", height)
|
||||
previewFile := fmt.Sprintf("%s_preview_%s.mp4", baseName, resolution)
|
||||
ok, _ := util.CompressVideo(ctx, copyFile, previewFile, height)
|
||||
if !ok {
|
||||
logger.Error("生成预览文件失败",
|
||||
zap.String("taskID", taskID),
|
||||
zap.String("resolution", resolution))
|
||||
continue
|
||||
}
|
||||
|
||||
err := api.UploadPreviewFile(ctx, taskID, previewFile, resolution)
|
||||
if err != nil {
|
||||
logger.Error("上传预览文件失败",
|
||||
zap.String("taskID", taskID),
|
||||
zap.String("resolution", resolution),
|
||||
zap.Error(err))
|
||||
os.Remove(previewFile)
|
||||
continue
|
||||
}
|
||||
if !api.ReportPreviewSuccess(ctx, taskID, resolution) {
|
||||
logger.Error("上报预览成功失败",
|
||||
zap.String("taskID", taskID),
|
||||
zap.String("resolution", resolution))
|
||||
continue
|
||||
}
|
||||
logger.Info("预览上传成功",
|
||||
zap.String("taskID", taskID),
|
||||
zap.String("resolution", resolution))
|
||||
}
|
||||
|
||||
err := api.UploadPreviewFile(ctx, taskID, previewFile)
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, "上传预览文件失败")
|
||||
logger.Error("上传预览文件失败",
|
||||
zap.String("taskID", taskID),
|
||||
zap.Error(err))
|
||||
os.Remove(previewFile)
|
||||
return
|
||||
}
|
||||
result := api.ReportPreviewSuccess(ctx, taskID)
|
||||
if !result {
|
||||
span.SetStatus(codes.Error, "上报预览成功失败")
|
||||
logger.Error("上报预览成功失败",
|
||||
zap.String("taskID", taskID))
|
||||
return
|
||||
}
|
||||
span.SetStatus(codes.Ok, "预览上传成功")
|
||||
logger.Info("预览上传成功", zap.String("taskID", taskID))
|
||||
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