|
|
package ConvertUtil
|
|
|
|
|
|
import (
|
|
|
"dsSupport/Utils/CommonUtil"
|
|
|
"dsSupport/Utils/FileUtil"
|
|
|
"fmt"
|
|
|
"os"
|
|
|
"path"
|
|
|
"path/filepath"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
//运行的目录
|
|
|
var runDir string
|
|
|
|
|
|
// ffmpeg 的命令行位置
|
|
|
var ffmpeg string
|
|
|
|
|
|
func init() {
|
|
|
runDir, _ = os.Getwd()
|
|
|
//ffmpeg = runDir + `\ffmpeg\ffmpeg.exe`
|
|
|
ffmpeg = `/usr/local/ffmpeg/bin/ffmpeg`
|
|
|
}
|
|
|
|
|
|
// https://blog.csdn.net/lijinshanba/article/details/95343634
|
|
|
|
|
|
const WorkingPath = "/"
|
|
|
const WorkingCutPath = "/Cut/"
|
|
|
const WorkingMp4Path = "/Mp4/"
|
|
|
const WorkingKeyFramePath="/KeyFrame/"
|
|
|
|
|
|
/**
|
|
|
功能:获取指定源文件的不同工作目录
|
|
|
作者:黄海
|
|
|
时间:2020-07-09
|
|
|
*/
|
|
|
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] + "/"
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:初始化目录
|
|
|
作者:黄海
|
|
|
时间:2020-07-09
|
|
|
*/
|
|
|
func InitDir(source string) {
|
|
|
//1、删除旧目录
|
|
|
os.RemoveAll(getWorkingPath(source, WorkingPath))
|
|
|
//2、创建新目录
|
|
|
os.MkdirAll(getWorkingPath(source, WorkingCutPath), os.ModePerm)
|
|
|
os.Mkdir(getWorkingPath(source, WorkingMp4Path), os.ModePerm)
|
|
|
os.Mkdir(getWorkingPath(source,WorkingKeyFramePath),os.ModePerm)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:对指定的视频文件进行切片
|
|
|
作者:黄海
|
|
|
时间:2020-07-08
|
|
|
*/
|
|
|
func Cut(source string) {
|
|
|
//1、源视频文件
|
|
|
sourcePath := getWorkingPath(source,WorkingKeyFramePath) + source
|
|
|
//2、获取文件后缀
|
|
|
extension := path.Ext(source)
|
|
|
//3、输出的文件通配名称
|
|
|
targetPath := getWorkingPath(source, WorkingCutPath) + source[0:36] + `_%03d` + extension
|
|
|
//4、切片
|
|
|
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)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:将指定的视频文件,在切片后,获取都生成了哪些切片
|
|
|
作者:黄海
|
|
|
时间:2020-07-08
|
|
|
*/
|
|
|
func GetAllCutChild(source string) []string {
|
|
|
filepathNames, err := filepath.Glob(filepath.Join(getWorkingPath(source, WorkingCutPath), "*"))
|
|
|
if err != nil {
|
|
|
fmt.Println(err.Error())
|
|
|
}
|
|
|
resultArray := make([]string, 0)
|
|
|
for i := range filepathNames {
|
|
|
resultArray = append(resultArray, filepathNames[i])
|
|
|
}
|
|
|
return resultArray
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:将视频转为H264格式的MP4
|
|
|
作者: 黄海
|
|
|
时间:2020-07-08
|
|
|
*/
|
|
|
func ConvertToH264Mp4(childMovie string) {
|
|
|
extension := path.Ext(childMovie)
|
|
|
childMovieMp4 := strings.Replace(filepath.Base(childMovie), extension, ".mp4", -1)
|
|
|
CommonUtil.Exec(ffmpeg, getWorkingPath(childMovieMp4, WorkingCutPath), `-i`, childMovie, `-c:v`, `libx264`, `-strict`, `-2`,
|
|
|
getWorkingPath(childMovieMp4, WorkingMp4Path)+childMovieMp4)
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:生成索引的文本文件
|
|
|
作者:黄海
|
|
|
时间:2020-07-08
|
|
|
*/
|
|
|
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)
|
|
|
filenameWithSuffix := filepath.Base(fullFilename) //获取文件名带后缀
|
|
|
content = append(content, `file '`+filenameWithSuffix+`'`)
|
|
|
}
|
|
|
//文件位置
|
|
|
indexName := getWorkingPath(source, WorkingMp4Path) + source[0:36] + ".txt"
|
|
|
FileUtil.WriteLines(content, indexName)
|
|
|
return indexName
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:合并视频
|
|
|
作者:黄海
|
|
|
时间:2020-07-08
|
|
|
*/
|
|
|
func Merge(source string) {
|
|
|
Target := getTargetPath(source)
|
|
|
if !FileUtil.Exists(Target) {
|
|
|
os.MkdirAll(Target, os.ModePerm)
|
|
|
}
|
|
|
if FileUtil.Exists(Target + "/" + source[0:36] + ".mp4") {
|
|
|
//删除
|
|
|
os.Remove(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")
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:清除垃圾
|
|
|
作者:黄海
|
|
|
时间:2020-07-08
|
|
|
*/
|
|
|
func ClearRubbish(source string) {
|
|
|
var deletePath = []string{"Working"}
|
|
|
for i := range deletePath {
|
|
|
//删除目录
|
|
|
os.RemoveAll(runDir + `/` + deletePath[i] + `/` + source[0:2] + "/" + source[0:36])
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:显示视频的信息
|
|
|
作者:黄海
|
|
|
时间:2020-07-09
|
|
|
*/
|
|
|
func ShowMovieInfo(source string) {
|
|
|
CommonUtil.Exec(ffmpeg, getTargetPath(source), `-i`,getTargetPath(source)+source[0:36]+".mp4")
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:将转码H264完成的MP4,切成M3U8形式
|
|
|
作者:黄海
|
|
|
时间:2020-07-09
|
|
|
*/
|
|
|
func ToM3u8(source string) {
|
|
|
CommonUtil.Exec(ffmpeg, getTargetPath(source), `-i`,
|
|
|
getTargetPath(source)+source[0:36]+".mp4",
|
|
|
`-f`,`segment`, `-segment_time`, `60`, `-segment_format`, `mpegts`, `-segment_list`,
|
|
|
getTargetPath(source)+source[0:36]+`.m3u8`, `-c`, `copy`, `-bsf:v`, `h264_mp4toannexb`,
|
|
|
`-map`, `0`, getTargetPath(source)+source[0:36]+`_%04d.ts`)
|
|
|
//删除mp4
|
|
|
os.Remove(getTargetPath(source)+source[0:36]+".mp4")
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
功能:将文件转换为每帧都是关键帧
|
|
|
作者:黄海
|
|
|
时间:2020-07-09
|
|
|
*/
|
|
|
func ConvertToKeyFrame(source string){
|
|
|
if FileUtil.Exists(getWorkingPath(source,WorkingKeyFramePath)+source){
|
|
|
os.Remove(getWorkingPath(source,WorkingKeyFramePath)+source)
|
|
|
}
|
|
|
CommonUtil.Exec(ffmpeg, getWorkingPath(source,WorkingKeyFramePath), `-i`,
|
|
|
getSourcePath(source)+source,`-strict`, `-2`, `-qscale`, `0`,`-intra`, getWorkingPath(source,WorkingKeyFramePath)+source)
|
|
|
} |