How to: Rip audio from YouTube with Python

Published 3 months ago: October 14, 2020


In this post

Disclaimer

Ripping audio from copyrighted music videos is, in most cases, not legal.

Prerequites

pip3 install youtube_dl

Script

Save this to a file and run it with a python interpreter. (python3 this_script.py)

import youtube_dl
from pathlib import Path

options = {
  'format': 'bestaudio/best',
  'extractaudio' : True,  # only keep the audio
  'audioformat' : "mp3",  # convert to mp3 
  'noplaylist' : True,    # only download single song, not playlist
}

options["artist"] = input("Artist:")
options["track"] = input("Track:")
options["genre"] = input("Genre:")

print("Youtube video URL:")
url = input()

filename = f'{options["artist"]} - {options["track"]}.mp3'

h = Path.home()
f = 'Downloads'
path = h.joinpath(f)
if not path.exists():
    path = h.joinpath(f.lower())
path = path.joinpath(filename)

options["outtmpl"] = str(path)

with youtube_dl.YoutubeDL(options) as ydl:
    ydl.download([url])

Christoffer Lybekk

Developer under development

Explore more articles with similar tags

PythonAudioMusicYouTubeGuide

Article stats


27words

6sentences

6paragraphs

1 minread time