I have written a Python script using Playwright to record a high-quality APNG video of a webpage. The script captures frames at 100 FPS and saves them as an animated PNG. I use it to record bar chart race animations from Flourish Studio, such as this one:
/visualisation/21660262/
Here is my script:
from playwright.sync_api import sync_playwright
import keyboard
import time
import os
import threading
from PIL import Image
import io
# Output folder
output_folder = r"E:\Desktop\New folder (4)\New folder (3)"
os.makedirs(output_folder, exist_ok=True) # Ensure directory exists
capturing = False # Flag to control capturing state
saving = False # Prevent capturing while saving
screenshot_buffer = [] # Store screenshots in memory (RAM)
lock = threading.Lock() # Prevent race conditions
# Function to listen for keyboard inputs
def listen_keys():
""" Listens for keyboard input to start/stop capturing or exit script. """
global capturing, saving
while True:
if keyboard.is_pressed("F3"): # Start capturing
if not capturing and not saving:
print("