Created
October 10, 2021 12:42
-
-
Save AndreasLonn/2a355b755dbbe8cd1727644df7cd22ad to your computer and use it in GitHub Desktop.
The script can either take command line arguments or, if none is supplied, ask for it. Usage with command line arguments: python3 youtube-download.py link-to-YouTube-video where-to-download
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
# The script can either take command line arguments or, if none is supplied, | |
# ask for it | |
# Usage with command line arguments: | |
# python3 youtube-download.py link-to-YouTube-video where-to-download | |
from pytube import YouTube | |
import subprocess, sys | |
# Get the YouTube link and where to save the file | |
# Use the arguments from the calling command line | |
# English: 'Link to the YouTube video:\n' | |
link = sys.argv[1] if sys.argv[1] else input('Länk till YouTube-videon:\n') | |
path = sys.argv[2] if sys.argv[2] else 'C:\\Users\\Username\\Videos' | |
def progress(stream, data_chunk, remaining): | |
""" | |
Handle progress updates | |
""" | |
# Print how much is completed | |
# Carriage return ('\r') is used to set the cursor to the start of the | |
# line to overwrite the previously written percent | |
print('\r{:.0f}%'.format(100 - remaining / stream.filesize * 100, 2), | |
end='') | |
def handleException(msg, e=''): | |
""" | |
Handle exceptions | |
""" | |
print(msg) | |
if e: print(e) | |
# English: 'Press Enter to continue' | |
input('Tryck Enter för att fortsätta') | |
exit() | |
try: | |
# Make a YouTube object with the supplied link | |
# and set the progress callback | |
yt = YouTube(link, on_progress_callback=progress) | |
length = yt.length | |
# Show hours only if necessary | |
if length >= 3600: | |
lengthStr = '{}:{:0>2}:{:0>2}'\ | |
.format(length // 3600, (length % 3600) // 60, length % 60) | |
else: | |
lengthStr = '{}:{:0>2}'.format(length // 60, length % 60) | |
except Exception as e: | |
# English: 'Couldn\'t load the video:' | |
handleException('Kunde inte läsa in videon:', e) | |
# English: 'Downloading \'{}\' by \'{}\'. ({})' | |
print('Laddar ner \'{}\' av \'{}\'. ({})'\ | |
.format(yt.title, yt.author, lengthStr)) | |
try: | |
# Check that there are progressive mp4 streams | |
# The progressive streams contain both video and audio, but is | |
# only availible for 720p and lower. For high resolution (>= 1080p) | |
# the video and audio has to be downloaded separately and then combined | |
# using another program like FFmpeg or the Windows Photos app | |
if len(yt.streams.filter(progressive=True, file_extension='mp4')) > 0: | |
outputFile = yt.streams\ | |
.filter(progressive=True, file_extension='mp4')\ | |
.order_by('resolution')\ | |
.desc().first().download(path) | |
print() | |
else: | |
# English: 'Couldn't find a downloadable stream :(' | |
handleException('Kunde inte hitta en nedladdningsbar stream :(') | |
except Exception as e: | |
# English: 'An error occurred during download:' | |
handleException('Ett fel uppstod vid nedladdningen:', e) | |
# English: 'Download successful :)' | |
print('Nedladdningen lyckades :)') | |
# English: 'The file is here:' | |
print('Filen hamnade här:') | |
print('"' + outputFile + '"') | |
# Revieal the file in Explorer | |
subprocess.Popen(r'explorer /select,"{}"'.format(outputFile)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment