All posts

Trim audio via CLI

ffmpeg: cut a section of MP3, WAV, or AAC by start and end time.

Open online converter

Packages to install

Only ffmpeg is required. Re-encode so the cut lands on the exact timestamps.

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

How to run the command

Put -ss start and -to end before -i, then -vn and a codec: libmp3lame for MP3, pcm_s16le for WAV, aac for AAC.

ffmpeg
ffmpeg -y -ss 5 -to 20 -i input.mp3 -vn -c:a libmp3lame -b:a 192k output.mp3

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-audio .
docker run --rm -v "$PWD:/work" -w /work trim-audio \
  ffmpeg -y -ss 5 -to 20 -i input.mp3 -vn -c:a libmp3lame -b:a 192k output.mp3

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.mp3 -vn -c:a libmp3lame -b:a 192k output.mp3
Example 2
ffmpeg -y -ss 10 -to 30 -i input.wav -vn -c:a pcm_s16le output.wav
Example 3
ffmpeg -y -ss 3 -to 15 -i input.aac -vn -c:a aac -b:a 192k output.aac