2016-09-14 180 views
0

我试图从here中获取示例,用于使用phantomjs记录网页,并将标准输出(它们是图像)传送到ffmpeg命令以创建视频。命令说,你需要运行的是:在Golang中使用管道运行命令exec

phantomjs runner.js | ffmpeg -y -c:v png -f image2pipe -r 25 -t 10 -i - -c:v libx264 -pix_fmt yuv420p -movflags +faststart dragon.mp4 

如果我直接在终端运行该命令的一个类似的版本,我可以得到它的工作就好了。问题是我需要通过Golang os/exec包运行上述命令。使用:

cmd := exec.Command(parts[0], parts[1:]...) 

方法,第一个参数是正在执行的命令的基本可执行文件,它不遵守管道。我想用一个命令得到这个工作,所以我不必将所有图像写入文件,然后运行第二个ffmpeg命令来读取所有这些图像。有什么建议么?

+1

pipe是一个shell构造,而不是命令参数,请参阅http://stackoverflow.com/a/10953142/32880 – JimB

回答

2

我认为这会对你有所帮助。

 cmd := "phantomjs runner.js | ffmpeg -y -c:v png -f image2pipe -r 25 -t 10 -i - -c:v libx264 -pix_fmt yuv420p -movflags +faststart dragon.mp4" 
    output, err := exec.Command("bash","-c",cmd).Output() 
    if err != nil { 
      return fmt.Sprintf("Failed to execute command: %s", cmd) 
    } 
    fmt.Println(string(output))