Software Engineer's Blog

Batch Trim MP3 Files on Ubuntu using FFmpeg

Batch Trim MP3 Files on Ubuntu using FFmpeg

Trimming dozens of MP3 files manually is tedious. FFmpeg allows you to crop hundreds of files in seconds without re-encoding, preserving original quality.

1. The Core Command

Before batching, here is the logic for a single file. We use -ss to set the start time and -c copy to trim without quality loss.

# Trim the first 4 seconds
$ ffmpeg -ss 00:00:04 -i input.mp3 -c copy output.mp3

Two things make this fast and lossless. -c copy streams the audio through without re-encoding, so there’s no quality loss and it finishes almost instantly. And putting -ss before -i uses input seeking — ffmpeg jumps straight to the timestamp instead of decoding up to it — which is much faster on long files. The trade-off is precision: with -c copy, the cut starts at the nearest seek point at or before your timestamp, so the exact offset is file-dependent rather than sample-exact. That’s fine for trimming songs; if you need an exact cut, drop -c copy and re-encode (-c:a libmp3lame), then check the result.

2. The Batch Script (batch_trim.sh)

Use this script to process all .mp3 files in the current directory automatically. It creates a trimmed folder to keep your output organized.

Create a file named batch_trim.sh and paste the following:

#!/bin/bash

# Create output directory
mkdir -p trimmed

for f in *.mp3; do
    # Skip if no MP3 files exist
    [ -f "$f" ] || continue 
    
    # Trim start (4s) and save to 'trimmed' folder
    ffmpeg -i "$f" -ss 00:00:04 -c copy "trimmed/${f%.*}_cut.mp3"
done

echo "Batch processing complete."

3. How to Run

Open your terminal in the target directory and run:

$ chmod +x batch_trim.sh
$ ./batch_trim.sh

Why this method?

  • Fast: Processes files instantly.
  • Lossless: -c copy ensures no audio degradation.
  • Clean: Keeps original files untouched and separates output.

Trimming the end, or a range

The script lops off a fixed first 4 seconds. To cut to a specific length or range instead, add -t (duration) or -to (end timestamp):

# Keep only 00:04 to 01:30
$ ffmpeg -ss 00:00:04 -to 00:01:30 -i input.mp3 -c copy output.mp3

Swap that into the loop and you can trim a whole folder to a consistent range. Confirm the new lengths with ffprobe:

$ ffprobe -v error -show_entries format=duration -of csv=p=0 trimmed/song_cut.mp3

Trimming and converting are different jobs. If you actually want to turn other formats into MP3, see converting WAV or MP4 to MP3, or batch-converting a folder of videos.