You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package ConvertUtil
import (
"dsSupport/Utils/FileUtil"
"dsSupport/Utils/ShellUtil"
"fmt"
"os"
"path"
"path/filepath"
"strings"
)
//运行的目录
var runDir string
// ffmpeg 的命令行位置
var ffmpeg string
func init() {
runDir, _ = os.Getwd()
ffmpeg=runDir + `\ffmpeg\ffmpeg.exe`
}
/**
功能:对指定的视频文件进行切片
作者:黄海
时间2020-07-08
*/
func Cut(source string) {
//1、源视频文件
sourcePath := runDir + "/Source/" + source[0:2] + "/" + source
//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++
}
//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)
}
/**
功能:将指定的视频文件,在切片后,获取都生成了哪些切片
作者:黄海
时间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{
fmt.Println(err.Error())
}
return filepathNames
}
/**
功能将视频转为H264格式的MP4
作者: 黄海
时间2020-07-08
*/
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
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){
var deletePath=[]string{"Working","Target"}
for i := range deletePath {
//删除目录
os.RemoveAll(runDir+`/`+deletePath[i]+`/`+source[0:2]+"/"+source[0:36])
}
}