←

ytmp3

ytmp3 logo

overview

a personal shell script for turning youtube links into local mp3 files. paste a url into the terminal, get an audio file back — just a script i keep here for reference. i use it when converting audio into samplable tracks for music production or spotify local files.

copy the script below into a local file, then run it from the terminal when i want a copy of something.

how it works

  1. pass one or more youtube urls as arguments.
  2. yt-dlp downloads the audio stream.
  3. ffmpeg converts it to mp3 with embedded metadata.
  4. files land in ~/Desktop/ConvertedMusic by default, or a folder you pass with -o.

stack

  • bash wrapper around yt-dlp and ffmpeg

setup

  1. install dependencies (one time): brew install yt-dlp ffmpeg
  2. create a scripts folder: mkdir -p ~/scripts
  3. open a new file in nano: nano ~/scripts/ytmp3
  4. copy the script from the section below and paste it into nano.
  5. save and exit: press ctrl + o, then enter, then ctrl + x.
  6. make it executable (one time): chmod +x ~/scripts/ytmp3

usage

  1. run with a youtube url: ~/scripts/ytmp3 "https://www.youtube.com/watch?v=..."
  2. mp3 files save to ~/Desktop/ConvertedMusic by default.
  3. override the output folder: ~/scripts/ytmp3 -o ~/Downloads "https://youtu.be/..."
  4. lower bitrate: ~/scripts/ytmp3 -q 2 "https://www.youtube.com/watch?v=..." (0 = best quality)
  5. show help: ~/scripts/ytmp3 -h

you can also run ytmp3 from anywhere without the full path if you'd like:

echo 'export PATH="$HOME/scripts:$PATH"' >> ~/.zshrc && source ~/.zshrc

script

#!/usr/bin/env bash
set -euo pipefail

usage() {
  cat <<'EOF'
ytmp3 — personal youtube-to-mp3 wrapper (yt-dlp + ffmpeg)

usage:
  ytmp3 [options] <url> [url...]

options:
  -o DIR    output directory (default: ~/Desktop/ConvertedMusic)
  -q N      audio quality 0=best..9=worst (default: 0)
  -h        show this help

requires: yt-dlp, ffmpeg
EOF
}

require_cmd() {
  if command -v "$1" >/dev/null 2>&1; then return 0; fi
  echo "ytmp3: missing dependency: $1" >&2
  if [[ "$(uname -s)" == "Darwin" ]] && command -v brew >/dev/null 2>&1; then
    echo "  install with: brew install $2" >&2
  fi
  exit 1
}

OUTPUT_DIR="$HOME/Desktop/ConvertedMusic"
QUALITY="0"

while getopts ":o:q:h" opt; do
  case "$opt" in
    o) OUTPUT_DIR="$OPTARG" ;;
    q) QUALITY="$OPTARG" ;;
    h) usage; exit 0 ;;
    *) usage >&2; exit 1 ;;
  esac
done
shift $((OPTIND - 1))

[[ $# -ge 1 ]] || { usage >&2; exit 1; }

require_cmd yt-dlp yt-dlp
require_cmd ffmpeg ffmpeg
mkdir -p "$OUTPUT_DIR"

for url in "$@"; do
  yt-dlp --no-playlist --extract-audio --audio-format mp3 \
    --audio-quality "$QUALITY" --embed-metadata \
    --output "$OUTPUT_DIR/%(title)s.%(ext)s" "$url"
done

echo "ytmp3: done — saved to $OUTPUT_DIR"