美文网首页
golang 运行ffmpeg命令

golang 运行ffmpeg命令

作者: 付少_Flj | 来源:发表于2020-07-28 10:12 被阅读0次

    golang 运行ffmpeg命令

    系统命令

    package main
    
    import (
        "bytes"
        "fmt"
        "os/exec"
    )
    
    func main() {
        in := bytes.NewBuffer(nil)
        //linux
        //cmd := exec.Command("sh")
        //windows
        cmd := exec.Command("cmd")
        cmd.Stdin = in
        go func() {
            in.WriteString("echo hello world > test.txt\n")
            in.WriteString("exit\n")
        }()
        if err := cmd.Run(); err != nil {
            fmt.Println(err)
            return
        }
    }
    

    ffmpeg转码

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

    ffmpeg录音

    录音设备名由命令获取

    ffmpeg -list_devices true -f dshow -i dummy
    ffmpeg version git-2020-04-22-2e38c63 Copyright (c) 2000-2020 the FFmpeg developers
      built with gcc 9.3.1 (GCC) 20200328
      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 
    --enable-amf
      libavutil      56. 42.102 / 56. 42.102
      libavcodec     58. 80.100 / 58. 80.100
      libavformat    58. 42.101 / 58. 42.101
      libavdevice    58.  9.103 / 58.  9.103
      libavfilter     7. 79.100 /  7. 79.100
      libswscale      5.  6.101 /  5.  6.101
      libswresample   3.  6.100 /  3.  6.100
      libpostproc    55.  6.100 / 55.  6.100
    [dshow @ 000001d6f042bbc0] DirectShow video devices (some may be both video and audio devices)
    [dshow @ 000001d6f042bbc0]  "Integrated Webcam"
    [dshow @ 000001d6f042bbc0]     Alternative name "@device_pnp_\\?\usb#vid_1bcf&pid_2b95&mi_00#6&1ca786e2&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global"
    [dshow @ 000001d6f042bbc0] DirectShow audio devices
    [dshow @ 000001d6f042bbc0]  "Line-In/Mic-In (SB X-Fi Surround 5.1)"
    [dshow @ 000001d6f042bbc0]     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{FC795785-0F3D-483E-94A3-B8A1ADAC0520}"
    [dshow @ 000001d6f042bbc0]  "麦克风 (Realtek(R) Audio)"
    [dshow @ 000001d6f042bbc0]     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{34BE0A01-C298-4CAA-88F7-87EFA5B666A6}"
    [dshow @ 000001d6f042bbc0]  "What U Hear (SB X-Fi Surround 5.1)"
    [dshow @ 000001d6f042bbc0]     Alternative name "@device_cm_{33D9A762-90C8-11D0-BD43-00A0C911CE86}\wave_{CC0A7A5D-9D3E-4AE7-8B81-89617B15CF5E}"
    

    录音

    func main() {
        in := bytes.Buffer{}
        outInfo := bytes.Buffer{}
        cmd := exec.Command("cmd")
        //cmd := exec.Command("ping", "www.163.com")
    
        cmd.Stdout = &outInfo
        cmd.Stdin = &in
        //in.WriteString("echo hello world > test.txt\n")
        // go func() {
        //  //in.WriteString("ffmpeg -f dshow -i  audio=麦克风 (Realtek(R) Audio) -ar 16000 -ac 1 -t 00:00:05 test.wav\n")
        // }()
    
        //命令写入cmd.Stdin
        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")
    
        //in.WriteString("ping www.baidu.com\n")
        //in.WriteString("echo hello world\n")
        // if err := cmd.Run(); err != nil {
        //  fmt.Println(err)
        //  return
        // }
    
    
        err := cmd.Start()
        if err != nil {
            fmt.Println(err.Error())
        }
        if err = cmd.Wait(); err != nil {
            fmt.Println(err.Error())
        } else {
    
            fmt.Println(cmd.ProcessState.Pid())
            //程序退出code
            fmt.Println(cmd.ProcessState.Sys().(syscall.WaitStatus).ExitCode)
            //输出结果
            fmt.Println(outInfo.String())
        }
    }
    

    相关文章

      网友评论

          本文标题:golang 运行ffmpeg命令

          本文链接:https://www.haomeiwen.com/subject/xooxrktx.html