Skip to content

Instantly share code, notes, and snippets.

@darkman66
Last active September 20, 2021 20:44
Show Gist options
  • Save darkman66/66b2e1291cb5d284b2405f1aa4bd41e1 to your computer and use it in GitHub Desktop.
Save darkman66/66b2e1291cb5d284b2405f1aa4bd41e1 to your computer and use it in GitHub Desktop.
import youtube_dl
def extract_url(yt_url):
ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s%(ext)s', 'quiet':True,})
video = ""
with ydl:
result = ydl.extract_info \
(yt_url,
download=False) #We just want to extract the info
data = []
if 'entries' in result:
# Can be a playlist or a list of videos
video = result['entries']
#loops entries to grab each video_url
for i, item in enumerate(video):
video = result['entries'][i]
data.append(video['webpage_url'])
return data
def get_it(video_url):
video_info = youtube_dl.YoutubeDL().extract_info(
url = video_url,download=False
)
filename = f"{video_info['title']}.mp3"
options={
'format':'bestaudio/best',
'keepvideo':False,
'outtmpl':filename,
}
with youtube_dl.YoutubeDL(options) as ydl:
try:
ydl.download([video_info['webpage_url']])
except Exception as e:
print(f"'boo boo: {e}")
return filename
def run():
video_url = input("please enter youtube video url:")
if 'list=' in video_url:
for item in extract_url(video_url):
get_it(item)
else:
get_it(video_url)
print("Download complete... ")
if __name__=='__main__':
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment