diff --git a/dsSupport/Utils/ConvertUtil/ConvertUtil.go b/dsSupport/Utils/ConvertUtil/ConvertUtil.go index 7b1b1fbb..cf4e5d59 100644 --- a/dsSupport/Utils/ConvertUtil/ConvertUtil.go +++ b/dsSupport/Utils/ConvertUtil/ConvertUtil.go @@ -21,19 +21,36 @@ func init() { ffmpeg = runDir + `\ffmpeg\ffmpeg.exe` } -const CutPath = "Cut/" -const Mp4Path = "Mp4/" -const AllPath = "/" +const WorkingPath = "/" +const WorkingCutPath = "Cut/" +const WorkingMp4Path = "Mp4/" /** 功能:获取指定源文件的不同工作目录 作者:黄海 时间:2020-07-09 */ -func getPath(source string, p string) string { +func getWorkingPath(source string, p string) string { return runDir + `/Working/` + source[0:2] + "/" + source[0:36] + "/" + p } +/** +功能:获取目标目录 +作者:黄海 +时间:2020-07-09 + */ +func getTargetPath(source string)string{ + return runDir + `/Target/` + source[0:2] + "/" + source[0:36] + "/" +} +/** +功能:获取源目录 +作者:黄海 +时间:2020-07-09 +*/ +func getSourcePath(source string) string{ + return runDir + `/Source/` + source[0:2] + "/" + source[0:36] + "/" +} + /** 功能:初始化目录 作者:黄海 @@ -41,10 +58,10 @@ func getPath(source string, p string) string { */ func InitDir(source string) { //1、删除旧目录 - os.RemoveAll(getPath(source, AllPath)) + os.RemoveAll(getWorkingPath(source, WorkingPath)) //2、创建新目录 - os.MkdirAll(getPath(source, CutPath), os.ModePerm) - os.Mkdir(getPath(source, Mp4Path), os.ModePerm) + os.MkdirAll(getWorkingPath(source, WorkingCutPath), os.ModePerm) + os.Mkdir(getWorkingPath(source, WorkingMp4Path), os.ModePerm) } /** @@ -54,13 +71,13 @@ func InitDir(source string) { */ func Cut(source string) { //1、源视频文件 - sourcePath := runDir + "/Source/" + source[0:2] + "/" + source + sourcePath := getSourcePath(source) + source //2、获取文件后缀 extension := path.Ext(source) //3、输出的文件通配名称 - targetPath := getPath(source, CutPath) + source[0:36] + `_%03d` + extension + targetPath := getWorkingPath(source, WorkingCutPath) + source[0:36] + `_%03d` + extension //4、切片 - CommonUtil.Exec(ffmpeg, runDir+"/Source/"+source[0:2]+"/", `-fflags`, `+genpts`, `-i`, sourcePath, + CommonUtil.Exec(ffmpeg, getSourcePath(source), `-fflags`, `+genpts`, `-i`, sourcePath, `-acodec`, `copy`, `-vcodec`, `copy`, `-f`, `segment`, `-segment_time`, `300`, `-reset_timestamps`, `1`, `-map`, `0:0`, `-map`, `0:1`, targetPath) } @@ -71,7 +88,7 @@ func Cut(source string) { 时间:2020-07-08 */ func GetAllCutChild(source string) []string { - filepathNames, err := filepath.Glob(filepath.Join(getPath(source, CutPath), "*")) + filepathNames, err := filepath.Glob(filepath.Join(getWorkingPath(source, WorkingCutPath), "*")) if err != nil { fmt.Println(err.Error()) } @@ -90,8 +107,8 @@ func GetAllCutChild(source string) []string { func ConvertToH264Mp4(childMovie string) { extension := path.Ext(childMovie) childMovieMp4 := strings.Replace(filepath.Base(childMovie), extension, ".mp4", -1) - CommonUtil.Exec(ffmpeg, getPath(childMovieMp4, CutPath), `-i`, childMovie, `-c:v`, `libx264`, `-strict`, `-2`, - getPath(childMovieMp4, Mp4Path)+childMovieMp4) + CommonUtil.Exec(ffmpeg, getWorkingPath(childMovieMp4, WorkingCutPath), `-i`, childMovie, `-c:v`, `libx264`, `-strict`, `-2`, + getWorkingPath(childMovieMp4, WorkingMp4Path)+childMovieMp4) } /** @@ -108,7 +125,7 @@ func GenerateIndexTxt(source string, childMovie []string) string { content = append(content, `file '`+filenameWithSuffix+`'`) } //文件位置 - indexName := getPath(source, Mp4Path) + source[0:36] + ".txt" + indexName := getWorkingPath(source, WorkingMp4Path) + source[0:36] + ".txt" FileUtil.WriteLines(content, indexName) return indexName } @@ -119,20 +136,16 @@ func GenerateIndexTxt(source string, childMovie []string) string { 时间:2020-07-08 */ func Merge(source string) { - Target := runDir + `/Target/` + source[0:2] + "/" - if !FileUtil.Exists(Target) { - os.Mkdir(Target, os.ModePerm) - } - Target = Target + source[0:36] + "/" + Target := getTargetPath(source) if !FileUtil.Exists(Target) { - os.Mkdir(Target, os.ModePerm) + os.MkdirAll(Target, os.ModePerm) } if FileUtil.Exists(Target + "/" + source[0:36] + ".mp4") { //删除 os.Remove(Target + "/" + source[0:36] + ".mp4") } //合并 - CommonUtil.Exec(ffmpeg, getPath(source, Mp4Path), `-f`, `concat`, `-i`, source[0:36]+".txt", `-c`, `copy`, Target+"/"+source[0:36]+".mp4") + CommonUtil.Exec(ffmpeg, getWorkingPath(source, WorkingMp4Path), `-f`, `concat`, `-i`, source[0:36]+".txt", `-c`, `copy`, Target+"/"+source[0:36]+".mp4") } /** @@ -154,7 +167,7 @@ func ClearRubbish(source string) { 时间:2020-07-09 */ func ShowMovieInfo(source string) { - CommonUtil.Exec(ffmpeg, runDir+`/Target/`+source[0:2]+"/"+source[0:36]+"/", `-i`, runDir+`/Target/`+source[0:2]+"/"+source[0:36]+"/"+source[0:36]+".mp4") + CommonUtil.Exec(ffmpeg, getTargetPath(source), `-i`,getTargetPath(source)+source[0:36]+".mp4") } /** @@ -163,11 +176,11 @@ func ShowMovieInfo(source string) { 时间:2020-07-09 */ func ToM3u8(source string) { - CommonUtil.Exec(ffmpeg, runDir+`/Target/`+source[0:2]+"/"+source[0:36]+"/", `-i`, - runDir+`/Target/`+source[0:2]+"/"+source[0:36]+"/"+source[0:36]+".mp4", + CommonUtil.Exec(ffmpeg, getTargetPath(source), `-i`, + getTargetPath(source)+source[0:36]+".mp4", `-f`,`segment`, `-segment_time`, `60`, `-segment_format`, `mpegts`, `-segment_list`, - runDir+`/Target/`+source[0:2]+"/"+source[0:36]+"/"+source+`.m3u8`, `-c`, `copy`, `-bsf:v`, `h264_mp4toannexb`, - `-map`, `0`, runDir+`/Target/`+source[0:2]+"/"+source[0:36]+"/"+source[0:36]+`_%04d.ts`) + getTargetPath(source)+source[0:36]+"/"+source+`.m3u8`, `-c`, `copy`, `-bsf:v`, `h264_mp4toannexb`, + `-map`, `0`, getTargetPath(source)+source[0:36]+`_%04d.ts`) //删除mp4 - os.Remove(runDir+`/Target/`+source[0:2]+"/"+source[0:36]+"/"+source[0:36]+".mp4") + os.Remove(getTargetPath(source)+source[0:36]+".mp4") }