Last active
September 29, 2020 01:50
-
-
Save martjanz/966e0164024d27f9b56949a6eed7933b to your computer and use it in GitHub Desktop.
YouTube Playlists Downloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -- Requirements (Python 3)-- | |
import re | |
import time | |
import traceback | |
# External dependency: pytube3 (pip install pytube3) | |
from pytube import Playlist | |
from pytube import YouTube | |
# -- Parameters -- | |
media_type = 'video' # or 'audio' | |
# Playlists to download | |
playlists = [ | |
"https://www.youtube.com/playlist?list=PLBS5efQ3a3-koBp48-X-DiROrpqpJhOf7", | |
"https://www.youtube.com/playlist?list=PLBS5efQ3a3-nbrtEoErX_fAwW7yp0MTnL", | |
] | |
# -- Code -- | |
def download_audio(url): | |
print('Downloading audio from {}...'.format(url)) | |
try: | |
YouTube(video_url) \ | |
.streams \ | |
.filter(only_audio=True, file_extension='mp4')[0] \ | |
.download() | |
except Exception as e: | |
traceback.print_exc() | |
pass | |
def download_video(url): | |
print('Downloading video from {}...'.format(url)) | |
try: | |
YouTube(url) \ | |
.streams \ | |
.filter(progressive=True, file_extension='mp4') \ | |
.order_by('resolution') \ | |
.desc() \ | |
.first() \ | |
.download() | |
except Exception as e: | |
traceback.print_exc() | |
pass | |
for playlist_url in playlists: | |
playlist = Playlist(playlist_url) | |
print('Downloading {} {}s from playlist...'.format(len(playlist.video_urls), media_type)) | |
for video_url in playlist.video_urls: | |
if media_type == 'video': | |
download_video(video_url) # Download audio | |
elif media_type == 'audio': | |
download_audio(video_url) # Download video (with audio) | |
else: | |
print('Media type not supported. Check "media_type" variable.') | |
# Throttle to avoid YouTube restriction (Too many requests) | |
time.sleep(3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pytube3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment