ffmpeg_cmd.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os/exec"
  6. "strings"
  7. )
  8. const (
  9. CMD = `ffmpeg`
  10. FileName = `./ffmpeg/mp4/wph.mp4`
  11. TEMPDir = `./ffmpeg/output/`
  12. AudioPickUpArgs = `-i %s -y -vn -acodec copy %s` // 音频提取
  13. VideoPickUpArgs = `-i %s -y -an -vcodec copy %s` // 视频提取
  14. SRTPickUpArgs = `-i %s -map 0:s:0 %s` // 文本字幕提取 or `-i %s -an -vn -bsf:s mov2textsub -scodec copy -f rawvideo %s` or `-i %s -an -vn -c:s copy -f rawvideo -map 0:s %s`
  15. ASSPickUpArgs = `-i %s -an -vn -scodec copy %s` // ASS字幕提取
  16. VideoAndAudioMergeArgs = `-i %s -i %s -y -vcodec copy -acodec copy %s` // 音视频混编
  17. SRTMapArgs = `-i %s -i %s -c copy -c:s mov_text %s` // 增加SRT字幕
  18. ASSMapArgs = `-i %s -i %s -map 0:0 -map 0:1 -map 1 -c:a copy -c:v copy -c:s copy %s` // 增加ASS字幕
  19. PushFlv = `-re -i %s -c copy -f flv %s` // 推Flv流
  20. )
  21. func main() {
  22. //_ = os.RemoveAll(TEMPDir)
  23. //_ = os.Mkdir(TEMPDir, os.ModePerm)
  24. //runCmd(CMD, strings.Split(fmt.Sprintf(AudioPickUpArgs, FileName, TEMPDir+`wph.aac`), " "))
  25. //runCmd(CMD, strings.Split(fmt.Sprintf(VideoPickUpArgs, FileName, TEMPDir+`wph.mp4`), " "))
  26. //if path.Ext(FileName) == "mkv" { // mkv文件才能提取出字幕流,mp4通常会报错
  27. // runCmd(CMD, strings.Split(fmt.Sprintf(SRTPickUpArgs, FileName, TEMPDir+`wph.txt`), " "))
  28. //}
  29. //runCmd(CMD, strings.Split(fmt.Sprintf(VideoAndAudioMergeArgs, TEMPDir+`wph.mp4`, TEMPDir+`wph.aac`, TEMPDir+`wph1.mp4`), " "))
  30. runCmd(CMD, strings.Split(fmt.Sprintf(PushFlv, FileName, `rtmp://localhost:1935/live/test`), ` `))
  31. }
  32. func runCmd(name string, args []string) {
  33. cmd := exec.Command(name, args...)
  34. buf := new(bytes.Buffer)
  35. cmd.Stdout = buf
  36. err := cmd.Run()
  37. if nil != err {
  38. panic(err.Error())
  39. } else {
  40. println(buf.String())
  41. }
  42. }