A glitch workshop · H.264 video in MP4 files

How video breaks, and how to break it on purpose

Datamoshing, the melting, smearing look in music videos and video art, is not a filter. It happens when you damage the instructions inside a video file. This page explains what those instructions are, lets you play with a toy version, and gives you Python scripts that break real videos.

1

A video is a recipe, not a flipbook

You might think a video file stores pictures one after another, like a flipbook. It doesn't, because that would be far too big. One HD picture (1280 × 720 pixels) takes about 1.4 MB. At 25 pictures per second, one minute of video would be over 2 GB.

82.9 MB2.4 s of HD video stored as plain pixels
0.43 MBthe same 2.4 s as an H.264 MP4
193×smaller, and it looks almost the same

The trick: most of a video barely changes from one moment to the next. So the encoder (the program that makes the file) stores one full picture, then mostly instructions for how to change it: "this patch moves two pixels left, that patch gets a bit darker". The decoder (the player) starts from the picture and follows the instructions, frame after frame.

I I-frame / keyframe

A complete picture, like a photo. Big. Playback can start here, and errors stop here.

P P-frame

Changes compared to the previous frame. Small. Useless on its own: it needs the picture before it.

B B-frame

Changes compared to frames before and after it. Smallest. Our scripts switch these off to keep things simple.

Size of each frame in a real H.264 file

90 KB
60 KB
30 KB
Show as a table
frametypesize (KB)

The first 50 frames of the sliding fractal used throughout this page, encoded by ffmpeg with a keyframe every second. Frame 26 is the second keyframe. Most frames are a tiny fraction of the full picture.

2

Encoding and decoding

The encoder chops each picture into small squares called blocks (16 × 16 pixels). For every block in a new frame, it searches the previous frame for the best-matching patch and writes down two things:

A spinning test card with its motion vectors drawn on top (made with frame_xray.py --arrows). Each little arrow is one instruction.
Pixels

Raw pictures from a camera or screen. Huge.

Encoder

Keeps one full picture, then finds the blocks and writes arrows plus corrections.

.mp4 file

An envelope (MP4) holding the instructions (H.264) and some tables: timing, keyframe list, playlist.

Decoder

Starts at a keyframe and follows the instructions. It cannot check whether they make sense.

Screen

Shows each finished picture at the time the file asks for.

This is the whole secret of glitch art: the decoder trusts the file blindly. Feed it instructions meant for a different picture and it follows them anyway. You can break things in two places:

The instructions (H.264)

Delete a keyframe, repeat a P-frame, damage some bytes. The picture goes wrong: melting, smearing, blocks.

The envelope (MP4)

Change the timing table, the keyframe list or the playlist. The order and timing go wrong, and players start to disagree.

3

Try it: a toy decoder

This is a tiny video codec running in your browser. Clip A is a still picture of a sunset. Clip B is a spinning wheel. They are encoded exactly as described above: one I-frame each, then P-frames made of arrows and corrections. Press play to watch them normally, then switch on a glitch and play again.

frame 1

I full picture P instructions. Dashed = deleted. Outlined = repeated. Striped = damaged.

4

Three ways to break a real video

The same three tricks, done to real H.264 by the scripts in the kit. The scripts re-encode your clip first, with settings that make it easy to break (no B-frames, few keyframes, one reference frame), then cut the stream into its packets and edit them with plain Python.

Datamosh datamosh.py

Two clips glued together, and every keyframe except the first thrown away. The spinning card's arrows drag the fractal around until the corrections slowly paint the card back in.

python3 datamosh.py clip1.mp4 clip2.mp4 --bake

Bloom bloom.py

One P-frame copied 25 times, at 1 s and at 2.6 s. Whatever was moving keeps moving the same way, dragging colours like wet paint.

python3 bloom.py clip.mp4 --at 1 2.6 --repeat 25 --bake

Byte rot byte_rot.py

A few random bytes changed per frame. H.264 packs data so tightly that one wrong byte scrambles the rest of the frame. A keyframe every 2 s repairs it.

python3 byte_rot.py clip.mp4 --amount 0.6 --heal 2 --bake

Strict players and forgiving players

