DEV Community

Milaan Games
Milaan Games

Posted on Originally published at milaangames.hashnode.dev

Frame-perfect gameplay videos from Godot 4 without a screen recorder

We make small mobile puzzle games in Godot, and we need a steady supply of vertical gameplay clips for YouTube Shorts and TikTok. Screen recorders gave us dropped frames, desktop notifications in the shot and a different result on every take. So we moved the camera inside the engine. The game renders into an offscreen viewport, and every frame is saved to disk. Then ffmpeg turns the frames into a video.

The idea

  1. Run Godot with a fixed timestep: --fixed-fps 30 --disable-vsync. The game advances exactly 1/30 s per frame no matter how slow the capture is, so animations never stutter.
  2. Start the game from a SceneTree script (--script res://tools/capture.gd) instead of the main scene. The script builds the scene, drives it and quits when done.
  3. Put the real game scene inside a SubViewport at the output size (for example 1080×1920), with render_target_update_mode = SubViewport.UPDATE_ALWAYS.
  4. On each frame, call RenderingServer.force_draw() and save viewport.get_texture().get_image().

Minimal capture script

extends SceneTree

const OUT := "user://capture"
const FRAMES := 300  # 10 s at 30 fps
var viewport: SubViewport

func _initialize() -> void:
    DirAccess.make_dir_recursive_absolute(OUT)
    viewport = SubViewport.new()
    viewport.size = Vector2i(1080, 1920)
    viewport.render_target_update_mode = SubViewport.UPDATE_ALWAYS
    root.add_child(viewport)
    viewport.add_child(load("res://scenes/main.tscn").instantiate())
    record.call_deferred()

func record() -> void:
    for i in FRAMES:
        await process_frame
        RenderingServer.force_draw()
        var img := viewport.get_texture().get_image()
        img.save_jpg("%s/f_%05d.jpg" % [OUT, i], 0.92)
    quit(0)
Enter fullscreen mode Exit fullscreen mode

Run it:

godot --path . --fixed-fps 30 --disable-vsync --audio-driver Dummy --script res://tools/capture.gd
ffmpeg -framerate 30 -i f_%05d.jpg -c:v libx264 -pix_fmt yuv420p -movflags +faststart out.mp4
Enter fullscreen mode Exit fullscreen mode

Driving real input

A recording of an idle menu is boring. Our capture script calls the same functions the drag-and-drop UI uses (begin drag, move, drop) with eased positions, so the clip shows real input, real line clears and the real animations. Every placement and clear is logged to a JSON file with its frame number. A small Python script then adds captions and overlays and lines up sound effects with the logged frames.

Things that bit us

  • Saves. The capture plays the game for real, so it would write to the player's save file. We run it on a throwaway copy of the project with private save paths, and the script checks that real saves are never touched.
  • Splash screens and first-run tutorials. Skip them in code before you start recording. Otherwise the first seconds of every clip show the same intro.
  • Determinism. Seed the piece generator so a "good" run can be re-recorded exactly when you change a caption or the music.
  • Disk. 30 fps at 1080×1920 is a lot of JPGs. Delete the frames after encoding.

Result

Every run produces the same stutter-free footage, so re-editing never needs a new recording. We use the same script for Play Store screenshots in several languages and screen sizes.

The clips end up in our Shorts for Blokito, a block puzzle with 300 Adventure levels. If you want to see the output, our games and free browser games are at https://lamian.dev. Questions about the script are welcome in the comments.

Top comments (2)

Collapse
 
latentlark profile image
LatentLark •

Nice setup. Worth comparing against Godot 4's built-in Movie Maker mode (--write-movie out.avi, or a .png path for a frame sequence), which also locks to a fixed frame rate regardless of real time. It records the main window, though, so your SubViewport approach still wins when the output size differs from the window. One small tweak: saving JPG at 0.92 and then encoding to x264 compresses twice, which can show up as banding on flat UI colors and gradients. PNG frames cost more disk but keep the final encode cleaner.

Collapse
 
christmastown_dev profile image
Christmas Town Dev •

Running the capture on a throwaway copy of the project with private save paths is a smart safeguard against touching real player saves. Does seeding the piece generator also let you re-edit only the captions or music without re-recording the footage? That sounds like a great workflow for a solo dev making lots of Shorts. Thanks for sharing the script.