Download YouTube with `pytube`

j3ffyang
Dec 11, 2023

--

If you love Python and want to write your own code, here’s a good and simple practice when you download a video from YouTube. Use your own Python code, instead of installing plugin in your browser.

TL;DR

# import pytube
from pytube import YouTube
from pytube.cli import on_progress

# define download function and download path
def download_video(url):
"""Download"""
yt = YouTube(url, on_progress_callback=on_progress) # the interface
# yt.streams.first().download() # get the very first result
# get the highest resolution
yt.streams.get_highest_resolution().download("/FILE/PATH/")


def main():
"""Main"""
url = input("Enter the url of the video to download: ")
download_video(url)


if __name__ == "__main__":
main()

Edit: add on_progress_callback=on_progress to show up the progress bar during download.

--

--

j3ffyang

ardent linux user, opensource, kubernetes containerization, blockchain, data security. handler of @analyticsource and @j3ffyang