Skip to content

Instantly share code, notes, and snippets.

@carlosmgv02
Last active December 20, 2023 16:01
Show Gist options
  • Save carlosmgv02/3e932a006c9e273e89b89b5911270a91 to your computer and use it in GitHub Desktop.
Save carlosmgv02/3e932a006c9e273e89b89b5911270a91 to your computer and use it in GitHub Desktop.

Python youtube video downloader

This code consists of a python script which allows us to easily download YouTube videos/audio by passing its URL via the command line.
python_32x32

Content

Project files

  • youtube.py: the script's code
  • aux_functions: contains multiple functions used to customize the output color and animations.

Default output

By default, the script outputs the file to: D:\YouTube, you can simply change it by modifying the path variable.

Execution

You can download the code and easily set the path as a system environmental variable in order to be able to run the script from anywhere on your computer.

How to run without the path modified:

  1. Go to your script's directory, mine is:
cd /d d:\WebDev\Python\Scripts
  1. Run the following command:
  2. Select wether you want to download the video or audio.
python youtube.py https://www.youtube.com/watch?v=-mwpoE0x0JQ

How to run with the path modified:

  1. Simply run the following command:
youtube.py https://www.youtube.com/watch?v=-mwpoE0x0JQ
  1. Select wether you want to download the video or audio.

Functions

Conti

This auxiliar function writes character by character and gives the program a very nice looking.

def conti(s):
    for ch in s:
        sys.stdout.write(ch)
        sys.stdout.flush()
        time.sleep(0.02)
    print("\n")

Bcolors

This class gives us the ability to change the output colors by calling its elements.

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'
import readchar
import sys
import time
def conti(s):
for ch in s:
sys.stdout.write(ch)
sys.stdout.flush()
time.sleep(0.02)
print("\n")
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def press_to_leave():
conti(f"{bcolors.WARNING}Press any key to leave.{bcolors.ENDC}")
k = readchar.readchar()
import pytube.exceptions
from pytube import YouTube
from sys import argv
from aux_functions import *
import subprocess
def downloader():
link = argv[1] # video link passed as argument via command line
try:
yt = YouTube(link) # object we're going to work with
path = "D:\YouTube" # Folder where we're going to save out video/audio
done = False
# Menu
conti("Vídeo title: " + yt.title)
conti("1- Download video at maximum resolution.")
conti("2- Download audio only.")
conti("0- Exit program.")
# We make sure the user introduces a valid option
while not done:
option = int(input("\tR: "))
if option == 1:
yd = yt.streams.get_highest_resolution()
type = "video"
done = True
elif option == 2:
yd = yt.streams.get_audio_only("mp4")
type = "audio"
done = True
elif option == 0:
exit()
else:
conti(f"{bcolors.FAIL}INCORRECT OPTION, TRY AGAIN{bcolors.ENDC}")
conti(f"{bcolors.OKBLUE}DOWNLOADING {type}...{bcolors.ENDC}")
yd.download(path)
conti(f"{bcolors.OKGREEN}Your {type} has been downloaded to folder: {path}{bcolors.ENDC}")
conti(f"Press {bcolors.BOLD}o{bcolors.ENDC} to open file browser, else to exit.")
op = input()
if op == "o":
subprocess.Popen(f'explorer /select,"{path}"')
else:
exit()
except pytube.exceptions.RegexMatchError:
conti(f"{bcolors.FAIL}Video not found{bcolors.ENDC}")
if __name__ == "__main__":
downloader()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment