drsl90685154 2016-08-17 17:57
浏览 247
已采纳

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.

  • 写回答

1条回答 默认 最新

  • douzhang2680 2016-08-17 18:06
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?