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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试