Skip to content

Video optimization

Videos used as web banners, hero backgrounds, and short video reviews should be compressed before storing on S3.

Installation

Install ffmpeg via Homebrew:

bash
brew install ffmpeg

Usage

The script at scripts/media/compress-videos.sh handles all video compression. It scans a directory for .mp4 files and compresses them with H.264/CRF-based encoding.

Silent videos (banners, backgrounds)

Strips the audio track by default — the single biggest win for reducing file size on decorative videos:

bash
./scripts/media/compress-videos.sh /path/to/videos

Videos with audio (reviews, shorts)

Pass --keep-audio to encode audio as AAC at 96k:

bash
./scripts/media/compress-videos.sh --keep-audio /path/to/videos

Capping resolution

Some source videos are recorded at 1440p or 4K but are only ever displayed at 1080p or smaller on the site. The extra pixels just waste bandwidth. Pass --cap-resolution to scale videos above 1080p down to 1080p while keeping lower resolutions native:

bash
./scripts/media/compress-videos.sh --cap-resolution /path/to/videos

Flags can be combined in any order:

bash
./scripts/media/compress-videos.sh --keep-audio --cap-resolution /path/to/videos

Output files are saved alongside the originals with a -minified.mp4 suffix.

Flags explained

  • -c:v libx264 — Sets the video codec to H.264 (AVC). Universal web standard that plays on everything from 2010-era phones to modern smart TVs.
  • -crf 26 — Constant Rate Factor. Instead of a fixed bitrate, CRF ensures consistent quality throughout the video. 26 is an efficient "web-light" setting — smaller than the default 23, but still looks sharp.
  • -preset slower — Tells the encoder to spend more time analyzing pixels to find better compression. Results in a smaller file size for the same quality level.
  • -pix_fmt yuv420p (format=yuv420p in -vf) — Ensures the color space is compatible with all web browsers. Without this, some high-end camera footage (10-bit color) won't play in Chrome or Safari.
  • scale=-2:'min(1080,ih)' — Caps the height at 1080 pixels. Videos at 1080p or below are untouched; anything above (1440p, 4K) is scaled down. The -2 keeps the width proportional and divisible by 2 (required by H.264). Only applied when --cap-resolution is passed.
  • -an — Short for "Audio None." Removes the audio track completely (default behavior).
  • -c:a aac -b:a 96k — AAC audio at 96 kbps, used when --keep-audio is passed.
  • -movflags +faststart — Moves the file's metadata index to the beginning so the video starts playing instantly while the rest downloads. Essential for the web.