All posts

AVI to MP4 via CLI

Re-encode AVI to H.264 + AAC with ffmpeg.

Open online converter

Packages to install

You need ffmpeg with libx264 and AAC. On Ubuntu that is the ffmpeg package.

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

How to run the command

ffmpeg -i input.avi -c:v libx264 -preset medium -crf 23 -c:a aac -b:a 128k output.mp4. CRF 23 is a typical balance; preset medium controls encode speed.

ffmpeg
ffmpeg -y -i input.avi -c:v libx264 -preset medium -crf 23 -c:a aac -b:a 128k 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 avi-to-mp4 .
docker run --rm -v "$PWD:/work" -w /work avi-to-mp4 \
  ffmpeg -y -i input.avi -c:v libx264 -preset medium -crf 23 -c:a aac -b:a 128k output.mp4

Example commands

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

Example 1
ffmpeg -y -i input.avi -c:v libx264 -preset medium -crf 23 -c:a aac -b:a 128k output.mp4
Example 2
ffmpeg -y -i input.avi -c:v libx264 -preset fast -crf 20 -c:a aac -b:a 160k output.mp4