How to download mp3 from youtube?

With python script of course 🙂

http://www.primalsecurity.net


First install python, pip, ffmpeg and git:

apt install python python-pip ffmpeg git

Next step is to install youtube_dl:

sudo pip install --upgrade youtube_dl

Lets get python script from git:

git clone https://gist.github.com/benzap/90ff22790bc0a9c6fd2902e91da4baef

This is source code:
[php]
# Requires: youtube_dl module
# Requires: ffmpeg
# Usage:
#
# python youtube2mp3.py <URL>, …
#
# Example:
#
# python youtube2mp3.py https://www.youtube.com/watch?v=dQw4w9WgXcQ

import youtube_dl
import sys

ydl_opts = {
‘format’: ‘bestaudio/best’,
‘postprocessors’: [{
‘key’: ‘FFmpegExtractAudio’,
‘preferredcodec’: ‘mp3’,
‘preferredquality’: ‘192’,
}],
}

if __name__ == “__main__”:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
filenames = sys.argv[1:]
ydl.download(filenames)
[/php]

Move script from folder 90ff22790bc0a9c6fd2902e91da4baef:

mv 90ff22790bc0a9c6fd2902e91da4baef/youtube2mp3.py .

Now we are ready to fetch mp3:

python youtube2mp3.py https://www.youtube.com/watch?v=PAzZ0jo9MdI

This is my example:

hamster@wheel:~/Scripts/youtube$ python youtube2mp3.py https://www.youtube.com/watch?v=PAzZ0jo9MdI
[youtube] PAzZ0jo9MdI: Downloading webpage
[youtube] PAzZ0jo9MdI: Downloading video info webpage
[youtube] PAzZ0jo9MdI: Extracting video information
WARNING: "id" field is not a string - forcing string conversion, there is an error in extractor
[download] Destination: Greatest Rock N Roll Vietnam War Music - 60s and 70s Classic Rock Songs-PAzZ0jo9MdI.webm
[download] 100% of 65.34MiB in 00:01
[ffmpeg] Destination: Greatest Rock N Roll Vietnam War Music - 60s and 70s Classic Rock Songs-PAzZ0jo9MdI.mp3
Deleting original file Greatest Rock N Roll Vietnam War Music - 60s and 70s Classic Rock Songs-PAzZ0jo9MdI.webm (pass -k to keep)