Extracting audio from a video is a common need—whether for personal use or professional workflows. A simple command-line tool like ffmpeg can do it, but only if used correctly.
What Happens When You Try a Basic Command
Someone asks an AI for a script to extract audio from a video. The AI responds with:
ffmpeg -i “Team call.mp4” -vn -acodec copy out.mp3
This command fails. The error says: Invalid audio stream. Exactly one MP3 audio stream is required.
The issue is simple: most videos use AAC audio, not MP3. The copy option works only if the output format matches the input codec. Trying to copy AAC into MP3 fails because the formats are incompatible.
How a Working Script Solves the Problem
A more robust script detects the actual audio codec inside a video and chooses the correct output format. It avoids re-encoding when possible, preserving quality and speed.
The script first checks the video with ffprobe to identify the audio codec. Then it selects an output extension based on that codec:
- aac or alac →
.m4a - mp3 →
.mp3 - opus →
.opus - vorbis →
.ogg - flac →
.flac - pcm_* →
.wav - Any other →
.mka
For the original format, it uses -c:a copy to preserve the audio without re-encoding. This is fast and lossless.
How the Script Works in Practice
The script is designed to work with a tool called cmdbook. Users save the script once and then use it as a command in cmdbook.
Key steps:
- Save the script in a file or paste it into cmdbook.
- Mark two variables as placeholders:
IN="{{prompt:video}}"andFORMAT="{{prompt:format?original|mp3|m4a|wav}}". - Click Run. A form appears where users drag a video file and select a format.
- The script runs, extracts the audio, and saves it next to the original video.
Example output:
Audio track: aac → original
47M video → 19M Long meeting.m4a
For a 20-minute video, the original format takes under a second on a Mac. Re-encoding to MP3 takes about three seconds.
Limitations and Edge Cases
If the video has no audio, the script detects it and stops with a message: No audio track in $IN.

If the output file already exists, it asks the user whether to overwrite it. This prevents accidental data loss.
Next time the user runs the script, the last selected format is pre-filled—making the workflow faster.
The script only works on macOS with ffmpeg installed. Users must have ffmpeg in their path. If not, the script prompts: Install it: brew install ffmpeg.
Why This Matters
Many people work with video recordings—calls, meetings, interviews. Extracting audio efficiently saves time and avoids quality loss. A well-designed script with smart defaults makes this process accessible to non-technical users.
By marking only the changing parts, the script gains a user-friendly interface without needing to be rewritten. This shows how automation tools can turn simple scripts into usable workflows.
For more on video processing with shell scripts, see Convert Every Video in a Folder to GIF with a Shell Script.
For a local task app that lets you set your own price, see TaskOpp: A Local Task App Where You Set Your Own Price.
For how AI prompts evolve through real-world testing, see Three Real Halloween AI Video Tests Changed How I Write Prompts.
Learn how to measure if AI agents actually save time in How to Measure if AI Agents Actually Save Time.
And explore a local-first markdown editor that stays fast on large files in I Built a Local-First Markdown Editor: How It Stays Fast on Huge Docs.
The full script is available at Original source.
Sources & further reading
Featured image: original publisher image. Via original publisher. Image source
