Python3 tool to download audio from a YouTube video and convert it to MP3.
You should only retrieve copyright-free MP3 sounds.
- Python 3
- ffmpeg (required for MP3 conversion)
- Python packages from
requirements.txt(mainlyyt-dlp) - Google Chrome installed (cookies are read from the Chrome profile)
python3 -m venv myenv
source myenv/bin/activate
pip install -r requirements.txtIf you get HTTP 403 from YouTube:
pip install -U yt-dlp
# or
python3 -m pip install -U --pre "yt-dlp[default]"The script uses the yt-dlp Python API in a single pass:
- One metadata extraction (
title,channel, formats, …) - Local filename build (no extra network calls)
- Download + MP3 conversion, reusing the same metadata
Chrome cookies are loaded once per run. This replaces the old design that
spawned 2–3 separate yt-dlp CLI processes.
From the folder that contains the script:
python3 youtube2mp3.py -u "https://www.youtube.com/watch?v=YOUTUBE_ID"or with a bare video ID:
python3 youtube2mp3.py -v YOUTUBE_ID| Flag | Meaning |
|---|---|
-u / --url |
Full YouTube URL |
-v / --video_id |
Video ID only |
-a / --artist |
Prepend a manual artist name to the filename |
-n / --add_account_name |
Prepend the YouTube channel name |
-q / --audio_quality |
MP3 bitrate (default: 128K) |
-b / --background |
Start download in background and return immediately |
Without artist:
python3 youtube2mp3.py -u "https://www.youtube.com/watch?v=..."With artist:
python3 youtube2mp3.py -u "https://www.youtube.com/watch?v=..." -a "Artist Name"With video ID, channel name in filename, and custom quality:
python3 youtube2mp3.py -v "VIDEO_ID" -n -q 192KReturns the shell immediately; progress goes to a log file:
python3 youtube2mp3.py -v "VIDEO_ID" -n -bExample output:
Download started in background (pid 12345).
Log: /path/to/youtube2mp3_VIDEO_ID.log
Follow with: tail -f youtube2mp3_VIDEO_ID.log
tail -f youtube2mp3_VIDEO_ID.logEquivalent shell approach without -b:
nohup python3 youtube2mp3.py -v "VIDEO_ID" -n > dl.log 2>&1 &Pattern (parts omitted when empty):
[channel - ][artist - ]title.mp3
Invalid filename characters are stripped/sanitized automatically.
- Default audio quality is
128KMP3 (re-encoded from the best available audio). - Playlists are disabled (
noplaylist). - When finished, leave the venv with
deactivate.