A broken file has no single correct look. Each decoder has its own way of coping with nonsense, called error concealment. When I tested the raw outputs on a Mac:

  • VLC and ffmpeg (and players built on ffmpeg, like IINA) are forgiving: they show the smears and keep going.
  • QuickTime (Apple's decoder) is strict. It played the datamosh, but refused the byte-rot file completely: zero frames.

That is why every script has --bake. It lets ffmpeg decode the broken file, then records what it showed into a new, healthy video. The baked file looks the same everywhere, including Instagram or YouTube. (Those sites re-encode everything you upload, so an unbaked glitch would get either rejected or "fixed" by their decoder.)

5

The unstable file

unstable.py makes a video that is a different film depending on which player opens it, and slightly different every time you press play. It stacks four tricks, two in the instructions and two in the envelope:

  1. One keyframe, ever. After the first picture, the encoder never sends a full picture again. Instead a column of fresh pixels slowly sweeps across the screen every 3 s ("intra refresh"), so a damaged picture heals like a wiper on a windscreen.
  2. Every frame claims to be a keyframe. MP4 files keep a list of safe starting points (the stss table). The script deletes it, and by the rules no list means every frame is safe. That's a lie, so scrubbing drops the player in the middle of the instructions.
  3. A playlist that jumps. MP4 can carry an edit list (elst): "play 0–1.6 s, then jump to 9.2 s, then back to the start…". Most jumps land on a P-frame, so the decoder gets arrows meant for a completely different picture.
  4. A burst of 2-millisecond frames. For one moment, 60 frames of the spinning card are squeezed into 0.12 s. Your screen refreshes only every 16 ms, so you see about one frame in eight. Which ones depends on tiny timing differences, so it changes every play.

The clock in the top-right corner shows the moment of the original video you are looking at (seconds and hundredths, so 492 means 4.92 s). Watch it jump.

This is your browser's version. Play it, scrub it, play it again. Then download it and open it in QuickTime and VLC side by side.

Things to try

  • Play it twice in QuickTime and read the clock during the spinning flash at about 7.6 s. You'll catch different numbers each time.
  • Drag the playhead to the middle in VLC, then in QuickTime. One shows a grey or smeared picture that heals; the other may freeze.
  • Open it in Firefox and Safari. I haven't tested those: fill in the table below.
  • Send it to yourself on a messaging app. Did the app "fix" it?

The same file in three players, sampled every 0.8 s

ffmpeg · ChromeFollow the playlist and keep decoding: mosh at every jump, clean again at each return to the keyframe.
Clean fractal, then pink and magenta smears after the first jump, then clean fractal again, then green spiral smears.
QuickTime engineFollows the playlist but restarts its decoder at each jump and refuses to start on a P-frame: the picture freezes, then a later jump moshes differently.
Clean fractal, the same frame repeated three times while frozen, clean fractal, then a heavily moshed coloured test card.
VLCIgnores the playlist and plays the clean original order: fractal, spinning card, spiral.
Clean sliding fractal, then the clean spinning test card, then a clean green and purple spiral.
PlayerAt the jumpsNotes from testing
ffmpeg 9moshFollows the edit list. Burst frames: shows them all if you decode to a file.
ChromemoshReads MP4 with ffmpeg's code, so it looks much like ffmpeg.
QuickTime engine (AVPlayer)freezeThree plays caught 13, 13 and 9 different burst frames out of 60.
VLC 3clean orderIgnores the edit list entirely.
Safariyour turnUses Apple's decoder, so probably like QuickTime. Check!
Firefoxyour turnHas its own MP4 reader.
IINA, phone, chat appsyour turn

Tested on macOS with the default test pattern, 30 November 2026. "QuickTime engine" means Apple's AVPlayer driven by a script, the same playback engine QuickTime Player uses.

6

The scripts

Six Python files, no extra libraries. They only need ffmpeg, and yt-dlp for downloading. Every file starts with an explanation of its trick, and works on its own.

Each script's code is below; use Copy to take one.
  1. Unzip the kit

    Move the glitch-kit folder somewhere easy, like Documents.

  2. Open Terminal and go to the folder

    Type cd (with a space), drag the folder onto the Terminal window, press Return.

  3. Get a clip
    python3 get_clip.py "https://www.youtube.com/watch?v=…" --start 0:30 --length 8 -o clip1.mp4 python3 get_clip.py my_video.mov --length 6 -o clip2.mp4

    Use footage you shot yourself or are allowed to use. Short clips (5–15 s) work best.

  4. Break it
    python3 datamosh.py clip1.mp4 clip2.mp4 --bake

    Add --help to any script to see its options. Open the raw result in VLC, and share the _baked one.

get_clip.pyDownload a piece of a web video (yt-dlp) or cut a file on your computer.code
frame_xray.pyList every frame's type and size; --arrows draws the motion vectors.code
datamosh.pyGlue clips together and delete every keyframe but the first.code
bloom.pyCopy one P-frame many times so the movement keeps going.code
byte_rot.pyChange random bytes inside the frames.code
unstable.pyBuild the player-dependent, timing-dependent file.code

Words

Words you'll meet

codec
A method for squeezing video (encoder + decoder). H.264 is the most common one.
H.264 / AVC
The language the instructions are written in. It lives inside the MP4.
MP4 / container
The envelope. Holds the video and audio streams plus tables of timing and keyframes. Made of nested "boxes" with four-letter names like moov, stts, stss and elst.
keyframe / I-frame
A complete picture. Playback can start here.
P-frame
Changes compared to the previous frame.
B-frame
Changes compared to a frame before and a frame after.
GOP
Group of pictures: one keyframe plus all the frames that depend on it.
block
A small square of the picture (16 × 16 pixels) that gets its own instructions. Officially a macroblock.
motion vector
An arrow saying "copy this block from over there in the previous picture".
residual
The colour correction added after copying.
NAL unit
One packet in an H.264 stream. Each starts with the bytes 00 00 01. One frame is usually one NAL unit.
error concealment
A decoder's way of guessing what a broken part should look like. Every player guesses differently.
edit list
A playlist inside the MP4 that says which parts of the video to play, in which order.
baking
Recording what a decoder shows into a new, healthy file, so the glitch looks the same everywhere.