{"slug": "extract-the-audio-from-a-video-with-ffmpeg-from-command-line", "title": "Extract the audio from a video with ffmpeg from command line", "summary": "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.", "body_md": "You have a recording of a call or a talk, and you only want the audio.\n\nHow do you do it?\n\nYou ask an AI, and it gives you the classic line:\n\n```\nffmpeg -i \"Team call.mp4\" -vn -acodec copy out.mp3\n```\n\nIt fails:\n\n```\nInvalid audio stream. Exactly one MP3 audio stream is required.\nCould not write header (incorrect codec parameters ?): Invalid argument\n```\n\nand 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.\n\nSo 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.\n\nWith [cmdbook](https://printf.app/cmdbook), the script has one place to live, and it gets a GUI for free.\n\n**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.\n\n**2. Mark what changes.** The video and the format become placeholders:\n\n```\nIN=\"{{prompt:video}}\"\nFORMAT={{prompt:format?original|mp3|m4a|wav}}\n```\n\n`original` keeps the audio as it is: no quality loss, and almost no time.\n\n**3. Click Run.** cmdbook shows a form. Drag the video onto it, pick a format, press Enter.\n\n**4. Get the file.** The audio is saved next to the video:\n\n``` php\nAudio track: aac -> original\n47M video ->  19M  Long meeting.m4a\n```\n\nFor 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.\n\nNext time, your last format is already selected.\n\nThat's all a script needs to get a GUI: mark the parts that change. The rest is the script you already have.\n\nTry cmdbook here: [printf.app/cmdbook](https://printf.app/cmdbook)\n\nThe full script (macOS, needs ffmpeg)\n\n```\nIN=\"{{prompt:video}}\"\nFORMAT={{prompt:format?original|mp3|m4a|wav}}\n\nexport PATH=\"/opt/homebrew/bin:/usr/local/bin:$PATH\"\ncommand -v ffmpeg &gt;/dev/null || { echo \"ffmpeg not found. Install it: brew install ffmpeg\"; exit 1; }\n[ -f \"$IN\" ] || { echo \"Not a file: $IN\"; exit 1; }\n\n# What audio is inside? \"original\" keeps it as is (fast, no quality loss),\n# so the file extension has to match the codec.\nCODEC=$(ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of csv=p=0 \"$IN\")\nif [ -z \"$CODEC\" ]; then\n  echo \"No audio track in $IN.\"\n  exit 1\nfi\n\ncase \"$FORMAT\" in\n  original)\n    case \"$CODEC\" in\n      aac|alac) EXT=m4a ;;\n      mp3)      EXT=mp3 ;;\n      opus)     EXT=opus ;;\n      vorbis)   EXT=ogg ;;\n      flac)     EXT=flac ;;\n      pcm_*)    EXT=wav ;;\n      *)        EXT=mka ;;\n    esac\n    CODEC_ARGS=\"-c:a copy\" ;;\n  mp3) EXT=mp3; CODEC_ARGS=\"-c:a libmp3lame -q:a 2\" ;;\n  m4a) EXT=m4a; CODEC_ARGS=\"-c:a aac -b:a 192k\" ;;\n  wav) EXT=wav; CODEC_ARGS=\"-c:a pcm_s16le\" ;;\n  *)   echo \"Unknown format: $FORMAT\"; exit 1 ;;\nesac\n\nOUT=\"${IN%.*}.$EXT\"\nif [ -e \"$OUT\" ]; then\n  printf \"%s already exists. Overwrite? [y/N] \" \"$OUT\"\n  read -r ANSWER\n  case \"$ANSWER\" in y|Y|yes) ;; *) echo \"Left as is.\"; exit 0 ;; esac\nfi\n\necho \"Audio track: $CODEC -&gt; $FORMAT\"\nif ! ffmpeg -nostdin -hide_banner -v error -stats -y -i \"$IN\" -map 0:a:0 $CODEC_ARGS \"$OUT\"; then\n  rm -f \"$OUT\"\n  exit 1\nfi\necho \"$(du -h \"$IN\" | cut -f1) video -&gt; $(du -h \"$OUT\" | cut -f1)  $OUT\"\n```\n\n", "url": "https://wpnews.pro/news/extract-the-audio-from-a-video-with-ffmpeg-from-command-line", "canonical_source": "https://dev.to/yingtse/extract-the-audio-from-a-video-with-ffmpeg-from-command-line-31ba", "published_at": "2026-11-21 01:30:00+00:00", "updated_at": "2026-11-21 01:42:01.428893+00:00", "lang": "en", "topics": ["developer-tools", "ai-tools"], "entities": ["cmdbook", "ffmpeg", "ffprobe", "printf.app"], "also_reported_by": [], "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.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/extract-the-audio-from-a-video-with-ffmpeg-from-command-line", "markdown": "https://wpnews.pro/news/extract-the-audio-from-a-video-with-ffmpeg-from-command-line.md", "text": "https://wpnews.pro/news/extract-the-audio-from-a-video-with-ffmpeg-from-command-line.txt", "jsonld": "https://wpnews.pro/news/extract-the-audio-from-a-video-with-ffmpeg-from-command-line.jsonld"}}