4 min read Md Afridi Sk

What FFmpeg Is Actually Doing

The stages inside FFmpeg, why -c copy beats transcoding, and why seeking quietly breaks a synced watch party. Notes from bundling it in Colloquium.

FFmpeg Video Systems

Most people meet FFmpeg as a command they copied off Stack Overflow. It works, you move on, and your mental model stays at “magic box that converts video.”

I had to open the box. Colloquium bundles FFmpeg so a watch party can play whatever file someone drags in, inside a plain HTML <video> element. When three people need to see the same frame at the same moment, “magic” stops being good enough.

The pipeline

flowchart LR
    A[Input file] --> B[Demuxer
libavformat] B -->|packets| C[Decoder
libavcodec] C -->|frames| D[Filters
libavfilter] D -->|frames| E[Encoder
libavcodec] E -->|packets| F[Muxer
libavformat] F --> G[Output file]

A .mkv or .mp4 file is a container. Inside are separate streams for video, audio and subtitles, each compressed by its own codec (H.264, AAC and so on).

The demuxer opens the container and hands out packets, which are small compressed chunks. The decoder turns packets into frames, meaning actual pixels. Filters change frames. The encoder compresses frames back into packets, and the muxer writes them into a new container.

With that picture in your head, the most useful flag in FFmpeg makes sense:

ffmpeg -i input.mkv -c:v copy -c:a copy output.mp4
ffmpeg -i input.mkv -c:v copy -c:a copy output.mp4

-c copy skips decoding and encoding, the two slow steps in the middle. The compressed bits go straight from the old container into the new one, so it runs at disk speed instead of CPU speed. That’s a remux: same video, different container.

Most files don’t need transcoding

When a browser refuses a file, it’s usually the container it doesn’t like. An MKV holding H.264 has the same video as the MP4 you want, so repacking is enough.

You decide per stream. Colloquium asks ffprobe what’s inside, then asks the browser what it can play:

return {
  video: video ? (caps.mp4Video(videoCodec) ? "copy" : "encode") : "copy",
  audio: audio ? (caps.mp4Audio(audioCodec) ? "copy" : "encode") : "copy",
  container: "hls",
};
return {
  video: video ? (caps.mp4Video(videoCodec) ? "copy" : "encode") : "copy",
  audio: audio ? (caps.mp4Audio(audioCodec) ? "copy" : "encode") : "copy",
  container: "hls",
};

Often one stream needs work and the other doesn’t. On a Mac, HEVC video with a DTS soundtrack keeps the video as is and re-encodes only the audio, because Safari’s engine plays HEVC natively. The same file on Windows without the HEVC extension has to re-encode both. I ask the browser instead of guessing from the operating system, otherwise you end up transcoding on every Mac for no reason.

Seeking is the hard part

Video isn’t stored as a list of full pictures. It comes in groups that start with one complete frame, the keyframe, followed by frames that only record what changed. So you can’t start at any second you like.

Ask for 7 seconds in while copying:

ffmpeg -ss 7 -copyts -i input.mkv -c:v copy ...
ffmpeg -ss 7 -copyts -i input.mkv -c:v copy ...

and FFmpeg quietly starts at the keyframe before it. With 10-second groups you get 0 instead of 7. A single viewer never notices. With three viewers, each lands somewhere different and the film drifts out of sync.

You also can’t compute where you landed. I tried. Re-encoded audio doesn’t start on the video keyframe, so the durations never add up. What works is asking FFmpeg after the fact:

ffprobe -show_entries format=start_time -of default=nw=1:nk=1 index.m3u8
ffprobe -show_entries format=start_time -of default=nw=1:nk=1 index.m3u8

This depends on -copyts in the earlier command, which keeps the file’s original timestamps instead of resetting them to zero. Send that number to everyone and each player offsets from it.

If you’re re-encoding anyway, you can choose where keyframes go. -force_key_frames expr:gte(t,n_forced*4) puts one every 4 seconds, so every segment starts cleanly.

Output settings

-f hls -hls_time 4 -hls_playlist_type event \
-hls_flags independent_segments+temp_file \
-hls_segment_type fmp4 -hls_fmp4_init_filename init.mp4
-f hls -hls_time 4 -hls_playlist_type event \
-hls_flags independent_segments+temp_file \
-hls_segment_type fmp4 -hls_fmp4_init_filename init.mp4

temp_file writes each segment under a temporary name and renames it when it’s done. Renames are atomic, so the server reading that folder never hands out a half-written segment. It’s a small flag, and the whole setup breaks without it.

The one that caught me out: FFmpeg isn’t a player, so it reads input as fast as it can. On a 1.6 GB file I got 19.5 minutes of video in the first 30 seconds, roughly 91x realtime, at 130 Mbit/s. That bandwidth came out of the video call the film was playing into, and playback stuttered. -readrate 1.5 caps reading at 1.5x realtime, and an initial burst keeps startup quick.

Building the binary

./configure decides what your FFmpeg can do at all. The flag to know is --disable-autodetect. Without it, configure links against whatever happens to be on the build machine, Homebrew’s x264 for example, and you ship a GPL binary without meaning to.

Most codecs already live inside libavcodec. I enabled the operating system’s own video frameworks and nothing else, and each binary came out at 13 MB. The popular prebuilt ones are 113 MB, mostly codecs I’d never call.

When it breaks

Knowing the stages tells you where to look. A file that won’t open is a demuxer problem. A green or stuttering picture points at the decoder. Audio drifting after a seek is timestamps. If a player rejects a file that looks fine, check the muxer or the codec settings.

I run ffprobe before anything else.

Comments