Skip to content

Instantly share code, notes, and snippets.

@StackZeroSec
Last active October 4, 2022 03:05
Show Gist options
  • Save StackZeroSec/36b2071af2d4f8bca742d09342999524 to your computer and use it in GitHub Desktop.
Save StackZeroSec/36b2071af2d4f8bca742d09342999524 to your computer and use it in GitHub Desktop.
A simple youtube downloader in python with CLI options
import click
import pytube
from pytube.cli import on_progress
from typing import List
def print_streams(streams: List[pytube.Stream]):
for stream in streams:
print(f"itag={stream.itag} mime_type={stream.mime_type} "
f"res={stream.resolution} fps={stream.fps} vcodec={stream.video_codec} "
f"acodec={stream.audio_codec} type={stream.type}>\n")
@click.command()
@click.option('-U','--url', help='The url of the YT video', required=True)
@click.option('-P','--playlist', is_flag = True, default=False, help='The url of the playlist')
def main(url, playlist):
if not playlist:
yt = pytube.YouTube(url, on_progress_callback=on_progress)
streams = yt.streams.filter(progressive=True)
print_streams(streams)
itag = input("Please choose the video you want to download: ")
yt.streams.get_by_itag(int(itag)).download()
else:
pl = pytube.Playlist(url)
for video_url in pl.video_urls:
yt = pytube.YouTube(video_url, on_progress_callback=on_progress)
streams = yt.streams.filter(progressive=True)
print(f"Downloading: {yt.title}")
streams.get_highest_resolution().download()
print("\n")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment