DEV Community

Cover image for How to convert audio with FFmpeg: WAV, MP3, AAC, FLAC, and batch processing
Wanda
Wanda

Posted on • Originally published at apidog.com

How to convert audio with FFmpeg: WAV, MP3, AAC, FLAC, and batch processing

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.

Try Apidog today

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
Enter fullscreen mode Exit fullscreen mode

Ubuntu/Debian:

sudo apt install ffmpeg
Enter fullscreen mode Exit fullscreen mode

Windows: Download from ffmpeg.org and add the bin folder to your PATH.

Verify installation:

ffmpeg -version
Enter fullscreen mode Exit fullscreen mode

Basic conversion syntax

The simplest conversion command:

ffmpeg -i input_file.ext output_file.ext
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • -b:a 192k sets 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
Enter fullscreen mode Exit fullscreen mode
  • -c:a aac specifies the AAC codec explicitly.

MP3 to WAV

ffmpeg -i compressed.mp3 output.wav
Enter fullscreen mode Exit fullscreen mode

Converts to uncompressed PCM WAV—expect a larger file.

OGG to MP3

ffmpeg -i podcast.ogg -b:a 192k output.mp3
Enter fullscreen mode Exit fullscreen mode

M4A to MP3

ffmpeg -i track.m4a -b:a 192k output.mp3
Enter fullscreen mode Exit fullscreen mode

Audio quality flags

Bitrate

Set constant bitrate (CBR):

ffmpeg -i input.wav -b:a 192k output.mp3
Enter fullscreen mode Exit fullscreen mode

For variable bitrate (VBR):

ffmpeg -i input.wav -q:a 2 output.mp3
Enter fullscreen mode Exit fullscreen mode
  • -q:a ranges 0 (highest quality) to 9 (lowest). Use 0–2 for music, 5–7 for speech.

Sample rate

ffmpeg -i input.wav -ar 44100 output.mp3
Enter fullscreen mode Exit fullscreen mode
  • -ar 44100 sets sample rate to 44.1 kHz (CD). Alternatives: 22050 (smaller files), 48000 (pro audio).

Channels

ffmpeg -i stereo.wav -ac 1 mono.mp3
Enter fullscreen mode Exit fullscreen mode
  • -ac 1 converts 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
Enter fullscreen mode Exit fullscreen mode

For AAC with quality mode:

ffmpeg -i input.wav -c:a aac -q:a 1.5 output.aac
Enter fullscreen mode Exit fullscreen mode

Batch processing

Shell loop (macOS/Linux)

for file in *.wav; do
  ffmpeg -i "$file" -b:a 192k "${file%.wav}.mp3"
done
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

PowerShell (Windows)

Get-ChildItem *.wav | ForEach-Object {
  $output = $_.BaseName + ".mp3"
  ffmpeg -i $_.FullName -b:a 192k $output
}
Enter fullscreen mode Exit fullscreen mode

Trimming during conversion

Extract a specific segment while converting:

ffmpeg -i input.mp3 -ss 00:01:30 -t 60 -b:a 192k clip.mp3
Enter fullscreen mode Exit fullscreen mode
  • -ss 00:01:30 starts at 1:30.
  • -t 60 extracts 60 seconds.

Or set an end time with -to:

ffmpeg -i input.mp3 -ss 00:01:30 -to 00:02:30 output.mp3
Enter fullscreen mode Exit fullscreen mode

Common errors and fixes

“No such file or directory”

Check the input path. Quote paths with spaces:

ffmpeg -i "my file.wav" output.mp3
Enter fullscreen mode Exit fullscreen mode

“Invalid data found when processing input”

The file may be corrupted or misnamed. Inspect format:

ffmpeg -i filename
Enter fullscreen mode Exit fullscreen mode

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:

  1. Receive audio (WAV, MP3, M4A, etc.)
  2. Convert to required format (often WAV 16kHz mono)
  3. POST to API

Convert for speech APIs:

ffmpeg -i input.m4a -ar 16000 -ac 1 -c:a pcm_s16le processed.wav
Enter fullscreen mode Exit fullscreen mode
  • -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]
Enter fullscreen mode Exit fullscreen mode

Add assertions in Apidog:

Status code is 200
Response body has field transcript
Response body, field confidence is greater than 0.7
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)