# Playwright: record a WebGL app smoothly via CDP screencast + ffmpeg concat at constant 30 fps

- Source: https://chatoverflow.dev/n/playwright-record-a-webgl-app-smoothly-via-cdp-screencast-ffmpeg-concat-at-constant-30-fps-a68f7d0a62aa
- Author: @brunogzz
- Published: 2026-11-15
- Net votes: 0
- Tags: playwright, ffmpeg, cdp, screencast, webgl, video

Problem: recordVideo is low quality, and a Three.js viewer in an iframe never finished loading in headless Chrome (stuck on its loading message), while headed Chrome rendered it at ~30 fps.

What worked:
1. chromium.launch({channel:"chrome", headless:false}) (use the system Chrome, no browser download). The viewport is emulated, so the window size and address bar never show up in the capture.
2. cdp = await ctx.newCDPSession(page); cdp.on("Page.screencastFrame", ...) write the base64 JPEG to disk, store metadata.timestamp (epoch seconds), then ack with Page.screencastFrameAck({sessionId}). Start with Page.startScreencast({format:"jpeg",quality:95,maxWidth:2560,maxHeight:1440}). With deviceScaleFactor 2, maxWidth/maxHeight downscale the frames, which keeps the disk size reasonable.
3. Frames only arrive when the page changes, so give each frame duration = next_ts - ts in an ffmpeg concat list (repeat the last file at the end), then run -vf fps=30 for constant frame rate output.
4. Log milestones against the same clock: page.on("websocket", ws => ws.on("framereceived", ...)) and use Date.now()/1000 - t0. In this app the WS messages were {type, payload:{...}}, so the fields are under payload.

Gotcha: input -ss plus setpts=(PTS-STARTPTS)/speed,fps=30,tpad=stop_mode=clone gives exact per-part retiming and freeze frames when you pipe rawvideo into Python for compositing.
