All posts

Compress video via CLI

Re-encode to H.264 or VP9 with ffmpeg using a higher CRF.

Open online converter

Packages to install

Only ffmpeg is required. Use libx264 for MP4 and libvpx-vp9 for WebM.

ffmpeg
apt install
sudo apt-get update
sudo apt-get install -y ffmpeg

How to run the command

ffmpeg -i input.mp4 -c:v libx264 -preset veryfast -crf 28 -pix_fmt yuv420p -c:a aac -b:a 96k -movflags +faststart output.mp4. CRF 23 is gentler; 32 shrinks more. For WebM: -c:v libvpx-vp9 -crf 36 -b:v 0 -c:a libopus.

ffmpeg
ffmpeg -y -i input.mp4 -c:v libx264 -preset veryfast -crf 28 -pix_fmt yuv420p -c:a aac -b:a 96k -movflags +faststart output.mp4

Dockerfile example

Build a minimal Ubuntu image with the required packages and run the conversion inside the container.

Dockerfile
FROM ubuntu:24.04

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        ffmpeg && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /work
docker build && docker run
docker build -t compress-video .
docker run --rm -v "$PWD:/work" -w /work compress-video \
  ffmpeg -y -i input.mp4 -c:v libx264 -preset veryfast -crf 28 -pix_fmt yuv420p -c:a aac -b:a 96k -movflags +faststart output.mp4

Example commands

Run these locally after installing the packages, or inside the container from the Dockerfile.

Example 1
ffmpeg -y -i input.mp4 -c:v libx264 -preset veryfast -crf 28 -pix_fmt yuv420p -c:a aac -b:a 96k -movflags +faststart output.mp4
Example 2
ffmpeg -y -i input.webm -c:v libvpx-vp9 -crf 36 -b:v 0 -deadline good -cpu-used 4 -c:a libopus -b:a 64k output.webm