TL;DR
FFmpeg allows fast audio format conversion with a simple command: ffmpeg -i input.wav output.mp3. Key options are -b:a for bitrate, -ar for sample rate, and -ac for channel count. For batch jobs, use shell loops or FFmpeg’s glob patterns. If you’re integrating with AI audio APIs, use Apidog to test request/response flows before production.
Introduction
FFmpeg is a powerful command-line tool for audio conversion, ideal for developers needing to automate or script format changes. Whether you need to convert WAV to MP3 for web, or FLAC to AAC for mobile apps, FFmpeg covers most scenarios without a GUI.
This guide provides actionable steps for common audio conversions, explains key flags, shows batch processing techniques, and outlines troubleshooting strategies.
Installation
macOS:
brew install ffmpeg
Ubuntu/Debian:
sudo apt install ffmpeg
Windows: Download from ffmpeg.org and add the bin folder to your PATH.
Verify installation:
ffmpeg -version
Basic conversion syntax
The simplest conversion command:
ffmpeg -i input_file.ext output_file.ext
FFmpeg infers formats from file extensions. This covers most use cases.
Common audio conversions
WAV to MP3
ffmpeg -i recording.wav -b:a 192k output.mp3
-
-b:a 192ksets bitrate. Common values:-
128k— Web quality -
192k— Good quality (music) -
320k— High quality (larger files)
-
For voice, 128k is enough; for music, use 192k or higher.
FLAC to AAC
ffmpeg -i lossless.flac -c:a aac -b:a 256k output.aac
-
-c:a aacspecifies the AAC codec explicitly.
MP3 to WAV
ffmpeg -i compressed.mp3 output.wav
Converts to uncompressed PCM WAV—expect a larger file.
OGG to MP3
ffmpeg -i podcast.ogg -b:a 192k output.mp3
M4A to MP3
ffmpeg -i track.m4a -b:a 192k output.mp3
Audio quality flags
Bitrate
Set constant bitrate (CBR):
ffmpeg -i input.wav -b:a 192k output.mp3
For variable bitrate (VBR):
ffmpeg -i input.wav -q:a 2 output.mp3
-
-q:aranges 0 (highest quality) to 9 (lowest). Use0–2for music,5–7for speech.
Sample rate
ffmpeg -i input.wav -ar 44100 output.mp3
-
-ar 44100sets sample rate to 44.1 kHz (CD). Alternatives:22050(smaller files),48000(pro audio).
Channels
ffmpeg -i stereo.wav -ac 1 mono.mp3
-
-ac 1converts stereo to mono, useful for speech.
Controlling output quality
For more control with MP3:
ffmpeg -i input.wav -c:a libmp3lame -b:a 192k -ar 44100 -ac 2 output.mp3
For AAC with quality mode:
ffmpeg -i input.wav -c:a aac -q:a 1.5 output.aac
Batch processing
Shell loop (macOS/Linux)
for file in *.wav; do
ffmpeg -i "$file" -b:a 192k "${file%.wav}.mp3"
done
Converts all .wav files in the directory to .mp3. ${file%.wav} strips the extension.
Batch with output directory
mkdir output
for file in *.flac; do
ffmpeg -i "$file" -c:a aac -b:a 256k "output/${file%.flac}.aac"
done
PowerShell (Windows)
Get-ChildItem *.wav | ForEach-Object {
$output = $_.BaseName + ".mp3"
ffmpeg -i $_.FullName -b:a 192k $output
}
Trimming during conversion
Extract a specific segment while converting:
ffmpeg -i input.mp3 -ss 00:01:30 -t 60 -b:a 192k clip.mp3
-
-ss 00:01:30starts at 1:30. -
-t 60extracts 60 seconds.
Or set an end time with -to:
ffmpeg -i input.mp3 -ss 00:01:30 -to 00:02:30 output.mp3
Common errors and fixes
“No such file or directory”
Check the input path. Quote paths with spaces:
ffmpeg -i "my file.wav" output.mp3
“Invalid data found when processing input”
The file may be corrupted or misnamed. Inspect format:
ffmpeg -i filename
Codec errors (“Conversion failed!”)
Specify encoder:
- For MP3:
-c:a libmp3lame - For AAC:
-c:a aac
Output file is larger than expected
Converting compressed (MP3) to uncompressed (WAV/FLAC) increases file size.
Integrating with AI audio APIs
Audio conversion is often required before sending to AI APIs (transcription, sentiment, speaker ID).
Typical pipeline:
- Receive audio (WAV, MP3, M4A, etc.)
- Convert to required format (often WAV 16kHz mono)
- POST to API
Convert for speech APIs:
ffmpeg -i input.m4a -ar 16000 -ac 1 -c:a pcm_s16le processed.wav
-
-ar 16000: 16 kHz sample rate -
-ac 1: mono -
-c:a pcm_s16le: 16-bit little-endian PCM
Test your AI API with Apidog:
Once you have the processed file, use Apidog to validate your API integration.
POST https://api.speech-service.example.com/v1/transcribe
Authorization: Bearer {{API_KEY}}
Content-Type: multipart/form-data
[audio_file: processed.wav]
[language: en-US]
Add assertions in Apidog:
Status code is 200
Response body has field transcript
Response body, field confidence is greater than 0.7
FAQ
Does FFmpeg lose quality when converting between compressed formats?
Yes. Each conversion (e.g., MP3 → AAC) re-encodes the audio, which causes quality loss. For best results, convert from the original source.
What’s the best format for web audio?
MP3 for browser compatibility. AAC is more efficient at the same bitrate. OGG Vorbis is open-source but less widely supported. MP3 at 128–192k is generally safe.
How do I check an audio file’s actual format?
ffmpeg -i filename.mp3
Look for “Audio:” in the output.
Can FFmpeg convert audio and video together?
Yes. Use -c:v copy -c:a aac to copy video and re-encode audio only (faster).
Top comments (0)