From e123364e28bd880276f20fd1dc3941da1a4662ed Mon Sep 17 00:00:00 2001 From: huanghai <10402852@qq.com> Date: Wed, 8 Jul 2020 15:29:31 +0800 Subject: [PATCH] 'commit' --- dsSupport/ConvertMovie/Convert.go | 12 ++--- dsSupport/Utils/ConvertUtil/ConvertUtil.go | 53 ++++++++++++++++++++-- dsSupport/Utils/FileUtil/FileUtil.go | 15 ++++++ 3 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 dsSupport/Utils/FileUtil/FileUtil.go diff --git a/dsSupport/ConvertMovie/Convert.go b/dsSupport/ConvertMovie/Convert.go index a206dc86..21b0bbcf 100644 --- a/dsSupport/ConvertMovie/Convert.go +++ b/dsSupport/ConvertMovie/Convert.go @@ -2,23 +2,17 @@ package main import ( "dsSupport/Utils/ConvertUtil" - "dsSupport/Utils/ShellUtil" - "path/filepath" - "strings" ) func main(){ //源文件 source:="B7318F5D-46B8-4AA1-8811-1A9D65528E19.wmv" - //1、对视频文件进行切片 ConvertUtil.Cut(source) - //2、切片完成后,尝试进行转码 - filepathNames,_ := filepath.Glob(filepath.Join(dir+`/Target`,"*")) - for i := range filepathNames { - cmdLine=dir+`\ffmpeg\ffmpeg.exe -i `+filepathNames[i]+` -c:v libx264 -strict -2 `+strings.Replace(filepathNames[i],".wmv",".mp4",-1) - ShellUtil.ExecCommand(cmdLine) + var childMovie=ConvertUtil.GetAllCutChild(source) + for i := range childMovie { + ConvertUtil.ConvertToH264Mp4(childMovie[i]) } } diff --git a/dsSupport/Utils/ConvertUtil/ConvertUtil.go b/dsSupport/Utils/ConvertUtil/ConvertUtil.go index 49ce27fe..55b8d07f 100644 --- a/dsSupport/Utils/ConvertUtil/ConvertUtil.go +++ b/dsSupport/Utils/ConvertUtil/ConvertUtil.go @@ -1,9 +1,13 @@ package ConvertUtil import ( + "dsSupport/Utils/FileUtil" "dsSupport/Utils/ShellUtil" + "fmt" "os" "path" + "path/filepath" + "strings" ) //运行的目录 @@ -17,15 +21,56 @@ func init() { 功能:对指定的视频文件进行切片 作者:黄海 时间:2020-07-08 - */ +*/ func Cut(source string) { //1、源视频文件 - sourcePath:=runDir+"/Source/"+source[0:2]+"/"+source + sourcePath := runDir + "/Source/" + source[0:2] + "/" + source //2、获取文件后缀 - extension:= path.Ext(source) + extension := path.Ext(source) //3、输出的文件通配名称 - targetPath:=runDir+`/Target/`+source[0:36]+`_%03d.`+extension + workingPath := runDir + `/Target/` + source[0:32] + "/" + if !FileUtil.Exists(workingPath) { + os.Mkdir(workingPath, os.ModePerm) + } + targetPath := workingPath + source[0:36] + `_%03d` + extension + //尝试删除已存在的视频 + var i = 0 + for { + var f = workingPath + source[0:36] + "_" + fmt.Sprintf("%03d", i) + extension + if FileUtil.Exists(f) { + os.Remove(f) + } else { + break + } + i++ + } //4、切片 cmdLine := runDir + `\ffmpeg\ffmpeg.exe -fflags +genpts -i ` + sourcePath + ` -acodec copy -vcodec copy -f segment -segment_time 300 -reset_timestamps 1 -map 0:0 -map 0:1 ` + targetPath ShellUtil.ExecCommand(cmdLine) } + +/** +功能:将指定的视频文件,在切片后,获取都生成了哪些切片 +作者:黄海 +时间:2020-07-08 +*/ +func GetAllCutChild(source string) []string { + filepathNames, _ := filepath.Glob(filepath.Join(runDir+`/Target/`+source[0:2]+"/"+source[0:36]+"/", "*")) + return filepathNames +} + +/** +功能:将视频转为H264格式的MP4 +作者: 黄海 +时间:2020-07-08 +*/ +func ConvertToH264Mp4(childMovie string) { + extension := path.Ext(childMovie) + childMovieMp4 := strings.Replace(childMovie, extension, ".mp4", -1) + //如果存在则删除之 + if FileUtil.Exists(childMovieMp4) { + os.Remove(childMovieMp4) + } + cmdLine := runDir + `\ffmpeg\ffmpeg.exe -i ` + childMovie + ` -c:v libx264 -strict -2 ` + childMovieMp4 + ShellUtil.ExecCommand(cmdLine) +} diff --git a/dsSupport/Utils/FileUtil/FileUtil.go b/dsSupport/Utils/FileUtil/FileUtil.go new file mode 100644 index 00000000..af6fd1a8 --- /dev/null +++ b/dsSupport/Utils/FileUtil/FileUtil.go @@ -0,0 +1,15 @@ +package FileUtil + +import "os" + +// 判断所给路径文件/文件夹是否存在 +func Exists(path string) bool { + _, err := os.Stat(path) //os.Stat获取文件信息 + if err != nil { + if os.IsExist(err) { + return true + } + return false + } + return true +}