ytmp3
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
- pass one or more youtube urls as arguments.
yt-dlpdownloads the audio stream.ffmpegconverts it to mp3 with embedded metadata.- files land in
~/Desktop/ConvertedMusicby default, or a folder you pass with-o.
stack
- bash wrapper around
yt-dlpandffmpeg
setup
- install dependencies (one time):
brew install yt-dlp ffmpeg - create a scripts folder:
mkdir -p ~/scripts - open a new file in nano:
nano ~/scripts/ytmp3 - copy the script from the section below and paste it into nano.
- save and exit: press
ctrl + o, then enter, thenctrl + x. - make it executable (one time):
chmod +x ~/scripts/ytmp3
usage
- run with a youtube url:
~/scripts/ytmp3 "https://www.youtube.com/watch?v=..." - mp3 files save to
~/Desktop/ConvertedMusicby default. - override the output folder:
~/scripts/ytmp3 -o ~/Downloads "https://youtu.be/..." - lower bitrate:
~/scripts/ytmp3 -q 2 "https://www.youtube.com/watch?v=..."(0 = best quality) - 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"