Trimming video and making GIFs in the browser
What ffmpeg.wasm downloads and why, why stream-copy cuts land on keyframes, the arithmetic behind GIF file sizes, and when a short MP4 is the better choice.
Published
Cutting a clip, turning it into a GIF or pulling the audio out of a video used to mean installing a desktop program. It can now be done in a browser tab, within limits worth knowing before you drop a two-gigabyte file onto a web page. This guide shows the exact commands the tools on this site run, and works through why GIFs come out so much larger than expected.
What ffmpeg.wasm is, and why it downloads first
FFmpeg is an open-source program that reads, converts and writes almost every audio and video format in common use. ffmpeg.wasm is FFmpeg compiled to WebAssembly, a compact binary format that browsers can run at close to native speed inside a page. The page writes your file into a virtual file system that exists only in the tab’s memory, runs an FFmpeg command against it, and reads the result back out for download.
The catch is that the engine is large. The video trimmer, GIF maker and audio converter load version 0.12.6 of the FFmpeg core from the public unpkg CDN, roughly 30 MB, when you start your first operation rather than when the page opens. Your media is not part of that request, but the CDN does see your IP address and the files you fetched. The broader version of that trade-off is covered in what “processed in your browser” really means.
Each tool page holds its own copy of the engine. Running a second operation on the same page reuses it; opening a different tool loads it again, although your browser may serve it from its cache.
Why it is slower than a desktop app, and where it runs out
The build these tools use is the single-threaded FFmpeg core, so encoding runs on one processor core no matter how many your machine has. Desktop FFmpeg can spread work across all of them and can use hardware video encoders; the browser build can do neither. Anything that re-encodes, including every GIF, is noticeably slower here.
Memory is the harder limit. The source file, the output file and FFmpeg’s working buffers all live inside the tab at once, and 32-bit WebAssembly cannot address more than 4 GB in total. The practical ceiling is lower and varies by browser and device. If a large file fails, desktop FFmpeg is the right tool for it.
Trimming: stream copy versus re-encoding
A video file is a container (MP4, WebM, MOV) holding compressed streams. There are two ways to cut one.
- Re-encoding decodes every frame and compresses the result again. The cut can land on any frame, but it is slow, and the second round of lossy compression costs some quality.
- Stream copy moves the existing compressed data into a new container untouched. It is fast and the quality is identical to the source, but the cut is restricted by how the video was compressed.
The restriction comes from keyframes. Video codecs store an occasional complete picture, a keyframe, and encode the frames after it as changes relative to earlier frames. A frame in the middle of that run cannot be shown without the keyframe it depends on, so a stream copy cannot start cleanly there. Keyframes are commonly a second or more apart, and in screen recordings with little movement they can be further apart still.
The video trimmer uses stream copy. With a start time of five seconds and a duration of ten, it runs the equivalent of:
ffmpeg -ss 00:00:05 -i input.mp4 -t 10 -c copy output.mp4Because -ss comes before the input, FFmpeg seeks within the file rather than reading it from the beginning. With stream copy it can only jump to a keyframe, so the clip starts at the keyframe at or before the time you asked for, and may include a little footage from before your start point. How much depends on how far apart the source’s keyframes are. A cut on an exact frame requires re-encoding, which this tool does not do; if the extra lead-in matters, trim again on a desktop editor.
The output is always an MP4 file. Copying streams into MP4 only works if MP4 can hold them, so a WebM file, such as one from the screen recorder, may fail to trim if its codecs are not accepted in MP4. The trimmer then shows an error instead of producing a file. The screen recorder itself does not use FFmpeg; it uses the browser’s own recording API, and the browser chooses the codec, resolution and bitrate.
Why GIFs are so large
GIF is a format from 1987, and it has three properties that work against video footage.
- 256 colours at most per frame. Each pixel is a single byte pointing into a palette. Smooth gradients and skin tones turn into bands or visible dither patterns.
- Simple, lossless compression. Each frame is compressed with LZW, which finds repeated runs of pixels. It works well on flat areas of colour and poorly on camera noise.
- No real motion compression. A GIF encoder can store only the rectangle that changed since the previous frame, but there is nothing like the motion prediction a video codec uses to describe a moving object cheaply. A pan or a fade changes nearly every pixel, so nearly every pixel is stored again.
The GIF maker runs a two-pass palette filter, which builds one 256-colour palette from the whole clip and then maps every frame onto it:
ffmpeg -i input.mp4 \
-vf "fps=10,scale=320:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" \
output.gifThe fps and scale values are the page’s two settings (1–30 and 100–800), shown at defaults of 10 frames per second and 320 pixels wide. Height follows the source’s aspect ratio, and the Lanczos filter keeps the downscaled frames sharp. The whole input is converted; there is no start or end setting, so trim first.
A rough size calculation
Because each GIF pixel is one byte before compression, the raw size of a clip is simply width × height × frame rate × seconds. The table below does that arithmetic for a five-second 16:9 clip. These are not measurements: LZW will shrink the real file, by a lot for a flat screen recording and by little for noisy camera footage. The point is the ratios between rows. One MB here means one million bytes.
| Width × height | Frame rate | Bytes per second (raw) | Five seconds (raw) |
|---|---|---|---|
| 800 × 450 | 15 | 360,000 × 15 = 5,400,000 | 27 MB |
| 480 × 270 | 15 | 129,600 × 15 = 1,944,000 | 9.72 MB |
| 320 × 180 | 15 | 57,600 × 15 = 864,000 | 4.32 MB |
| 320 × 180 (tool default) | 10 | 57,600 × 10 = 576,000 | 2.88 MB |
| 240 × 135 | 8 | 32,400 × 8 = 259,200 | 1.30 MB |
Halving the width quarters the pixel count, because the height halves with it. Frame rate and duration scale the size in a straight line. For comparison, a video file’s size is its bitrate multiplied by its duration: an MP4 encoded at 1 megabit per second for five seconds is 5 megabits, or 625,000 bytes, and at that bitrate it can carry a far larger picture in full colour.
Settings that keep a GIF small
- Trim to the moment that matters, with the video trimmer first.
- Reduce the width. 320 to 480 pixels is enough for most chat messages and issue trackers.
- Lower the frame rate. Interface demonstrations usually look fine at 8 to 10 frames per second; fast motion needs more.
When a GIF is the wrong choice
If the destination plays video, a short MP4 or WebM is almost always the better file: full colour, sound if you want it, and a fraction of the size. A GIF still earns its place where video is unavailable or unwelcome, such as many email clients and older forums. If the clip is long, needs audio or contains photographic footage with gradients, do not make it a GIF.
Converting audio without losing more than you need to
The audio converter offers MP3, WAV and AAC, and accepts video files as well as audio. It runs the plainest possible command, naming only the input and output, so FFmpeg picks the encoder from the output extension and uses that encoder’s default settings:
ffmpeg -i input.m4a output.mp3MP3 and AAC are lossy: they discard detail that is hard to hear in order to save space. Converting one lossy format to another decodes the audio and discards detail again, so each conversion is a generation of loss that cannot be undone. Always convert from the most original file you have.
Bitrate is what sets lossy file size. At 128 kilobits per second, three minutes is 128,000 ÷ 8 × 180 = 2,880,000 bytes, about 2.9 MB. WAV is uncompressed: CD-quality stereo is 44,100 samples × 2 bytes × 2 channels = 176,400 bytes per second, so the same three minutes is about 31.8 MB. Turning an MP3 into WAV produces that larger file with no improvement, because the detail the MP3 discarded is gone. WAV is useful when an editor requires it, not as a way to restore quality.