Reputation: 1714
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();
Upvotes: 8
Views: 1738
Reputation: 3540
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'
})
Upvotes: 2