All posts

Trim video via CLI

ffmpeg: trim by time, crop the frame, and resize.

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

Trim with -ss start and -to end before -i. Crop with -vf crop=width:height:x:y. Resize with -vf scale=width:height. Chain filters with commas.

ffmpeg
ffmpeg -y -ss 5 -to 20 -i input.mp4 -c:v libx264 -preset medium -crf 23 -pix_fmt yuv420p -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 trim-video .
docker run --rm -v "$PWD:/work" -w /work trim-video \
  ffmpeg -y -ss 5 -to 20 -i input.mp4 -c:v libx264 -preset medium -crf 23 -pix_fmt yuv420p -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 -ss 5 -to 20 -i input.mp4 -c:v libx264 -preset medium -crf 23 -pix_fmt yuv420p -c:a aac -b:a 128k output.mp4
Example 2
ffmpeg -y -i input.mp4 -vf crop=640:360:80:40 -c:v libx264 -preset medium -crf 23 -pix_fmt yuv420p -c:a aac -b:a 128k output.mp4
Example 3
ffmpeg -y -ss 10 -to 30 -i input.mp4 -vf scale=1280:720 -c:v libx264 -preset medium -crf 23 -pix_fmt yuv420p -c:a aac -b:a 128k output.mp4