先按照ffmpeg的安装攻略,搞定你CentOS上的ffmpeg,我目前使用的版本是3.0.2
然后直接上代码package mainimport ( "fmt" "log" "os" "os/exec" "time")func main() { var srcFileName, outputfilename string log.SetFlags(log.Lshortfile | log.Ldate | log.Ltime) if len(os.Args) != 3 { fmt.Println("简单的转码服务,利用FFMPEG和编码器转换成MP4.") fmt.Println("使用:[输入文件路径] [输出文件路径]") os.Exit(1) } else { srcFileName = os.Args[1] outputfilename = os.Args[2] } param := "ffmpeg -y -i " + srcFileName + " -metadata:s:v rotate=0 -vf fps=15,setdar=dar=1 -s 480x480 -pix_fmt yuv420p -strict -2 -c:v h264 -b:v 500k -b:a 48k -ss 0 -t 300 -threads 2 " + outputfilename + "" fmt.Println("parm : ", param) start := time.Now() _, err := exec.Command("bash", "-c", param).CombinedOutput() if err != nil { fmt.Println("error: " + err.Error()) } else { fmt.Println(" out file : ", outputfilename, " exec time ", time.Now().Sub(start).Seconds()) }}
执行下:
go run main.go simple.mov simple_1.mp4