Extract Audio from Video with FFmpeg or an API
Extract audio from video with FFmpeg: copy the track with no quality loss, convert to MP3 or WAV, make audio for transcription, and do it with one API call.
To extract audio from a video with FFmpeg, drop the video stream with -vn and copy the audio: ffmpeg -i input.mp4 -vn -c:a copy output.m4a. To get an MP3 in place of the original codec, re-encode: ffmpeg -i input.mp4 -vn -c:a libmp3lame -q:a 2 output.mp3.
This guide explains both commands, how to make audio for transcription, and how to do the same job with one Xora API call. For other common commands, see the FFmpeg cheat sheet.
Step 1: Check the audio codec
ffprobe -v error -select_streams a -show_entries stream=index,codec_name,channels,sample_rate -of compact input.mp4The codec tells you which file extension a copy needs. With no output, the file has no audio stream. For the same check as an API call, see the ffprobe API guide.
| Audio codec | Extension for a copy |
|---|---|
aac | .m4a (or .aac) |
mp3 | .mp3 |
opus | .opus |
vorbis | .ogg |
flac | .flac |
ac3, eac3, dts | Re-encode to MP3 or AAC for wide playback. |
Step 2a: Copy the audio (no quality loss)
ffmpeg -i input.mp4 -vn -c:a copy output.m4a| Flag | What it does |
|---|---|
-vn | No video in the output. |
-c:a copy | Copy the audio stream unchanged. No quality loss. |
output.m4a | The extension must fit the codec, or FFmpeg stops with an error. |
Step 2b: Convert to MP3, AAC or WAV
# MP3, high-quality variable bitrateffmpeg -i input.mp4 -vn -c:a libmp3lame -q:a 2 output.mp3
# AAC at 192 kbpsffmpeg -i input.mp4 -vn -c:a aac -b:a 192k output.m4a
# WAV, 16-bit PCMffmpeg -i input.mp4 -vn -c:a pcm_s16le output.wavThe same job with the Xora API
The extractAudio recipe does Step 1 for you. It probes the input, copies the stream when the codec has a web-friendly extension, and converts to output.format (mp3, aac or wav) only when a copy is not possible:
{ "mode": "recipe", "input": { "url": "https://example.com/interview.mp4" }, "output": { "format": "mp3" }, "recipe": { "name": "extractAudio" }, "stageInput": false}Because of the copy, the file can have a different extension from output.format, for example result.aac from an AAC source. To always get MP3, send your own arguments:
{ "mode": "ffmpeg", "input_files": { "in_1": "https://example.com/interview.mp4" }, "output_files": { "out_1": "interview_audio.mp3" }, "ffmpeg": { "args": ["-i", "{{in_1}}", "-vn", "-c:a", "libmp3lame", "-q:a", "2", "{{out_1}}"] }}Send the body to https://api.xora.sh/v1/jobs with Authorization: Bearer YOUR_API_KEY, then poll GET /v1/jobs/{id} or pass webhookUrl. See the extract audio recipe.
Variants
Audio for transcription
Speech does not need stereo or a high sample rate. A mono, 16 kHz file is small and is accepted by many speech-to-text services:
ffmpeg -i input.mp4 -vn -ac 1 -ar 16000 -c:a pcm_s16le audio.wavWith Xora, send the same flags in mode: "ffmpeg" with an output name such as audio_16k.wav.
Only part of the audio
Add -ss and -t: ffmpeg -ss 60 -i input.mp4 -t 300 -vn -c:a copy part.m4a. The extractAudio recipe also accepts startSeconds and durationSeconds. More on cuts: how to trim video with FFmpeg.
A different audio track
When a file has more than one audio track (for example two languages), choose one with -map 0:a:1 for the second track.
Common errors
| Error | Cause | Fix |
|---|---|---|
Output file does not contain any stream | The video has no audio stream. | Check with ffprobe. There is nothing to extract. |
Could not find tag for codec ... in stream #0 | The extension does not fit the copied codec. | Use the extension from the table above, or re-encode. |
UNSUPPORTED_JOB (Xora, job rejected) | The input has no audio stream. | Check hasAudio with a probe job first. |
The Xora file is .aac, not .mp3 | The recipe copied the AAC stream. | Expected. To always get MP3, use mode: "ffmpeg" with libmp3lame. |
Failed and rejected Xora jobs cost nothing. See error handling.
Related
- Extract audio recipe reference.
- FFmpeg cheat sheet: the common commands, each as an API call.
- How to trim and cut video with FFmpeg.
Frequently asked questions
How do I extract audio from a video with FFmpeg without losing quality?
Copy the audio stream: ffmpeg -i input.mp4 -vn -c:a copy output.m4a. Use a container that fits the codec: .m4a for AAC, .mp3 for MP3, .opus or .ogg for Opus. Check the codec first with ffprobe.
How do I convert a video to MP3 with FFmpeg?
Run ffmpeg -i input.mp4 -vn -c:a libmp3lame -q:a 2 output.mp3. -vn drops the video, and -q:a 2 gives a variable bitrate of high quality. Use -b:a 192k for a fixed bitrate.
What audio format is best for speech-to-text?
Many transcription services accept MP3 and WAV. A mono, 16 kHz WAV is a common choice for speech: ffmpeg -i input.mp4 -vn -ac 1 -ar 16000 -c:a pcm_s16le audio.wav. Check the input formats that your service lists.
Why did the Xora extractAudio job return .aac when I asked for mp3?
The extractAudio recipe copies the audio stream when it can, which keeps the quality and uses less machine time. output.format is the format it converts to only when a copy is not possible. To always get MP3, use mode ffmpeg with libmp3lame.