From 9b8f61e8e15a21f30622cc00091b8a06cb0c21e3 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Wed, 19 Feb 2025 10:28:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E7=BB=93=E6=9D=9F=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E8=AF=BB=E5=8F=96=E9=94=99=E8=AF=AF=EF=BC=8C=E4=B8=8D?= =?UTF-8?q?=E6=8A=A5=E9=94=99=EF=BC=8C=E7=94=B1=E5=90=8E=E7=BB=AD=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fs/local_adapter.go | 10 +++++++++- util/ffmpeg.go | 27 +++++++++++++++++++++++++++ util/parse_time.go | 2 +- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/fs/local_adapter.go b/fs/local_adapter.go index 828d507..00ff816 100644 --- a/fs/local_adapter.go +++ b/fs/local_adapter.go @@ -42,7 +42,15 @@ func (l *LocalAdapter) GetFileList(dirPath string, relDt time.Time) ([]dto.File, continue } if startTime.Equal(stopTime) { - stopTime = stopTime.Add(time.Second * time.Duration(config.Config.Record.Duration)) + // 如果文件名没有时间戳,则认为该文件是未录制完成的 + // 尝试读取一下视频信息 + duration, err := util.GetVideoDuration(path.Join(l.StorageConfig.Path, dirPath, file.Name())) + if err != nil { + // 如果还是没有,就按照配置文件里的加起来 + stopTime = stopTime.Add(time.Second * time.Duration(config.Config.Record.Duration)) + } else { + stopTime = stopTime.Add(time.Second * time.Duration(duration)) + } } fileList = append(fileList, dto.File{ BasePath: l.StorageConfig.Path, diff --git a/util/ffmpeg.go b/util/ffmpeg.go index 44da5ba..9135b03 100644 --- a/util/ffmpeg.go +++ b/util/ffmpeg.go @@ -300,3 +300,30 @@ func handleFfmpegProcess(ffmpegCmd []string) (bool, error) { return true, nil } } + +func GetVideoDuration(filePath string) (float64, error) { + ffprobeCmd := []string{ + "ffprobe", + "-v", "error", + "-show_entries", "format=duration", + "-of", "default=noprint_wrappers=1:nokey=1", + filePath, + } + + cmd := exec.Command(ffprobeCmd[0], ffprobeCmd[1:]...) + var out bytes.Buffer + cmd.Stdout = &out + + err := cmd.Run() + if err != nil { + return 0, fmt.Errorf("failed to get video duration: %w", err) + } + + durationStr := strings.TrimSpace(out.String()) + duration, err := strconv.ParseFloat(durationStr, 64) + if err != nil { + return 0, fmt.Errorf("failed to parse video duration: %w", err) + } + + return duration, nil +} diff --git a/util/parse_time.go b/util/parse_time.go index a58c45f..349a35e 100644 --- a/util/parse_time.go +++ b/util/parse_time.go @@ -19,7 +19,7 @@ func ParseStartStopTime(filePath string, relativeDate time.Time) (time.Time, tim } stopTime, err := ParseTime(split[1], relativeDate) if err != nil { - return time.Time{}, time.Time{}, err + return startTime, startTime, nil } return startTime, stopTime, nil }