From 79683bedbdc3aba24f69254a04e482ccc385b4b4 Mon Sep 17 00:00:00 2001 From: huanghai <10402852@qq.com> Date: Wed, 8 Jul 2020 16:03:15 +0800 Subject: [PATCH] 'commit' --- dsSupport/ConvertMovie/Convert.go | 4 ++ dsSupport/Utils/ConvertUtil/ConvertUtil.go | 51 ++++++++++++++++++- dsSupport/Utils/FileUtil/FileUtil.go | 44 +++++++++++++++- .../B7318F5D-46B8-4AA1-8811-1A9D65528E19.txt | 4 ++ 4 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19.txt diff --git a/dsSupport/ConvertMovie/Convert.go b/dsSupport/ConvertMovie/Convert.go index 21b0bbcf..655b0961 100644 --- a/dsSupport/ConvertMovie/Convert.go +++ b/dsSupport/ConvertMovie/Convert.go @@ -14,5 +14,9 @@ func main(){ for i := range childMovie { ConvertUtil.ConvertToH264Mp4(childMovie[i]) } + //3、生成拼接的索引文件 + indexName:=ConvertUtil.GenerateIndexTxt(source,childMovie) + //4、合成MP4 + ConvertUtil.Merge(source,indexName) } diff --git a/dsSupport/Utils/ConvertUtil/ConvertUtil.go b/dsSupport/Utils/ConvertUtil/ConvertUtil.go index 2426bb1c..9966e019 100644 --- a/dsSupport/Utils/ConvertUtil/ConvertUtil.go +++ b/dsSupport/Utils/ConvertUtil/ConvertUtil.go @@ -13,8 +13,13 @@ import ( //运行的目录 var runDir string +// ffmpeg 的命令行位置 +var ffmpeg string + func init() { runDir, _ = os.Getwd() + + ffmpeg=runDir + `\ffmpeg\ffmpeg.exe` } /** @@ -50,7 +55,7 @@ func Cut(source string) { 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 + 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) } @@ -79,6 +84,48 @@ func ConvertToH264Mp4(childMovie string) { if FileUtil.Exists(childMovieMp4) { os.Remove(childMovieMp4) } - cmdLine := runDir + `\ffmpeg\ffmpeg.exe -i ` + childMovie + ` -c:v libx264 -strict -2 ` + childMovieMp4 + cmdLine := ffmpeg + ` -i ` + childMovie + ` -c:v libx264 -strict -2 ` + childMovieMp4 ShellUtil.ExecCommand(cmdLine) } +/** +功能:生成索引的文本文件 +作者:黄海 +时间:2020-07-08 +*/ +func GenerateIndexTxt(source string,childMovie []string)string{ + content:=make([]string,0) + for i := range childMovie { + extension := path.Ext(childMovie[i]) + content= append(content, `file '`+strings.Replace(childMovie[i],extension,".mp4",-1)+`'`) + } + //文件位置 + 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){ + Target := runDir + `/Target/` + source[0:2] + "/" + if !FileUtil.Exists(Target) { + os.Mkdir(Target, os.ModePerm) + } + Target = Target + source[0:36] + "/" + if !FileUtil.Exists(Target) { + os.Mkdir(Target, os.ModePerm) + } + cmdLine:=ffmpeg+` -f concat -i `+indexName+` -c copy `+Target+"/"+source[0:36]+".mp4" + ShellUtil.ExecCommand(cmdLine) +} + +/** +功能:清除垃圾 +作者:黄海 +时间:2020-07-08 + */ +func ClearRubbish(source string){ + +} diff --git a/dsSupport/Utils/FileUtil/FileUtil.go b/dsSupport/Utils/FileUtil/FileUtil.go index af6fd1a8..952d3f59 100644 --- a/dsSupport/Utils/FileUtil/FileUtil.go +++ b/dsSupport/Utils/FileUtil/FileUtil.go @@ -1,6 +1,10 @@ package FileUtil -import "os" +import ( + "bufio" + "fmt" + "os" +) // 判断所给路径文件/文件夹是否存在 func Exists(path string) bool { @@ -13,3 +17,41 @@ func Exists(path string) bool { } return true } +/** +功能:按行读取文件 +作者:黄海 +时间:2020-07-08 + */ +func ReadLines(path string) ([]string, error) { + file, err := os.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + + var lines []string + scanner := bufio.NewScanner(file) + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + return lines, scanner.Err() +} + +/** +功能:按行写入文件 +作者:黄海 +时间:2020-07-08 +*/ +func WriteLines(lines []string, path string) error { + file, err := os.Create(path) + if err != nil { + return err + } + defer file.Close() + + w := bufio.NewWriter(file) + for _, line := range lines { + fmt.Fprintln(w, line) + } + return w.Flush() +} \ No newline at end of file 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 new file mode 100644 index 00000000..c444bdca --- /dev/null +++ b/dsSupport/Working/B7/B7318F5D-46B8-4AA1-8811-1A9D65528E19/B7318F5D-46B8-4AA1-8811-1A9D65528E19.txt @@ -0,0 +1,4 @@ +file 'E:\Work\dsMin\dsSupport\Working\B7\B7318F5D-46B8-4AA1-8811-1A9D65528E19\B7318F5D-46B8-4AA1-8811-1A9D65528E19_000.mp4' +file 'E:\Work\dsMin\dsSupport\Working\B7\B7318F5D-46B8-4AA1-8811-1A9D65528E19\B7318F5D-46B8-4AA1-8811-1A9D65528E19_000.mp4' +file 'E:\Work\dsMin\dsSupport\Working\B7\B7318F5D-46B8-4AA1-8811-1A9D65528E19\B7318F5D-46B8-4AA1-8811-1A9D65528E19_001.mp4' +file 'E:\Work\dsMin\dsSupport\Working\B7\B7318F5D-46B8-4AA1-8811-1A9D65528E19\B7318F5D-46B8-4AA1-8811-1A9D65528E19_001.mp4'