Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running command with pipe in Golang exec

Tags:

go

ffmpeg

I am trying to get the example from here working to record a webpage with phantomjs and pipe the stdout, which are images, to the ffmpeg command to create the video. The command stated that you need to run is:

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

If I run a similar version of that command directly in the terminal, I can get it working just fine. The issue is that I need to run the above command through the Golang os/exec package. With the:

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

method, the first parameter is the base executable of the command being executed and it is not honoring the pipe. I would like to get this working in one command so I don't have to write all the images to files and then run a second ffmpeg command to read from all of those images. Any suggestions?

like image 808
jrkt Avatar asked Oct 26 '25 12:10

jrkt


1 Answers

I think this will help you.

     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))
like image 109
Kyaw Myint Thein Avatar answered Oct 29 '25 01:10

Kyaw Myint Thein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!