Skip to content
All articles

Game Development

Writing a video player inside Stardew Valley: SMAPI, FFmpeg and sync

When I wanted to watch something with friends in other cities while we were on the same farm, we had to leave the game. So I brought the video into the game. Here's how a 15,000-line C# mod works.

Fahrettin Baştürk5 min read

I play Stardew Valley with people close to me who live in other cities. One evening, sitting on the same farm, we wanted to watch something together, and all we could do was start a screen share or switch to a separate browser tab. The warmth of the game disappeared the moment we did. Instead of sitting in the same living room, it felt like everyone had gone back to their own room.

What I wanted was simple: the video should play inside the game's own world, on a TV or a cinema screen, and everyone should see the same scene at the same time. A simple wish, but underneath it was a small media player, a sync protocol and a negotiation with a game engine's draw loop. The result is a C# mod of about 15,500 lines: BigScreenTogether.

Getting inside the game

Stardew Valley mods load through SMAPI. SMAPI lets you subscribe to game events; to change the behavior of the game's own methods, you use Harmony, which lets you inject your code before or after a method at runtime. To draw the screen, I patched in after Furniture.draw and GameLocation.draw.

The first decision I made was an abstraction: the "screen". The 3×3 Big Screen furniture, the outdoor cinema and the indoor theater all implement the same interface. The player doesn't know which screen it draws on; the screen doesn't know what's playing on it. Adding a new kind of screen is possible without touching the player.

Turning video into frames

I didn't want to embed a browser in the game. It's heavy, and it doesn't get along well with the game's draw loop. Instead I left the work to two well-known tools: yt-dlp resolves the stream URL, and ffmpeg writes low-resolution raw frames to its standard output.

// ffmpeg writes raw RGBA frames to stdout; a separate thread reads them.
var args = $"-i \"{streamUrl}\" -vf scale={w}:{h} -f rawvideo -pix_fmt rgba -";
using var ffmpeg = Process.Start(new ProcessStartInfo("ffmpeg", args) {
    RedirectStandardOutput = true, UseShellExecute = false, CreateNoWindow = true
});

The most important rule: the game thread must never wait. Frames are read and decoded on a separate thread; on each draw, the game loop only copies the latest ready frame into a Texture2D. If the network slows down for a moment, the game doesn't stutter; at worst the picture is one frame behind. Part of the compressed stream is also prefetched into memory, so small network fluctuations never reach the screen.

Why audio is the master clock

Anyone who writes a video player eventually faces the question: when picture and sound drift apart, which one do you trust? The human ear notices a hiccup in audio much more easily than a skipped frame. So audio is the master clock. Frames are dropped or held back to match the audio; the audio never speeds up to chase the picture.

Multiplayer sync: a single authority

In multiplayer, each computer plays its own copy of the video, so sync means keeping everyone at the same second. The simplest solution was a single authority: the host. Other players' "play", "pause" or "seek" commands go to the host as requests, and the host broadcasts the state to everyone.

  • Drift beyond one second is corrected; smaller differences are ignored, because constant correction is more distracting than a tiny offset.
  • Players who join late receive the room's current state and start at the right second.
  • A media item's identity is a canonical URL everywhere. Playback, sync, quality changes and recovery all work only with that URL, so the same video under two different addresses is never treated as two different things.

Growing beyond the game

Over time the project outgrew the game. Someone without Stardew should be able to watch too. For that I wrote a Rust server, a Tauri desktop app and an Android TV app. For global sharing, the mod starts the bundled Rust server as a hidden helper process and connects to a Steam lobby room through a WebSocket bridge. I wrote about that side in a separate post.

Small but important details

  • yt-dlp and ffmpeg are downloaded and updated from their official sources with SHA-256 verification. I think any tool that downloads something to a user's machine should do this.
  • WebVTT and SRT subtitles are parsed, and lines are wrapped to fit the screen.
  • The release script builds the distribution package and verifies its contents; it stops if the DLL, manifest or tools are missing.
  • Up to version 1.22, every release was tested in-game with two separate computers. Sync bugs don't show up on one machine.

What I loved most about this project: technically I was writing a media player, a sync protocol and a game engine patch, but what I was really doing was seating faraway people in the same living room. Knowing why you build something shapes how you build it.

Fahrettin Baştürk

Software Engineer & Consultant

Share
  • C#
  • Game Modding
  • FFmpeg
  • Multiplayer

Working on a similar system or problem?Tell me about your project