Search
Close this search box.

Download YouTube video with Python

Table of Contents

1. Introduction

Sometimes you like video on YouTube so much that you want to keep it on your computer. This is good because every video can disappear if author wants and if YouTube will delete his account (see article about it). In this tutorial I will guide you how to do it using Python language.

2. Package installation

2.1. Install anaconda

To make it simple I used anaconda package manager. Below commands to install required dependencies. Check your environment and modify URL to point to your system.

 
				
					curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
sh Miniconda3-latest-MacOSX-arm64.sh
				
			

2.2. Install pytube

 
				
					conda install pytube
				
			

3. Run script and get video

				
					cat<<EOF >yt_download.py
from pytube import YouTube
import argparse

parser = argparse.ArgumentParser(description="Downloading movie from YouTube")
parser.add_argument("--videourl", type=str, help="Provide URL from YouTube", default="https://youtu.be/5vBa7AwfslE?si=0PysW5CxjH3BLFpT")

args = parser.parse_args()
url = args.videourl

yt = YouTube(url)
yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download()
EOF
				
			

You can run script without any arguments so it will download sample video

				
					conda run python yt_download.py
				
			

or specify URL to get your favorite one

				
					conda run python yt_download.py --videourl "https://youtu.be/cpYodgzclqo?si=lTJWhzJ7QZCQQPKN"
				
			

4. Summary

In this tutorial you have learned how to create python script and use it to download video from YouTube.

Have a nice coding!

Follow me on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *

Share the Post:

Enjoy Free Useful Amazing Content

Related Posts