master
huanghai 5 years ago
parent e733e6a67f
commit 1ad84b79f1

@ -21,19 +21,36 @@ func init() {
ffmpeg = runDir + `\ffmpeg\ffmpeg.exe` ffmpeg = runDir + `\ffmpeg\ffmpeg.exe`
} }
const CutPath = "Cut/" const WorkingPath = "/"
const Mp4Path = "Mp4/" const WorkingCutPath = "Cut/"
const AllPath = "/" const WorkingMp4Path = "Mp4/"
/** /**
2020-07-09 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 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) { func InitDir(source string) {
//1、删除旧目录 //1、删除旧目录
os.RemoveAll(getPath(source, AllPath)) os.RemoveAll(getWorkingPath(source, WorkingPath))
//2、创建新目录 //2、创建新目录
os.MkdirAll(getPath(source, CutPath), os.ModePerm) os.MkdirAll(getWorkingPath(source, WorkingCutPath), os.ModePerm)
os.Mkdir(getPath(source, Mp4Path), os.ModePerm) os.Mkdir(getWorkingPath(source, WorkingMp4Path), os.ModePerm)
} }
/** /**
@ -54,13 +71,13 @@ func InitDir(source string) {
*/ */
func Cut(source string) { func Cut(source string) {
//1、源视频文件 //1、源视频文件
sourcePath := runDir + "/Source/" + source[0:2] + "/" + source sourcePath := getSourcePath(source) + source
//2、获取文件后缀 //2、获取文件后缀
extension := path.Ext(source) extension := path.Ext(source)
//3、输出的文件通配名称 //3、输出的文件通配名称
targetPath := getPath(source, CutPath) + source[0:36] + `_%03d` + extension targetPath := getWorkingPath(source, WorkingCutPath) + source[0:36] + `_%03d` + extension
//4、切片 //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`, `-acodec`, `copy`, `-vcodec`, `copy`, `-f`, `segment`, `-segment_time`,
`300`, `-reset_timestamps`, `1`, `-map`, `0:0`, `-map`, `0:1`, targetPath) `300`, `-reset_timestamps`, `1`, `-map`, `0:0`, `-map`, `0:1`, targetPath)
} }
@ -71,7 +88,7 @@ func Cut(source string) {
2020-07-08 2020-07-08
*/ */
func GetAllCutChild(source string) []string { 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 { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
} }
@ -90,8 +107,8 @@ func GetAllCutChild(source string) []string {
func ConvertToH264Mp4(childMovie string) { func ConvertToH264Mp4(childMovie string) {
extension := path.Ext(childMovie) extension := path.Ext(childMovie)
childMovieMp4 := strings.Replace(filepath.Base(childMovie), extension, ".mp4", -1) childMovieMp4 := strings.Replace(filepath.Base(childMovie), extension, ".mp4", -1)
CommonUtil.Exec(ffmpeg, getPath(childMovieMp4, CutPath), `-i`, childMovie, `-c:v`, `libx264`, `-strict`, `-2`, CommonUtil.Exec(ffmpeg, getWorkingPath(childMovieMp4, WorkingCutPath), `-i`, childMovie, `-c:v`, `libx264`, `-strict`, `-2`,
getPath(childMovieMp4, Mp4Path)+childMovieMp4) getWorkingPath(childMovieMp4, WorkingMp4Path)+childMovieMp4)
} }
/** /**
@ -108,7 +125,7 @@ func GenerateIndexTxt(source string, childMovie []string) string {
content = append(content, `file '`+filenameWithSuffix+`'`) 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) FileUtil.WriteLines(content, indexName)
return indexName return indexName
} }
@ -119,20 +136,16 @@ func GenerateIndexTxt(source string, childMovie []string) string {
2020-07-08 2020-07-08
*/ */
func Merge(source string) { func Merge(source string) {
Target := runDir + `/Target/` + source[0:2] + "/" Target := getTargetPath(source)
if !FileUtil.Exists(Target) {
os.Mkdir(Target, os.ModePerm)
}
Target = Target + source[0:36] + "/"
if !FileUtil.Exists(Target) { if !FileUtil.Exists(Target) {
os.Mkdir(Target, os.ModePerm) os.MkdirAll(Target, os.ModePerm)
} }
if FileUtil.Exists(Target + "/" + source[0:36] + ".mp4") { if FileUtil.Exists(Target + "/" + source[0:36] + ".mp4") {
//删除 //删除
os.Remove(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 2020-07-09
*/ */
func ShowMovieInfo(source string) { 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 2020-07-09
*/ */
func ToM3u8(source string) { func ToM3u8(source string) {
CommonUtil.Exec(ffmpeg, runDir+`/Target/`+source[0:2]+"/"+source[0:36]+"/", `-i`, CommonUtil.Exec(ffmpeg, getTargetPath(source), `-i`,
runDir+`/Target/`+source[0:2]+"/"+source[0:36]+"/"+source[0:36]+".mp4", getTargetPath(source)+source[0:36]+".mp4",
`-f`,`segment`, `-segment_time`, `60`, `-segment_format`, `mpegts`, `-segment_list`, `-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`, getTargetPath(source)+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`) `-map`, `0`, getTargetPath(source)+source[0:36]+`_%04d.ts`)
//删除mp4 //删除mp4
os.Remove(runDir+`/Target/`+source[0:2]+"/"+source[0:36]+"/"+source[0:36]+".mp4") os.Remove(getTargetPath(source)+source[0:36]+".mp4")
} }

Loading…
Cancel
Save