无法执行 ffprobe 命令

Can't execute ffprobe command

我尝试使用 ffprobe 获取视频文件的持续时间。当我 运行 此代码时出现错误:

exit status 1:

var out bytes.Buffer
var stderr bytes.Buffer

cmdArgs := []string{"-i", "bunny.mp4", "-show_entries", "format=duration", "-v", "quiet", "-of", `csv="p=0"`}

cmd := exec.Command("ffprobe", cmdArguments...)
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
    fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
}
fmt.Printf("command output: %q\n", out.String())

但是当我像这样在没有 -of csv="p=0" 的情况下传递参数时:

cmdArgs := []string{"-i", "bunny.mp4", "-show_entries", "format=duration", "-v", "quiet"}

有效,returns 结果(但格式不正确):

command output: "[FORMAT]\nduration=3.008000\n[/FORMAT]\n"

那么问题是什么,如何解决呢?

尝试像这样格式化参数(对字符串使用双引号而不是反引号并删除内部双引号):

cmdArgs := []string{..., "csv=p=0"}

Go exec 包不调用系统 shell 来处理参数,因此您在指定它们时无需采取相同的预防措施。在这种情况下,无需将第一个“=”之后的部分用引号引起来。

来自the package documentation

Unlike the "system" library call from C and other languages, the os/exec package intentionally does not invoke the system shell and does not expand any glob patterns or handle other expansions, pipelines, or redirections typically done by shells. The package behaves more like C's "exec" family of functions. To expand glob patterns, either call the shell directly, taking care to escape any dangerous input, or use the path/filepath package's Glob function. To expand environment variables, use package os's ExpandEnv.