cd /news/developer-tools/extract-the-audio-from-a-video-with-… · home › topics › developer-tools › article
[ARTICLE · art-173581] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=↑ positive

Extract the audio from a video with ffmpeg from command line

A developer built a reusable ffmpeg script, packaged in the cmdbook tool, that extracts audio from video files by first probing the embedded codec with ffprobe and selecting a matching output extension, avoiding the common failure of copying AAC audio into an .mp3 container. The script offers an 'original' mode that copies the audio stream without re-encoding, extracting a 20-minute video's audio in under a second, while mp3 re-encoding took about 3 seconds. Marking the input file and format as placeholders gives the script a form-based GUI, with overwrite prompts and a no-audio-track check.

key takeaway

Probe the source codec with ffprobe before choosing an output container, since copying AAC into .mp3 fails; copying into a matching extension like .m4a avoids re-encoding and cut a 20-minute video's extraction to under a second.

by read3 min views3 publishedNov 21, 2026

You have a recording of a call or a talk, and you only want the audio.

How do you do it?

You ask an AI, and it gives you the classic line:

ffmpeg -i "Team call.mp4" -vn -acodec copy out.mp3

It fails:

Invalid audio stream. Exactly one MP3 audio stream is required.
Could not write header (incorrect codec parameters ?): Invalid argument

and leaves an empty out.mp3 behind. The audio inside an .mp4 is usually AAC, and copy can't put AAC into an .mp3. You ask again. Now you either re-encode to mp3, or copy into .m4a, which only works because this video happens to have AAC inside.

So you save the script somewhere: a Markdown note, or an extract-audio.sh file. Next time it's a .webm with different audio inside, and the extension in your script is wrong again.

With cmdbook, the script has one place to live, and it gets a GUI for free.

1. Save the script once. Create a command in cmdbook and paste the script (it's at the end). It asks ffprobe what audio is inside, then picks the right extension.

2. Mark what changes. The video and the format become placeholders:

IN="{{prompt:video}}"
FORMAT={{prompt:format?original|mp3|m4a|wav}}

original keeps the audio as it is: no quality loss, and almost no time.

3. Click Run. cmdbook shows a form. Drag the video onto it, pick a format, press Enter.

4. Get the file. The audio is saved next to the video:

Audio track: aac -> original
47M video ->  19M  Long meeting.m4a

For a 20-minute video, original took under a second on my Mac, and mp3 took 3 seconds. If the file already exists, it asks before overwriting. If the video has no audio, it says so.

Next time, your last format is already selected.

That's all a script needs to get a GUI: mark the parts that change. The rest is the script you already have.

Try cmdbook here: printf.app/cmdbook

The full script (macOS, needs ffmpeg)

IN="{{prompt:video}}"
FORMAT={{prompt:format?original|mp3|m4a|wav}}

export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH"
command -v ffmpeg >/dev/null || { echo "ffmpeg not found. Install it: brew install ffmpeg"; exit 1; }
[ -f "$IN" ] || { echo "Not a file: $IN"; exit 1; }

CODEC=$(ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of csv=p=0 "$IN")
if [ -z "$CODEC" ]; then
  echo "No audio track in $IN."
  exit 1
fi

case "$FORMAT" in
  original)
    case "$CODEC" in
      aac|alac) EXT=m4a ;;
      mp3)      EXT=mp3 ;;
      opus)     EXT=opus ;;
      vorbis)   EXT=ogg ;;
      flac)     EXT=flac ;;
      pcm_*)    EXT=wav ;;
      *)        EXT=mka ;;
    esac
    CODEC_ARGS="-c:a copy" ;;
  mp3) EXT=mp3; CODEC_ARGS="-c:a libmp3lame -q:a 2" ;;
  m4a) EXT=m4a; CODEC_ARGS="-c:a aac -b:a 192k" ;;
  wav) EXT=wav; CODEC_ARGS="-c:a pcm_s16le" ;;
  *)   echo "Unknown format: $FORMAT"; exit 1 ;;
esac

OUT="${IN%.*}.$EXT"
if [ -e "$OUT" ]; then
  printf "%s already exists. Overwrite? [y/N] " "$OUT"
  read -r ANSWER
  case "$ANSWER" in y|Y|yes) ;; *) echo "Left as is."; exit 0 ;; esac
fi

echo "Audio track: $CODEC -> $FORMAT"
if ! ffmpeg -nostdin -hide_banner -v error -stats -y -i "$IN" -map 0:a:0 $CODEC_ARGS "$OUT"; then
  rm -f "$OUT"
  exit 1
fi
echo "$(du -h "$IN" | cut -f1) video -> $(du -h "$OUT" | cut -f1)  $OUT"
── more in #developer-tools 4 stories · sorted by recency
── more on @cmdbook 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/extract-the-audio-fr…] indexed:0 read:3min 2026-11-21 · —