四季花海
您可以简单地将输出放入bytes.Buffer变量中,如下所示:cmd := exec.Command( "ffmpeg", "-y", "-loglevel", "warning", "-i", ConvertImage, "-i", videoInput, "-c:v", cv, "-c:a", audioCodec, "-crf", fmt.Sprintf("%d", crf), "-map", "[v]", "-map", "1:a?", "-r", fmt.Sprintf("%d", Res.FrameRate), "-strict", "-2", outputFile,)var execOut bytes.Buffervar execErr bytes.Buffercmd.Stdout = &execOutcmd.Stderr = &execErr通过这样做,输出和错误都在相应的缓冲区中。现在,如果您想打印它们,则可以将以下代码片段与上述代码一起使用:err := cmd.Run()if err != nil { fmt.Println("Cannot Execute cmd: ", err.Error())}outStr := execOut.String()errStr := execErr.String()if len(outStr) > 0 { fmt.Print(outStr)}if len(errStr) > 0 { fmt.Print(errStr)}更新:或者,如果您完全不需要 stdout 和 stderr,那么您可以将cmd.Stdout和设置cmd.Stderr为nil如下所示:cmd.Stdout = nilcmd.Stderr = nilerr := cmd.Run()if err != nil { fmt.Println("Cannot Execute cmd: ", err.Error())}