DEV Community

loading...
Cover image for Python Scripts- YouTube CLI Downloader

Python Scripts- YouTube CLI Downloader

envoy_ profile image Envoy-VC Updated on ・1 min read

Today i learnt about cli applications so here's a simple handy tool to download YouTube videos in your preferred resolution type using pytube and argparse.


To install these modules open Terminal/PowerShell and run -

pip install pytube
pip install argparse
Enter fullscreen mode Exit fullscreen mode

Some Examples of using it are as follows -

python main.py -u URL -O OUTPUT_PATH
python main.py -u https://www.youtube.com/watch?v=kJQP7kiw5Fk -O C:\Users\USERNAME\Downloads
Enter fullscreen mode Exit fullscreen mode

You get to choose among all the available resolutions

import argparse
from pytube import YouTube

parser = argparse.ArgumentParser(description="CLI Tool to download Youtube Videos")
parser.add_argument('-u','--url',help="Url for the Video")
parser.add_argument('-o','--out_dir',help="Output Location")
args = parser.parse_args()

def download(url,out_dir):
    try:
        yt = YouTube(url)
    except:
        print("Connection Error")
        return

    videos = yt.streams

    resolution_list = []
    for i in range(len(videos)):
        resolution_list.append((videos[i]).resolution)
    resolution_list = set(resolution_list)
    resolution_list.remove(None)
    print('Available Resolutions -')
    print(resolution_list)
    res = str(input('\n Enter your desired Resolution - '))
    video = videos.filter(resolution=res)
    video.first().download(out_dir)

download(args.url,args.out_dir)
Enter fullscreen mode Exit fullscreen mode

Visit my Blog Here

Discussion (0)

pic
Editor guide