简体   繁体   English

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

[英]Golang command working in terminal but not with exec package

I am converting a video generation library from NodeJS to GO that primarily uses FFMPEG for all the video processing. 我正在将视频生成库从NodeJS转换为GO,该库主要将FFMPEG用于所有视频处理。 I already have all the FFMPEG commands written out to do the generation I want. 我已经写了所有FFMPEG命令来完成所需的生成。 The issue is that when I try to run the command through the os/exec package, it fails. 问题是,当我尝试通过os / exec软件包运行命令时,该命令失败。 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: 尝试运行此代码时遇到的ffmpeg错误是:

[AVFilterGraph @ 0x22a9bc0] No such filter: 'fade=in:0:15,fade=out:105:15' [AVFilterGraph @ 0x22a9bc0]没有这样的过滤器:“ fade = in:0:15,fade = out:105:15”

Error opening filters! 打开过滤器时出错!

2016/08/17 17:48:53 exit status 1 2016/08/17 17:48:53退出状态1

Like I previously stated, if I copy the EXACT command: 如前所述,如果我复制EXACT命令:

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. 请帮忙。

When you are using strings.Fields(command) , the fields are being split on spaces. 当您使用strings.Fields(command) ,字段将在空格上分割。 This results in the parts slice containing a value 'fade=in:0:15,fade=out:105:15' , with quotes. 这将导致parts切片包含带有引号的值'fade=in:0:15,fade=out:105:15' This complete value (with quotes) is being passed to the ffmpeg command, which the command is unable to recognize. 该完整值(带引号)将传递给ffmpeg命令,该命令无法识别。

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. 一个shell会去除这些引号,并只传递Go没做的字符串fade=in:0:15,fade=out:105:15 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM