Last active
May 23, 2020 11:59
-
-
Save sotlucas/be5a0b05753fb35ba326f9a2ca7713bc to your computer and use it in GitHub Desktop.
Downloads a whole youtube playlist & converts all to mp3
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
import os | |
import subprocess | |
import pytube | |
from time import sleep | |
import sys | |
def show_progress_bar(stream, chunk, file_handle, bytes_remaining): | |
""" | |
Shows progress bar | |
""" | |
size = stream.filesize | |
progress = (float(abs(bytes_remaining-size)/size))*float(100) | |
# Shows progress bar as |### | 20% | |
sys.stdout.write('\r') | |
sys.stdout.write("|%-20s| %d%%" % ('#'*(int(round(progress)/5)), progress)) | |
if (progress == 100): print('\n') | |
sys.stdout.flush() | |
def convert_to_mp3(vid): | |
""" | |
Converts the mp4 to mp3 audio. Keeps the same filename. | |
""" | |
parent_dir = os.path.dirname(os.path.abspath(__file__)) # directory where the script is run | |
default_filename = vid.default_filename # get default name using pytube API | |
new_filename = default_filename + '.mp3' # e.g. new_filename.mp3 | |
print(new_filename) | |
subprocess.run(['ffmpeg', '-hide_banner', '-loglevel', 'panic', '-i', | |
os.path.join(parent_dir, default_filename), | |
os.path.join(parent_dir, new_filename) | |
], stdout=subprocess.DEVNULL) | |
print('Conversion complete') | |
def download_all(video_urls): | |
""" | |
Downloads all videos from the given video_urls[] | |
""" | |
current = 1 | |
total = len(video_urls) | |
download_path = os.path.dirname(os.path.abspath(__file__)) # optional? | |
print('Starting download...') | |
print('Downloading directory: ', download_path) | |
for link in video_urls: | |
try: | |
yt = pytube.YouTube(link) | |
# Progress bar | |
yt.register_on_progress_callback(show_progress_bar) | |
# Filtering by mp4 audio | |
vid = yt.streams.filter( | |
only_audio=True, subtype='mp4', | |
).order_by('resolution').desc().first() | |
# Download fase | |
print('\nDownloading: ' + vid.default_filename) | |
vid.download() | |
# mp3 conservion handling | |
print('Converting to mp3...') | |
convert_to_mp3(vid) | |
# TODO: add progress bar for every video or for every playlist | |
print('Download & conversion complete: ' + vid.default_filename) | |
except Exception: | |
print('Video unavailable: ' + vid.default_filename + '. Not downloaded') | |
print(str(current) + ' of ' + str(total) + ' remaining') | |
current += 1 | |
if __name__ == "__main__": | |
pl_link = input("Enter the playlist link: ") | |
pl = pytube.Playlist(pl_link) | |
pl.populate_video_urls() | |
video_urls = pl.video_urls | |
# Ask for user verification | |
print('Total videos found: ', len(video_urls)) | |
option = input('¿Download all and convert to mp3? (Y)es | (N)o ') | |
if (option == 'Y'): | |
download_all(video_urls) | |
print('All done! Enjoy your music :)') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment