获取时直接传入相对时间,还是0点可能会出问题,使用path.join而不是直接拼接地址

This commit is contained in:
Jerry Yan 2025-02-16 14:43:52 +08:00
parent 1115bed7e2
commit 5b4d94e905
7 changed files with 46 additions and 24 deletions

View File

@ -6,11 +6,15 @@ import (
"ZhenTuLocalPassiveAdapter/fs" "ZhenTuLocalPassiveAdapter/fs"
"ZhenTuLocalPassiveAdapter/util" "ZhenTuLocalPassiveAdapter/util"
"fmt" "fmt"
"path"
) )
func HandleTask(device config.DeviceMapping, task dto.Task) (*dto.FileObject, error) { func HandleTask(device config.DeviceMapping, task dto.Task) (*dto.FileObject, error) {
adapter := fs.GetAdapter() 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -3,10 +3,11 @@ package fs
import ( import (
"ZhenTuLocalPassiveAdapter/config" "ZhenTuLocalPassiveAdapter/config"
"ZhenTuLocalPassiveAdapter/dto" "ZhenTuLocalPassiveAdapter/dto"
"time"
) )
type Adapter interface { type Adapter interface {
GetFileList(path string) ([]dto.File, error) GetFileList(path string, relDt time.Time) ([]dto.File, error)
} }
func GetAdapter() Adapter { func GetAdapter() Adapter {

View File

@ -6,6 +6,7 @@ import (
"ZhenTuLocalPassiveAdapter/util" "ZhenTuLocalPassiveAdapter/util"
"fmt" "fmt"
"os" "os"
"path"
"sort" "sort"
"time" "time"
) )
@ -14,21 +15,12 @@ type LocalAdapter struct {
StorageConfig config.StorageConfig 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 == "" { if l.StorageConfig.Path == "" {
return nil, fmt.Errorf("未配置存储路径") 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 { if err != nil {
return nil, err return nil, err
} }
@ -45,8 +37,6 @@ func (l *LocalAdapter) GetFileList(path string) ([]dto.File, error) {
if err != nil { if err != nil {
continue continue
} }
// TODO: 0点左右会出问题
relDt := info.ModTime()
startTime, stopTime, err := util.ParseStartStopTime(info.Name(), relDt) startTime, stopTime, err := util.ParseStartStopTime(info.Name(), relDt)
if err != nil { if err != nil {
continue continue
@ -57,8 +47,8 @@ func (l *LocalAdapter) GetFileList(path string) ([]dto.File, error) {
fileList = append(fileList, dto.File{ fileList = append(fileList, dto.File{
BasePath: l.StorageConfig.Path, BasePath: l.StorageConfig.Path,
Name: file.Name(), Name: file.Name(),
Path: path, Path: dirPath,
Url: fmt.Sprintf("%s%s%s", l.StorageConfig.Path, path, file.Name()), Url: path.Join(l.StorageConfig.Path, dirPath, file.Name()),
StartTime: startTime, StartTime: startTime,
EndTime: stopTime, EndTime: stopTime,
}) })

8
go.mod
View File

@ -3,7 +3,8 @@ module ZhenTuLocalPassiveAdapter
go 1.23 go 1.23
require ( 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/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // 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/atomic v1.9.0 // indirect
go.uber.org/multierr 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/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.18.0 // indirect golang.org/x/net v0.33.0 // indirect
golang.org/x/text v0.14.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/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect
) )

15
go.sum
View File

@ -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.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.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 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= 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 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= 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 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= 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.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 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= 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= 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.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.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.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/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 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= 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= 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 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= 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 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 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 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= 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/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 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=

View File

@ -8,6 +8,7 @@ import (
"math/rand" "math/rand"
"os" "os"
"os/exec" "os/exec"
"path"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -45,7 +46,7 @@ func runFfmpegForMultipleFile1(task *dto.FfmpegTask) bool {
wg.Add(1) wg.Add(1)
go func(file *dto.File) { go func(file *dto.File) {
defer wg.Done() defer wg.Done()
tmpFile := os.TempDir() + "/" + file.Name + ".ts" tmpFile := path.Join(os.TempDir(), file.Name+".ts")
result, err := convertMp4ToTs(*file, tmpFile) result, err := convertMp4ToTs(*file, tmpFile)
if err != nil { if err != nil {
log.Printf("转码出错: %v", err) log.Printf("转码出错: %v", err)
@ -101,6 +102,15 @@ func runFfmpegForSingleFile(task *dto.FfmpegTask) bool {
if err != nil { if err != nil {
return false 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 return result
} }
@ -138,7 +148,7 @@ func CheckFileCoverageAndConstructTask(fileList []dto.File, beginDt, endDt time.
Files: fileList, Files: fileList,
Length: int(endDt.Sub(beginDt).Seconds()), Length: int(endDt.Sub(beginDt).Seconds()),
Offset: int(beginDt.Sub(fileList[0].StartTime).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 return ffmpegTask, nil

View File

@ -13,7 +13,7 @@ func FilterAndSortFiles(fileList []dto.File, beginDt, endDt time.Time) []dto.Fil
fileStartTime := file.StartTime fileStartTime := file.StartTime
nextFileStartTime := file.EndTime nextFileStartTime := file.EndTime
file.DiffMs = fileStartTime.Sub(beginDt).Milliseconds() file.DiffMs = beginDt.Sub(fileStartTime).Milliseconds()
// 如果当前文件还没有开始 // 如果当前文件还没有开始
if beginDt.After(fileStartTime) { 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 { sort.Slice(filteredFiles, func(i, j int) bool {
return filteredFiles[i].DiffMs > filteredFiles[j].DiffMs return filteredFiles[i].DiffMs > filteredFiles[j].DiffMs
}) })