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();
Puppeteer screenshot is one of the tools that Puppeteer offers to take and save screenshots of a page. And in this post, I will explain what Puppeteer screenshot is and what it is composed of.
However, Puppeteer enables you to use Node.js as a client, putting yourself in the user-side viewpoint. Node.js becomes a bot that will open a web browser, go to a new tab, open the website, and do much more. Where Is Puppeteer Screenshot Mostly Used? Have you ever wondered where you could use a simple screenshot function?
Luckily, Puppeteer gives a robust and straightforward foundation to do to it. Puppeteer screenshot enables you to see what clients see and generate the test data. It can help you screenshot the specific area of the website and capture only relevant data. If you are interested in E2E testing, check out Testim’s new feature, Screenplay.
For example, Puppeteer sets the initial page size to 800×600 pixels, but you can change it with page.setViewport. What is Puppeteer? According to Puppeteer’s documentation on GitHub, “Puppeteer is a Node library that provides a high-level API to control Chrome or Chromium over the DevTools Protocol.
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'
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With