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.
A glitch workshop · H.264 video in MP4 files
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
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.
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.
A complete picture, like a photo. Big. Playback can start here, and errors stop here.
Changes compared to the previous frame. Small. Useless on its own: it needs the picture before it.
Changes compared to frames before and after it. Smallest. Our scripts switch these off to keep things simple.
| frame | type | size (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
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:
frame_xray.py --arrows). Each little arrow is one instruction.Raw pictures from a camera or screen. Huge.
Keeps one full picture, then finds the blocks and writes arrows plus corrections.
An envelope (MP4) holding the instructions (H.264) and some tables: timing, keyframe list, playlist.
Starts at a keyframe and follows the instructions. It cannot check whether they make sense.
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:
Delete a keyframe, repeat a P-frame, damage some bytes. The picture goes wrong: melting, smearing, blocks.
Change the timing table, the keyframe list or the playlist. The order and timing go wrong, and players start to disagree.
3
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.
I full picture P instructions. Dashed = deleted. Outlined = repeated. Striped = damaged.
4
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.
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.
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.
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.
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:
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
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:
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.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.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.



| Player | At the jumps | Notes from testing |
|---|---|---|
| ffmpeg 9 | mosh | Follows the edit list. Burst frames: shows them all if you decode to a file. |
| Chrome | mosh | Reads MP4 with ffmpeg's code, so it looks much like ffmpeg. |
| QuickTime engine (AVPlayer) | freeze | Three plays caught 13, 13 and 9 different burst frames out of 60. |
| VLC 3 | clean order | Ignores the edit list entirely. |
| Safari | your turn | Uses Apple's decoder, so probably like QuickTime. Check! |
| Firefox | your turn | Has its own MP4 reader. |
| IINA, phone, chat apps | your 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
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.
Move the glitch-kit folder somewhere easy, like Documents.
Type cd (with a space), drag the folder onto the Terminal window, press Return.
Use footage you shot yourself or are allowed to use. Short clips (5–15 s) work best.
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.codeframe_xray.pyList every frame's type and size; --arrows draws the motion vectors.codedatamosh.pyGlue clips together and delete every keyframe but the first.codebloom.pyCopy one P-frame many times so the movement keeps going.codebyte_rot.pyChange random bytes inside the frames.codeunstable.pyBuild the player-dependent, timing-dependent file.codeWords
moov, stts, stss and elst.00 00 01. One frame is usually one NAL unit.