8

I'm trying to pipe screenshots generated by puppeteer to an ffmpeg process to make a video without writing to intermediate files first.

From the command line, I know ffmpeg has an option to make videos from raw data from stdin, for example this works:

cat img/*.png | ffmpeg -f image2pipe -i - output.mp4

I want to get basically the same result, but sending data directly from puppeteer to an ffmpeg process. Here's my attempt to send some frames over a pipe to ffmpeg from puppeteer, but it doesn't work. The program doesn't even exit, I suspect I'm misusing pipes or something. How can I make it work properly?

const puppeteer = require("puppeteer");
const { spawn } = require("child_process");

async function main() {
    let browser = await puppeteer.launch({});
    let page = await browser.newPage();
    await page.goto("http://google.com");

    let ffmpeg = spawn("ffmpeg", ["-f", "image2pipe", "-i", "-", "output.mp4"], {
        stdio: ["pipe", process.stdout, process.stderr]
    });
    for (let i = 0; i < 10; i++) {
        let screenshot = await page.screenshot();
        ffmpeg.stdin.write(screenshot);
    }
    await browser.close();
}

main();
Mei Zhang
  • 1,434
  • 13
  • 29
  • I'm not sure, but the screenshot is a buffer, so you need to transform it in a stream and then pipe it to the ffmpeg. I would try: `screenshotStream.pipe(ffmpeg.stdin)` and here to create the stream froma buffer https://stackoverflow.com/a/16044400/3309466 – Manuel Spigolon Jan 24 '19 at 09:28
  • @Mei Zhang, did it work for you? Have you succeeded in recording a video out of puppeteer process? – Andrevinsky Oct 10 '19 at 13:03
  • 1
    Any solution for this problem? – Saurabh Feb 17 '20 at 07:20

1 Answers1

2

Hey so I've never something like this but I checked out the puppeteer docs. If you don't specify a path to save to it won't save to file and if you specify base64 it returns the raw data. Maybe you'd pipe that raw data into ffmpeg?

So when you call screenshot it would be something like this

let screenshot = await page.screenshot({
  encoding:'base64'
})
PhantomSpooks
  • 2,877
  • 2
  • 8
  • 13