diff --git a/dsSupport/ConvertMovie/Convert.go b/dsSupport/ConvertMovie/Convert.go index 0f6edbab..cf2852d9 100644 --- a/dsSupport/ConvertMovie/Convert.go +++ b/dsSupport/ConvertMovie/Convert.go @@ -7,17 +7,19 @@ import ( func main() { //源文件 source := "B7318F5D-46B8-4AA1-8811-1A9D65528E19.wmv" - //1、对视频文件进行切片 + //1、目录实始化 + ConvertUtil.InitDir(source) + //2、对视频文件进行切片 ConvertUtil.Cut(source) //2、切片完成后,尝试进行转码 var childMovie = ConvertUtil.GetAllCutChild(source) for i := range childMovie { ConvertUtil.ConvertToH264Mp4(childMovie[i]) } - //3、生成拼接的索引文件 - indexName := ConvertUtil.GenerateIndexTxt(source, childMovie) - //4、合成MP4 - ConvertUtil.Merge(source, indexName) + ////3、生成拼接的索引文件 + //indexName := ConvertUtil.GenerateIndexTxt(source, childMovie) + ////4、合成MP4 + //ConvertUtil.Merge(source, indexName) //5、清除垃圾 //ConvertUtil.ClearRubbish(source) } diff --git a/dsSupport/Utils/ConvertUtil/ConvertUtil.go b/dsSupport/Utils/ConvertUtil/ConvertUtil.go index 5cd5dbce..81aa2297 100644 --- a/dsSupport/Utils/ConvertUtil/ConvertUtil.go +++ b/dsSupport/Utils/ConvertUtil/ConvertUtil.go @@ -18,8 +18,35 @@ var ffmpeg string func init() { runDir, _ = os.Getwd() + ffmpeg = runDir + `\ffmpeg\ffmpeg.exe` +} + +const CutPath = "Cut/" +const Mp4Path = "Mp4/" +const IndexPath = "Index/" +const AllPath = "/" + +/** +功能:获取指定源文件的不同工作目录 +作者:黄海 +时间:2020-07-09 +*/ +func getPath(source string, p string) string { + return runDir + `/Working/` + source[0:2] + "/" + source[0:36] + "/" + p +} - ffmpeg=runDir + `\ffmpeg\ffmpeg.exe` +/** +功能:初始化目录 +作者:黄海 +时间:2020-07-09 +*/ +func InitDir(source string) { + //1、删除旧目录 + os.RemoveAll(getPath(source, AllPath)) + //2、创建新目录 + os.MkdirAll(getPath(source, CutPath), os.ModePerm) + os.Mkdir(getPath(source, Mp4Path), os.ModePerm) + os.Mkdir(getPath(source, IndexPath), os.ModePerm) } /** @@ -33,27 +60,7 @@ func Cut(source string) { //2、获取文件后缀 extension := path.Ext(source) //3、输出的文件通配名称 - workingPath := runDir + `/Working/` + source[0:2] + "/" - if !FileUtil.Exists(workingPath) { - os.Mkdir(workingPath, os.ModePerm) - } - workingPath = workingPath + source[0:36] + "/" - 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++ - } + targetPath := getPath(source,CutPath) + source[0:36] + `_%03d` + extension //4、切片 cmdLine := ffmpeg + ` -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) @@ -65,15 +72,13 @@ func Cut(source string) { 时间:2020-07-08 */ func GetAllCutChild(source string) []string { - filepathNames, err := filepath.Glob(filepath.Join(runDir+`/Working/`+source[0:2]+"/"+source[0:36]+"/", "*")) - if err!=nil{ + filepathNames, err := filepath.Glob(filepath.Join(getPath(source,CutPath), "*")) + if err != nil { fmt.Println(err.Error()) } - resultArray:=make([]string,0) + resultArray := make([]string, 0) for i := range filepathNames { - if strings.Index(filepathNames[i],"_")>=0{ - resultArray= append(resultArray, filepathNames[i]) - } + resultArray = append(resultArray, filepathNames[i]) } return resultArray } @@ -85,38 +90,36 @@ func GetAllCutChild(source string) []string { */ func ConvertToH264Mp4(childMovie string) { extension := path.Ext(childMovie) - childMovieMp4 := strings.Replace(childMovie, extension, ".mp4", -1) - //如果存在则删除之 - if FileUtil.Exists(childMovieMp4) { - os.Remove(childMovieMp4) - } - cmdLine := ffmpeg + ` -i ` + childMovie + ` -c:v libx264 -strict -2 ` + childMovieMp4 + childMovieMp4 := strings.Replace(filepath.Base(childMovie), extension, ".mp4", -1) + cmdLine := ffmpeg + ` -i ` + childMovie + ` -c:v libx264 -strict -2 ` + getPath(childMovie,Mp4Path)+childMovieMp4 ShellUtil.ExecCommand(cmdLine) } + /** 功能:生成索引的文本文件 作者:黄海 时间:2020-07-08 */ -func GenerateIndexTxt(source string,childMovie []string)string{ - content:=make([]string,0) +func GenerateIndexTxt(source string, childMovie []string) string { + content := make([]string, 0) for i := range childMovie { extension := path.Ext(childMovie[i]) - fullFilename:=strings.Replace(childMovie[i],extension,".mp4",-1) + fullFilename := strings.Replace(childMovie[i], extension, ".mp4", -1) filenameWithSuffix := filepath.Base(fullFilename) //获取文件名带后缀 - content= append(content, `file '`+filenameWithSuffix+`'`) + content = append(content, `file '`+filenameWithSuffix+`'`) } //文件位置 - indexName:=runDir+`/Working/`+source[0:2]+"/"+source[0:36]+"/"+source[0:36]+".txt" - FileUtil.WriteLines(content,indexName) + indexName := runDir + `/Working/` + source[0:2] + "/" + source[0:36] + "/" + source[0:36] + ".txt" + FileUtil.WriteLines(content, indexName) return indexName } + /** 功能:合并视频 作者:黄海 时间:2020-07-08 - */ -func Merge(source string,indexName string){ +*/ +func Merge(source string, indexName string) { Target := runDir + `/Target/` + source[0:2] + "/" if !FileUtil.Exists(Target) { os.Mkdir(Target, os.ModePerm) @@ -125,7 +128,7 @@ func Merge(source string,indexName string){ if !FileUtil.Exists(Target) { os.Mkdir(Target, os.ModePerm) } - cmdLine:=ffmpeg+` -f concat -i `+indexName+` -c copy `+Target+"/"+source[0:36]+".mp4" + cmdLine := ffmpeg + ` -f concat -i ` + indexName + ` -c copy ` + Target + "/" + source[0:36] + ".mp4" ShellUtil.ExecCommand(cmdLine) } @@ -133,11 +136,11 @@ func Merge(source string,indexName string){ 功能:清除垃圾 作者:黄海 时间:2020-07-08 - */ -func ClearRubbish(source string){ - var deletePath=[]string{"Working","Target"} +*/ +func ClearRubbish(source string) { + var deletePath = []string{"Working", "Target"} for i := range deletePath { //删除目录 - os.RemoveAll(runDir+`/`+deletePath[i]+`/`+source[0:2]+"/"+source[0:36]) + os.RemoveAll(runDir + `/` + deletePath[i] + `/` + source[0:2] + "/" + source[0:36]) } } diff --git a/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19.mp4 b/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19.mp4 deleted file mode 100644 index 0035788b..00000000 Binary files a/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19.mp4 and /dev/null differ diff --git a/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19.txt b/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19.txt deleted file mode 100644 index 930ba25f..00000000 --- a/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19.txt +++ /dev/null @@ -1,6 +0,0 @@ -file 'B7318F5D-46B8-4AA1-8811-1A9D65528E19.mp4' -file 'B7318F5D-46B8-4AA1-8811-1A9D65528E19.mp4' -file 'B7318F5D-46B8-4AA1-8811-1A9D65528E19_000.mp4' -file 'B7318F5D-46B8-4AA1-8811-1A9D65528E19_000.mp4' -file 'B7318F5D-46B8-4AA1-8811-1A9D65528E19_001.mp4' -file 'B7318F5D-46B8-4AA1-8811-1A9D65528E19_001.mp4' diff --git a/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19_000.mp4 b/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19_000.mp4 deleted file mode 100644 index e90d1f06..00000000 Binary files a/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19_000.mp4 and /dev/null differ diff --git a/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19_001.mp4 b/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19_001.mp4 deleted file mode 100644 index 6ca91b8b..00000000 Binary files a/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19_001.mp4 and /dev/null differ diff --git a/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19_000.wmv b/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/Cut/B7318F5D-46B8-4AA1-8811-1A9D65528E19_000.wmv similarity index 100% rename from dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19_000.wmv rename to dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/Cut/B7318F5D-46B8-4AA1-8811-1A9D65528E19_000.wmv diff --git a/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19_001.wmv b/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/Cut/B7318F5D-46B8-4AA1-8811-1A9D65528E19_001.wmv similarity index 100% rename from dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19_001.wmv rename to dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/Cut/B7318F5D-46B8-4AA1-8811-1A9D65528E19_001.wmv