英文:
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 "video1.ts" -c:v libx264 -crf 20 -c:a aac -strict -2 "video1-fix.ts"
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
&{/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>}
Here below is my code
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)
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 "\"video-1.ts\""
- your quotes is shell feature.
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())
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论