Golang命令在终端中运行,但不适用于exec包

问题描述:

I am converting a video generation library from NodeJS to GO that primarily uses FFMPEG for all the video processing. I already have all the FFMPEG commands written out to do the generation I want. The issue is that when I try to run the command through the os/exec package, it fails. However, if I copy the exact command and run it directly in the terminal it works and I cannot figure out why that is. My code that runs the command is below:

command := "ffmpeg -y -loop 1 -i image.png -vf 'fade=in:0:15,fade=out:105:15' -c:v mpeg2video -t 5 -s 1280x720 -r 30 -q:v 1 -preset ultrafast image.mpg"

parts := strings.Fields(command)
cmd := exec.Command(parts[0], parts[1:]...)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
err := cmd.Run()
if err != nil {
    panic(err)
}

The ffmpeg error I get when I try to run this code is:

[AVFilterGraph @ 0x22a9bc0] No such filter: 'fade=in:0:15,fade=out:105:15'

Error opening filters!

2016/08/17 17:48:53 exit status 1

Like I previously stated, if I copy the EXACT command:

ffmpeg -y -loop 1 -i image.png -vf 'fade=in:0:15,fade=out:105:15' -c:v mpeg2video -t 5 -s 1280x720 -r 30 -q:v 1 -preset ultrafast image.mpg

and run it directly in the terminal, it works no problem.

PLEASE HELP.

我正在将视频生成库从NodeJS转换为GO,该库主要将FFMPEG用于所有视频处理。 我已经写了所有FFMPEG命令来完成所需的生成。 问题是,当我尝试通过 os / exec 程序包运行命令时,它将失败 。 但是,如果我复制确切的命令并直接在终端中运行它,那么它将起作用,而且我无法弄清为什么。 我运行该命令的代码如下: p>

 命令:=“ ffmpeg -y -loop 1 -i image.png -vf'fade = in:0:15,fade  = out:105:15'-c:v mpeg2video -t 5 -s 1280x720 -r 30 -q:v 1-预设超快图像。mpg“ 
 
parts:= strings.Fields(command)
cmd:= exec  .Command(部件[0],部件[1:] ...)
cmd.Stderr = os.Stderr 
cmd.Stdout = os.Stdout 
err:= cmd.Run()
if err!= nil {\  n panic(err)
} 
  code>  pre> 
 
 

当我尝试运行此代码时遇到的ffmpeg错误是: p>

[AVFilterGraph @ 0x22a9bc0]没有这样的过滤器:'fade = in:0:15,fade = out:105:15' p>

打开过滤器时出错! p>

2016/08/17 17:48:53退出状态1 p> blockquote>

如我之前所述,如果我复制EXACT命令: / p>

  ffmpeg -y -loop 1 -i image.png -vf'fade = in:0:15,fade = out:105:15'-c:v mpeg2video -t  5 -s 1280x720 -r 30 -q:v 1-预设ultrafast image.mpg 
  code>  pre> 
 
 

并直接在终端中运行它,没问题。 p>

请帮助。 p> div>

When you are using strings.Fields(command), the fields are being split on spaces. This results in the parts slice containing a value 'fade=in:0:15,fade=out:105:15', with quotes. This complete value (with quotes) is being passed to the ffmpeg command, which the command is unable to recognize.

A shell would strip off these quotes and pass the string fade=in:0:15,fade=out:105:15 only which Go isn't doing. To fix, try:

// remove the quotes around fade=in:0:15,fade=out:105:15
command := "ffmpeg -y -loop 1 -i image.png -vf fade=in:0:15,fade=out:105:15 -c:v mpeg2video -t 5 -s 1280x720 -r 30 -q:v 1 -preset ultrafast image.mpg"

parts := strings.Fields(command)