Golang exec.Command() 错误 – 通过 Golang 运行 ffmpeg 命令

huangapple go评论103阅读模式
英文:

Golang exec.Command() error - ffmpeg command through golang

问题

目前正在使用以下ffmpeg命令来编辑视频:

ffmpeg -i "video1.ts" -c:v libx264 -crf 20 -c:a aac -strict -2 "video1-fix.ts"

当我在终端中输入它时,它可以正常工作。但是,当我尝试使用Golang的exec.Command()函数时,我得到了以下错误响应:

&{/usr/local/bin/ffmpeg [ffmpeg -i "video1.ts" -c:v libx264 -crf 20 -c:a aac -strict -2 "video1-fix.ts"] []  <nil> <nil> <nil> [] <nil> <nil> <nil> <nil> <nil> false [] [] [] [] <nil> <nil>}

以下是我的代码:

cmdArguments := []string{"-i", "\"video-1.ts\"", "-c:v", "libx264",
			 "-crf", "20", "-c:a", "aac", "-strict", "-2", "\"video1-fix.ts\""}

err := exec.Command("ffmpeg", cmdArguments...)
fmt.Println(err)

我是否在命令语法中漏掉了什么?不确定为什么它无法加载视频。

英文:

Currently working with this ffmpeg command to edit video

ffmpeg -i &quot;video1.ts&quot; -c:v libx264 -crf 20 -c:a aac -strict -2 &quot;video1-fix.ts&quot;

When I enter it in the terminal, it works. However when I try to use the Golang exec.Command() func, I get err response of

&amp;{/usr/local/bin/ffmpeg [ffmpeg -i &quot;video1.ts&quot; -c:v libx264 -crf 20 -c:a aac -strict -2 &quot;video1-fix.ts&quot;] []  &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; [] &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; &lt;nil&gt; false [] [] [] [] &lt;nil&gt; &lt;nil&gt;}

Here below is my code

cmdArguments := []string{&quot;-i&quot;, &quot;\&quot;video-1.ts\&quot;&quot;, &quot;-c:v&quot;, &quot;libx264&quot;,
		 &quot;-crf&quot;, &quot;20&quot;, &quot;-c:a&quot;, &quot;aac&quot;, &quot;-strict&quot;, &quot;-2&quot;, &quot;\&quot;video1-fix.ts\&quot;&quot;}

err := exec.Command(&quot;ffmpeg&quot;, cmdArguments...)
fmt.Println(err)

Am i missing something from my command syntax? Not sure why it is not loading the videos

答案1

得分: 3

根据@JimB的说法,exec.Command不会返回错误。
以下是从https://golang.org/pkg/os/exec/#Command示例中更改的代码:

顺便说一下,你不需要使用"video-1.ts" - 这是shell的特性。

package main

import (
	"bytes"
	"fmt"
	"log"
	"os/exec"
)

func main() {
    cmdArguments := []string{"-i", "video-1.ts", "-c:v", "libx264",
     "-crf", "20", "-c:a", "aac", "-strict", "-2", "video1-fix.ts"}

	cmd := exec.Command("tr", cmdArguments...)

	var out bytes.Buffer
	cmd.Stdout = &out
	err := cmd.Run()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("command output: %q\n", out.String())
}

希望对你有帮助!

英文:

as @JimB says, exec.Command does not return an error.
Here is changed code from example https://golang.org/pkg/os/exec/#Command

By the way you dont need to use &quot;\&quot;video-1.ts\&quot;&quot; - your quotes is shell feature.

package main

import (
	&quot;bytes&quot;
	&quot;fmt&quot;
	&quot;log&quot;
	&quot;os/exec&quot;
)

func main() {
    cmdArguments := []string{&quot;-i&quot;, &quot;video-1.ts&quot;, &quot;-c:v&quot;, &quot;libx264&quot;,
     &quot;-crf&quot;, &quot;20&quot;, &quot;-c:a&quot;, &quot;aac&quot;, &quot;-strict&quot;, &quot;-2&quot;, &quot;video1-fix.ts&quot;}

	cmd := exec.Command(&quot;tr&quot;, cmdArguments...)

	var out bytes.Buffer
	cmd.Stdout = &amp;out
	err := cmd.Run()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf(&quot;command output: %q\n&quot;, out.String())
}

huangapple
  • 本文由 发表于 2017年8月15日 09:59:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/45685483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定