0

I used the following code to generate a .mp4 file:

    args := []string{"-i", "rtsp://zigong.stream.xl02.cn:557/HongTranSvr?DevId=1b038d27-858c-46a1-b803-a2984af343df&Session=1b038d27-858c-46a1-b803-a2984af343df",
        "-vcodec", "copy", "-t", "5", "-y", "output.mp4"}
    command := exec.Command("ffmpeg", args...)
    bytes, err := command.Output()
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(len(bytes))  //result:0

Is there a way to get the []byte format of the output.mp4?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

1

Use the pipe:1 for the ouput instead of a file path. The example below shows you how to access to the FFmpeg command output and the generated file.

var stdOut bytes.Buffer
var stdErr bytes.Buffer

args := []string{"-i", "test.mp4", "-vcodec", "copy", "-t", "5", "-f", "mp4", "pipe:1"}
command := exec.Command("ffmpeg", args...)
command.Stdout = bufio.NewWriter(&stdOut)
command.Stderr = bufio.NewWriter(&stdErr)
if err := command.Run(); err != nil {
    log.Println(err)
    log.Println(string(stdErr.Bytes()))
    return
}

// FFmpeg log
log.Println(string(stdErr.Bytes()))

// data from FFmpeg
log.Println(len(stdOut.Bytes()))

The problem with mp4 is that FFmpeg can not directly output it, because the metadata of the file is written at the beginning of the file but must be written at the end of the encoding. Follow this link for more information: FFMPEG: Transmux mpegts to mp4 gives error: muxer does not support non seekable output

martinr92
  • 588
  • 1
  • 7
  • 15