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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?