统计TS文件

This commit is contained in:
BlackTeay 2024-05-06 17:35:41 +08:00
parent 57e0df0062
commit 7938f80b8e

33
main.go
View File

@ -1,7 +1,7 @@
/*
* @Author: BlackTeay
* @Date: 2024-04-30 09:37:39
* @LastEditTime: 2024-05-06 17:24:31
* @LastEditTime: 2024-05-06 17:33:58
* @LastEditors: BlackTeay
* @Description:
* @FilePath: /hls_builder/main.go
@ -366,6 +366,13 @@ func upload(localPath string, keyPath string, onComplete func()) {
fmt.Println("上传到Ucloud")
fmt.Println("localPath:", localPath)
fmt.Println("keyPath:", keyPath)
// 统计 .ts 文件数量
tsCount, err := countTSFiles(localPath)
if err != nil {
log.Fatalf("Failed to count .ts files: %s", err)
} else {
fmt.Printf("Found %d .ts files in %s\n", tsCount, localPath)
}
us3cliPath := getUs3cliPath()
bucket := "us3://jlntv-live/replay/" + keyPath
@ -382,7 +389,7 @@ func upload(localPath string, keyPath string, onComplete func()) {
}
go func() {
buffer := make([]byte, 1024) // 创建一个足够大的buffer
buffer := make([]byte, 4096) // 创建一个足够大的buffer
for {
n, err := stdout.Read(buffer)
if n > 0 {
@ -402,3 +409,25 @@ func upload(localPath string, keyPath string, onComplete func()) {
onComplete()
}
}
// countTSFiles 统计指定目录下所有 .ts 文件的数量。
//
// 参数:
// directory - 需要统计的目录路径。
//
// 返回值:
// 返回 .ts 文件的数量以及可能出现的错误。
func countTSFiles(directory string) (int, error) {
var count int // 用于记录 .ts 文件数量
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err // 如果遍历过程中出现错误,则返回该错误
}
// 检查当前路径项是否为文件且扩展名为 ".ts"
if !info.IsDir() && filepath.Ext(info.Name()) == ".ts" {
count++ // 如果满足条件,则计数加一
}
return nil
})
return count, err // 返回计数结果和可能出现的错误
}