# Extract the audio from a video with ffmpeg from command line

> Source: <https://dev.to/yingtse/extract-the-audio-from-a-video-with-ffmpeg-from-command-line-31ba>
> Published: 2026-11-21 01:30:00+00:00

**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.

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](https://printf.app/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:

``` php
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](https://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 &gt;/dev/null || { echo "ffmpeg not found. Install it: brew install ffmpeg"; exit 1; }
[ -f "$IN" ] || { echo "Not a file: $IN"; exit 1; }

# What audio is inside? "original" keeps it as is (fast, no quality loss),
# so the file extension has to match the codec.
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 -&gt; $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 -&gt; $(du -h "$OUT" | cut -f1)  $OUT"
```


