From 5b4d94e905eb64245517a4c1488dc381e81bb5a9 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Sun, 16 Feb 2025 14:43:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=97=B6=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E4=BC=A0=E5=85=A5=E7=9B=B8=E5=AF=B9=E6=97=B6=E9=97=B4=EF=BC=8C?= =?UTF-8?q?=E8=BF=98=E6=98=AF0=E7=82=B9=E5=8F=AF=E8=83=BD=E4=BC=9A?= =?UTF-8?q?=E5=87=BA=E9=97=AE=E9=A2=98=EF=BC=8C=E4=BD=BF=E7=94=A8path.join?= =?UTF-8?q?=E8=80=8C=E4=B8=8D=E6=98=AF=E7=9B=B4=E6=8E=A5=E6=8B=BC=E6=8E=A5?= =?UTF-8?q?=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/task.go | 6 +++++- fs/adapter.go | 3 ++- fs/local_adapter.go | 20 +++++--------------- go.mod | 8 +++++--- go.sum | 15 +++++++++++++++ util/ffmpeg.go | 14 ++++++++++++-- util/file_filter.go | 4 ++-- 7 files changed, 46 insertions(+), 24 deletions(-) diff --git a/core/task.go b/core/task.go index ecf4f1a..8f886de 100644 --- a/core/task.go +++ b/core/task.go @@ -6,11 +6,15 @@ import ( "ZhenTuLocalPassiveAdapter/fs" "ZhenTuLocalPassiveAdapter/util" "fmt" + "path" ) func HandleTask(device config.DeviceMapping, task dto.Task) (*dto.FileObject, error) { adapter := fs.GetAdapter() - fileList, err := adapter.GetFileList(device.Name + "/" + task.StartTime.Format("2006"+config.Config.FileName.DateSeparator+"01"+config.Config.FileName.DateSeparator+"02")) + fileList, err := adapter.GetFileList( + path.Join(device.Name, task.StartTime.Format("2006"+config.Config.FileName.DateSeparator+"01"+config.Config.FileName.DateSeparator+"02")), + task.StartTime, + ) if err != nil { return nil, err } diff --git a/fs/adapter.go b/fs/adapter.go index ead6d59..8740f2c 100644 --- a/fs/adapter.go +++ b/fs/adapter.go @@ -3,10 +3,11 @@ package fs import ( "ZhenTuLocalPassiveAdapter/config" "ZhenTuLocalPassiveAdapter/dto" + "time" ) type Adapter interface { - GetFileList(path string) ([]dto.File, error) + GetFileList(path string, relDt time.Time) ([]dto.File, error) } func GetAdapter() Adapter { diff --git a/fs/local_adapter.go b/fs/local_adapter.go index e38ff7f..828d507 100644 --- a/fs/local_adapter.go +++ b/fs/local_adapter.go @@ -6,6 +6,7 @@ import ( "ZhenTuLocalPassiveAdapter/util" "fmt" "os" + "path" "sort" "time" ) @@ -14,21 +15,12 @@ type LocalAdapter struct { StorageConfig config.StorageConfig } -func (l *LocalAdapter) GetFileList(path string) ([]dto.File, error) { +func (l *LocalAdapter) GetFileList(dirPath string, relDt time.Time) ([]dto.File, error) { if l.StorageConfig.Path == "" { return nil, fmt.Errorf("未配置存储路径") } - if path == "" { - path = "/" - } - if path[0] != '/' { - path = "/" + path - } - if path[len(path)-1] != '/' { - path = path + "/" - } // 读取文件夹下目录 - files, err := os.ReadDir(l.StorageConfig.Path + path) + files, err := os.ReadDir(path.Join(l.StorageConfig.Path, dirPath)) if err != nil { return nil, err } @@ -45,8 +37,6 @@ func (l *LocalAdapter) GetFileList(path string) ([]dto.File, error) { if err != nil { continue } - // TODO: 0点左右会出问题 - relDt := info.ModTime() startTime, stopTime, err := util.ParseStartStopTime(info.Name(), relDt) if err != nil { continue @@ -57,8 +47,8 @@ func (l *LocalAdapter) GetFileList(path string) ([]dto.File, error) { fileList = append(fileList, dto.File{ BasePath: l.StorageConfig.Path, Name: file.Name(), - Path: path, - Url: fmt.Sprintf("%s%s%s", l.StorageConfig.Path, path, file.Name()), + Path: dirPath, + Url: path.Join(l.StorageConfig.Path, dirPath, file.Name()), StartTime: startTime, EndTime: stopTime, }) diff --git a/go.mod b/go.mod index 9792e00..60ee601 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,8 @@ module ZhenTuLocalPassiveAdapter go 1.23 require ( - github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/fsnotify/fsnotify v1.8.0 // indirect + github.com/go-resty/resty/v2 v2.16.5 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect @@ -19,8 +20,9 @@ require ( go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.9.0 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect - golang.org/x/sys v0.18.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/net v0.33.0 // indirect + golang.org/x/sys v0.28.0 // indirect + golang.org/x/text v0.21.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 2b668ad..942dbd2 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,13 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= +github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= +github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptdhTM= +github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= @@ -11,6 +17,8 @@ github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= @@ -33,6 +41,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= @@ -42,10 +51,16 @@ go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I= +golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= +golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= +golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= diff --git a/util/ffmpeg.go b/util/ffmpeg.go index 5f839ae..44da5ba 100644 --- a/util/ffmpeg.go +++ b/util/ffmpeg.go @@ -8,6 +8,7 @@ import ( "math/rand" "os" "os/exec" + "path" "strconv" "strings" "sync" @@ -45,7 +46,7 @@ func runFfmpegForMultipleFile1(task *dto.FfmpegTask) bool { wg.Add(1) go func(file *dto.File) { defer wg.Done() - tmpFile := os.TempDir() + "/" + file.Name + ".ts" + tmpFile := path.Join(os.TempDir(), file.Name+".ts") result, err := convertMp4ToTs(*file, tmpFile) if err != nil { log.Printf("转码出错: %v", err) @@ -101,6 +102,15 @@ func runFfmpegForSingleFile(task *dto.FfmpegTask) bool { if err != nil { return false } + outfile, err := os.Stat(task.OutputFile) + if err != nil { + log.Printf("文件不存在:%s", task.OutputFile) + return false + } + if outfile.Size() < 4096 { + log.Printf("文件大小过小:%s", task.OutputFile) + return false + } return result } @@ -138,7 +148,7 @@ func CheckFileCoverageAndConstructTask(fileList []dto.File, beginDt, endDt time. Files: fileList, Length: int(endDt.Sub(beginDt).Seconds()), Offset: int(beginDt.Sub(fileList[0].StartTime).Seconds()), - OutputFile: os.TempDir() + "/" + task.TaskID + ".mp4", + OutputFile: path.Join(os.TempDir(), task.TaskID+".mp4"), } return ffmpegTask, nil diff --git a/util/file_filter.go b/util/file_filter.go index 2b89ae8..f4e0629 100644 --- a/util/file_filter.go +++ b/util/file_filter.go @@ -13,7 +13,7 @@ func FilterAndSortFiles(fileList []dto.File, beginDt, endDt time.Time) []dto.Fil fileStartTime := file.StartTime nextFileStartTime := file.EndTime - file.DiffMs = fileStartTime.Sub(beginDt).Milliseconds() + file.DiffMs = beginDt.Sub(fileStartTime).Milliseconds() // 如果当前文件还没有开始 if beginDt.After(fileStartTime) { // 没有下一个文件的情况下,就是最后一个文件 @@ -33,7 +33,7 @@ func FilterAndSortFiles(fileList []dto.File, beginDt, endDt time.Time) []dto.Fil } } - // 按照 GetDiffMs 的值降序排序 + // 按照 DiffMs 的值降序排序 sort.Slice(filteredFiles, func(i, j int) bool { return filteredFiles[i].DiffMs > filteredFiles[j].DiffMs })