Skip to content

Commit

Permalink
使用文件头判断视频文件
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyiming748 committed Aug 16, 2024
1 parent 7a2766f commit d4ff596
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

var (
Root string = "/data" // 工作目录 如果为空 默认/data
To string = "h265" // 转换到的编码 如果为空 默认vp9
To string = "h265" // 转换到的编码 如果为空 默认vp9
Direction string = "ToRight"
CpuNums int = runtime.NumCPU() // 核心数
TransTitle bool
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22
toolchain go1.22.2

require (
github.com/h2non/filetype v1.1.3
github.com/schollz/progressbar/v3 v3.14.4
github.com/zhangyiming748/DeepLX v0.0.1
github.com/zhangyiming748/FastMediaInfo v0.0.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHG
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
return err
}
log.Printf("准备处理的文件夹%v\n", info.Name())
files := util.GetAllFiles(absPath)
files := util.GetFiles(absPath)
cpus := constant.GetCpuNums()
if cpus > constant.MaxCPU {
cpus = constant.MaxCPU
Expand Down
39 changes: 39 additions & 0 deletions util/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package util
import (
"bufio"
"fmt"
"github.com/h2non/filetype"
"io"
"log"
"os"
Expand Down Expand Up @@ -60,6 +61,44 @@ func GetAllFiles(root string) (files []string) {
return files
}

/*
获取当前文件夹下视频文件
*/

func GetFiles(root string) (files []string) {
files = append(files, getFilesByHead(root)...)
return files
}

/*
获取当前文件夹和全部子文件夹下指定扩展名的全部文件
*/
func getFilesByHead(root string) []string {
var files []string
defer func() {
if err := recover(); err != nil {
log.Println("获取文件出错")
os.Exit(-1)
}
}()
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
// Open a file descriptor
file, _ := os.Open(path)
// We only have to pass the file header = first 261 bytes
head := make([]byte, 261)
file.Read(head)
if filetype.IsVideo(head) {
fmt.Printf("File: %v is a video/n", path)
files = append(files, path)
}

}
return nil
})
return files
}

/*
获取当前文件夹和全部子文件夹下指定扩展名的全部文件
*/
Expand Down

0 comments on commit d4ff596

Please sign in to comment.