Change Audio Sample Rate with FFmpeg on Linux
This guide provides a quick, practical overview of how to change the audio sample rate of a media file using FFmpeg on a Linux system. You will learn the core command-line syntax for basic conversions, how to modify sample rates while preserving multiple audio tracks, and how to batch-process an entire directory of files.
Understanding the Audio Resampling Command
FFmpeg uses the -ar flag to define the target audio
sample rate (measured in Hz). When you change the sample rate, FFmpeg
automatically handles the resampling process using its internal
resampler.
The standard syntax for a basic conversion is:
ffmpeg -i input.mp3 -ar 44100 output.mp3In this command:
-i input.mp3: Specifies the path to your source file.-ar 44100: Sets the new audio sample rate to 44,100 Hz (CD quality). Other common values include48000(DAT/DVD quality) and96000(High-Resolution audio).output.mp3: The name and format of the newly generated file.
Advanced Resampling Scenarios
Depending on your project requirements, you may need to control the audio codec or handle multiple audio streams within a video container.
Resampling Audio Within a Video File
If you want to change the audio sample rate of a video file (like an MP4 or MKV) without re-encoding the video stream, you must explicitly tell FFmpeg to copy the video data. This saves a massive amount of processing time and prevents quality loss.
ffmpeg -i input.mp4 -c:v copy -ar 48000 output.mp4The -c:v copy flag instructs FFmpeg to copy the video
stream exactly as it is, while the -ar 48000 flag alters
the audio stream to 48 kHz.
Explicitly Defining the Audio Codec
By default, FFmpeg selects a native encoder based on your output file
extension. If you want to force a specific audio codec during the
resampling process, use the -c:a flag:
ffmpeg -i input.wav -c:a libmp3lame -ar 44100 output.mp3Batch Processing Multiple Files in Linux
If you have a large folder of audio files that all need to be converted to the same sample rate, you can leverage the Linux Bash shell to automate the task.
Run the following loop directly in your terminal to process all
.wav files in a directory and save them as 44.1 kHz
.mp3 files:
for file in *.wav; do
ffmpeg -i "$file" -ar 44100 "${file%.wav}_resampled.mp3"
doneThis script loops through every file ending in .wav,
applies the -ar 44100 flag, and appends
_resampled.mp3 to the original filename to avoid
overwriting your source data.