You've already forked VptPassiveAdapter
config p
This commit is contained in:
@@ -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"`
|
||||
}
|
||||
|
||||
42
main.go
42
main.go
@@ -53,13 +53,17 @@ 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)
|
||||
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)
|
||||
if err != nil {
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -104,43 +108,35 @@ func uploadPreview(taskID string, copyFile string) {
|
||||
|
||||
baseName := strings.TrimSuffix(copyFile, "_copy.mp4")
|
||||
|
||||
type previewSpec struct {
|
||||
resolution string
|
||||
height int
|
||||
}
|
||||
specs := []previewSpec{
|
||||
{"720p", 720},
|
||||
{"1080p", 1080},
|
||||
}
|
||||
|
||||
for _, spec := range specs {
|
||||
previewFile := fmt.Sprintf("%s_preview_%s.mp4", baseName, spec.resolution)
|
||||
ok, _ := util.CompressVideo(ctx, copyFile, previewFile, spec.height)
|
||||
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", spec.resolution))
|
||||
zap.String("resolution", resolution))
|
||||
continue
|
||||
}
|
||||
|
||||
err := api.UploadPreviewFile(ctx, taskID, previewFile, spec.resolution)
|
||||
err := api.UploadPreviewFile(ctx, taskID, previewFile, resolution)
|
||||
if err != nil {
|
||||
logger.Error("上传预览文件失败",
|
||||
zap.String("taskID", taskID),
|
||||
zap.String("resolution", spec.resolution),
|
||||
zap.String("resolution", resolution),
|
||||
zap.Error(err))
|
||||
os.Remove(previewFile)
|
||||
continue
|
||||
}
|
||||
if !api.ReportPreviewSuccess(ctx, taskID, spec.resolution) {
|
||||
if !api.ReportPreviewSuccess(ctx, taskID, resolution) {
|
||||
logger.Error("上报预览成功失败",
|
||||
zap.String("taskID", taskID),
|
||||
zap.String("resolution", spec.resolution))
|
||||
zap.String("resolution", resolution))
|
||||
continue
|
||||
}
|
||||
logger.Info("预览上传成功",
|
||||
zap.String("taskID", taskID),
|
||||
zap.String("resolution", spec.resolution))
|
||||
zap.String("resolution", resolution))
|
||||
}
|
||||
|
||||
span.SetStatus(codes.Ok, "预览处理完成")
|
||||
|
||||
Reference in New Issue
Block a user