TL;DR
FFmpeg converts audio formats with one command: ffmpeg -i input.wav output.mp3. Use -b:a for bitrate, -ar for sample rate, and -ac for channel count. For batch conversion, loop over files in your shell or PowerShell. If the converted audio is sent to an AI audio API, validate the API request and response before wiring it into production.
Introduction
Audio conversion is one of FFmpeg’s most common use cases. You might receive a WAV file from a recording workflow and need MP3 for web delivery, or get FLAC from a production pipeline and need AAC for a mobile app.
This guide shows how to:
- Convert between common audio formats
- Control bitrate, sample rate, and channel count
- Batch-process files
- Trim audio during conversion
- Prepare audio for AI API workflows
- Troubleshoot common FFmpeg errors
Installation
Install FFmpeg first if it is not already available on your system.
macOS
brew install ffmpeg
Ubuntu/Debian
sudo apt install ffmpeg
Windows
Download FFmpeg from ffmpeg.org, then add the bin folder to your PATH.
Verify the installation:
ffmpeg -version
Basic conversion syntax
The general pattern is:
ffmpeg -i input_file.ext output_file.ext
FFmpeg usually infers the output format from the file extension. For simple conversions, that is enough.
Example:
ffmpeg -i input.wav output.mp3
Common audio conversions
WAV to MP3
ffmpeg -i recording.wav -b:a 192k output.mp3
The -b:a 192k flag sets the audio bitrate to 192 kbps.
Common bitrate values:
-
128k— standard web quality -
192k— good quality, common for music streaming -
320k— high quality, larger file size
For spoken word audio such as podcasts or voice notes, 128k is usually enough. For music, use 192k or higher.
FLAC to AAC
ffmpeg -i lossless.flac -c:a aac -b:a 256k output.aac
The -c:a aac flag explicitly selects the AAC audio codec. FFmpeg can often infer this from the output extension, but setting the codec directly makes the command clearer and more reliable.
MP3 to WAV
ffmpeg -i compressed.mp3 output.wav
This decodes MP3 into PCM WAV. The output file will be much larger because WAV is uncompressed.
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
Set bitrate
Use -b:a to set a constant audio bitrate:
ffmpeg -i input.wav -b:a 192k output.mp3
For variable bitrate MP3 output, use -q:a:
ffmpeg -i input.wav -q:a 2 output.mp3
For MP3, -q:a ranges from 0 to 9:
-
0— highest quality -
2— high quality -
5-7— acceptable for speech in many cases -
9— lowest quality
Set sample rate
Use -ar to set the output sample rate:
ffmpeg -i input.wav -ar 44100 output.mp3
Common values:
-
22050— smaller files, lower quality -
44100— standard CD-quality audio -
48000— common for professional audio and video workflows
Set channel count
Use -ac to set the number of audio channels:
ffmpeg -i stereo.wav -ac 1 mono.mp3
-ac 1 converts stereo audio to mono. This is useful for voice content where stereo does not add value.
Control output codec and quality
For MP3, use libmp3lame when you want explicit control over the encoder:
ffmpeg -i input.wav -c:a libmp3lame -b:a 192k -ar 44100 -ac 2 output.mp3
This command:
- Reads
input.wav - Encodes with
libmp3lame - Sets bitrate to
192k - Sets sample rate to
44100 - Outputs stereo audio with
-ac 2
For AAC:
ffmpeg -i input.wav -c:a aac -q:a 1.5 output.aac
Batch processing
Batch convert WAV to MP3 on macOS/Linux
for file in *.wav; do
ffmpeg -i "$file" -b:a 192k "${file%.wav}.mp3"
done
This converts every .wav file in the current directory to .mp3.
The expression:
${file%.wav}
removes the .wav extension before appending .mp3.
Batch convert into an output directory
mkdir output
for file in *.flac; do
ffmpeg -i "$file" -c:a aac -b:a 256k "output/${file%.flac}.aac"
done
This keeps the original files in place and writes converted files to output/.
Batch convert on Windows PowerShell
Get-ChildItem *.wav | ForEach-Object {
$output = $_.BaseName + ".mp3"
ffmpeg -i $_.FullName -b:a 192k $output
}
Trim audio during conversion
You can trim and convert in the same command.
Start at 1 minute 30 seconds and export 60 seconds:
ffmpeg -i input.mp3 -ss 00:01:30 -t 60 -b:a 192k clip.mp3
Flags:
-
-ss 00:01:30— start at 1 minute 30 seconds -
-t 60— capture 60 seconds
Use -to when you want to specify the end timestamp instead of duration:
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. If the filename contains spaces, wrap it in quotes:
ffmpeg -i "my file.wav" output.mp3
Invalid data found when processing input
The file may be corrupted or the extension may not match the actual format.
Inspect the file:
ffmpeg -i filename
FFmpeg will print metadata about the file and detected streams.
Conversion failed! with codec errors
Specify the audio codec explicitly.
For MP3:
ffmpeg -i input.wav -c:a libmp3lame -b:a 192k output.mp3
For AAC:
ffmpeg -i input.wav -c:a aac -b:a 256k output.aac
Output file is larger than expected
You may be converting from a compressed format such as MP3 to an uncompressed format such as WAV.
For example:
ffmpeg -i input.mp3 output.wav
This usually creates a much larger file because WAV is uncompressed.
Integrating with AI audio APIs
Audio conversion is often a preprocessing step before sending audio to AI APIs for transcription, sentiment analysis, or speaker identification.
A typical pipeline:
- Receive audio in any format, such as WAV, MP3, or M4A.
- Convert it to the format required by the AI API.
- Send the converted file with a
POSTrequest. - Validate the API response before using it in your application.
Many speech APIs expect WAV audio with:
- 16 kHz sample rate
- Mono channel
- 16-bit PCM encoding
Convert an M4A file into that format:
ffmpeg -i input.m4a -ar 16000 -ac 1 -c:a pcm_s16le processed.wav
Flags:
-
-ar 16000— sets the sample rate to 16 kHz -
-ac 1— converts to mono -
-c:a pcm_s16le— outputs 16-bit little-endian PCM
Test the API request
After conversion, test the API endpoint before integrating it into application code.
Example request:
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 response checks such as:
Status code is 200
Response body has field transcript
Response body field confidence is greater than 0.7
These checks help catch unexpected response structures before they cause silent production errors.
FAQ
Does FFmpeg lose quality when converting between compressed formats?
Yes. Converting from MP3 to AAC, for example, decodes and re-encodes the audio, which introduces generation loss.
If possible, convert from the original uncompressed source. For archival workflows, use a lossless format.
What is the best format for web audio?
MP3 has the widest browser support. AAC is slightly more efficient at the same bitrate. OGG Vorbis is open-source and provides good quality.
For most web use cases, MP3 at 128k to 192k is the safest choice.
How do I check what format an audio file actually uses?
Run:
ffmpeg -i filename.mp3
FFmpeg prints file information without converting. Look for the Audio: line to see the codec, sample rate, bitrate, and channel layout.
Can FFmpeg convert audio and video together?
Yes. For example, this command copies the video stream and re-encodes only the audio:
ffmpeg -i input.mp4 -c:v copy -c:a aac output.mp4
-c:v copy avoids re-encoding the video, which is faster than processing both streams.
Top comments (0)