1. package main
  2. /*
  3. #cgo CFLAGS: -IC:/ffmpeg/include
  4. #cgo LDFLAGS: -LC:/ffmpeg/lib/ -llibavformat -llibavcodec -llibavutil -llibavdevice -llibavfilter -llibswresample -llibswscale
  5. #include <libavformat/avformat.h>
  6. #include <libavcodec/avcodec.h>
  7. #include <libavutil/avutil.h>
  8. #include <libavutil/opt.h>
  9. #include <libavdevice/avdevice.h>
  10. static const AVStream *go_av_streams_get(const AVStream **streams,unsigned int n)
  11. {
  12. return streams[n];
  13. }
  14. */
  15. import "C"
  16. import (
  17. "fmt"
  18. )
  19. func main() {
  20. fmt.Println(C.avdevice_version())
  21. }

系统命令

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os/exec"
  6. )
  7. func main() {
  8. in := bytes.NewBuffer(nil)
  9. //linux
  10. //cmd := exec.Command("sh")
  11. //windows
  12. cmd := exec.Command("cmd")
  13. cmd.Stdin = in
  14. go func() {
  15. in.WriteString("echo hello world > test.txt\n")
  16. in.WriteString("exit\n")
  17. }()
  18. if err := cmd.Run(); err != nil {
  19. fmt.Println(err)
  20. return
  21. }
  22. }

ffmpeg转码

  1. func main() {
  2. cmdArguments := []string{"-i", "divx.avi", "-c:v", "libx264",
  3. "-crf", "20", "-c:a", "aac", "-strict", "-2", "video1-fix.ts"}
  4. cmd := exec.Command("ffmpeg", cmdArguments...)
  5. var out bytes.Buffer
  6. cmd.Stdout = &out
  7. err := cmd.Run()
  8. if err != nil {
  9. log.Fatal(err)
  10. }
  11. fmt.Printf("command output: %q", out.String())
  12. }

ffmpeg录音

  1. ffmpeg -list_devices true -f dshow -i dummy
  2. ffmpeg version git-2020-04-22-2e38c63 Copyright (c) 2000-2020 the FFmpeg developers
  3. built with gcc 9.3.1 (GCC) 20200328
  4. configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libsrt --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --disable-w32threads --enable-libmfx --enable-ffnvcodec --enable-cuda-llvm --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt
  5. --enable-amf
  6. libavutil 56. 42.102 / 56. 42.102
  7. libavcodec 58. 80.100 / 58. 80.100
  8. libavformat 58. 42.101 / 58. 42.101
  9. libavdevice 58. 9.103 / 58. 9.103
  10. libavfilter 7. 79.100 / 7. 79.100
  11. libswscale 5. 6.101 / 5. 6.101
  12. libswresample 3. 6.100 / 3. 6.100
  13. libpostproc 55. 6.100 / 55. 6.100
  14. [dshow @ 000001d6f042bbc0] DirectShow video devices (some may be both video and audio devices)
  15. [dshow @ 000001d6f042bbc0] "Integrated Webcam"
  16. [dshow @ 000001d6f042bbc0] Alternative name "@device_pnp_\\?\usb#vid_1bcf&pid_2b95&mi_00#6&1ca786e2&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"
  17. [dshow @ 000001d6f042bbc0] DirectShow audio devices
  18. [dshow @ 000001d6f042bbc0] "Line-In/Mic-In (SB X-Fi Surround 5.1)"
  19. [dshow @ 000001d6f042bbc0] Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{FC795785-0F3D-483E-94A3-B8A1ADAC0520}"
  20. [dshow @ 000001d6f042bbc0] "麦克风 (Realtek(R) Audio)"
  21. [dshow @ 000001d6f042bbc0] Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{34BE0A01-C298-4CAA-88F7-87EFA5B666A6}"
  22. [dshow @ 000001d6f042bbc0] "What U Hear (SB X-Fi Surround 5.1)"
  23. [dshow @ 000001d6f042bbc0] Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{CC0A7A5D-9D3E-4AE7-8B81-89617B15CF5E}"

录音

  1. func main() {
  2. in := bytes.Buffer{}
  3. outInfo := bytes.Buffer{}
  4. cmd := exec.Command("cmd")
  5. //cmd := exec.Command("ping", "www.163.com")
  6. cmd.Stdout = &outInfo
  7. cmd.Stdin = &in
  8. //in.WriteString("echo hello world > test.txt\n")
  9. // go func() {
  10. // //in.WriteString("ffmpeg -f dshow -i audio=麦克风 (Realtek(R) Audio) -ar 16000 -ac 1 -t 00:00:05 test.wav\n")
  11. // }()
  12. //命令写入cmd.Stdin
  13. in.WriteString("ffmpeg -f dshow -i audio=@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\\wave_{34BE0A01-C298-4CAA-88F7-87EFA5B666A6} -ar 16000 -ac 1 -t 00:00:05 test.wav -y \n")
  14. //in.WriteString("ping www.baidu.com\n")
  15. //in.WriteString("echo hello world\n")
  16. // if err := cmd.Run(); err != nil {
  17. // fmt.Println(err)
  18. // return
  19. // }
  20. err := cmd.Start()
  21. if err != nil {
  22. fmt.Println(err.Error())
  23. }
  24. if err = cmd.Wait(); err != nil {
  25. fmt.Println(err.Error())
  26. } else {
  27. fmt.Println(cmd.ProcessState.Pid())
  28. //程序退出code
  29. fmt.Println(cmd.ProcessState.Sys().(syscall.WaitStatus).ExitCode)
  30. //输出结果
  31. fmt.Println(outInfo.String())
  32. }
  33. }