2016-09-14 95 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))