Over 100,000 people search for ways to download YouTube videos every month. Here’s how you can download a YouTube video with Python, for free. In this post we’re going to cover how to use the youtube_dl
library to download YouTube videos with Python. We’re going to first learn about what the youtube_dl
library is, and then build a Python program that will download YouTube videos.
In this post we will cover:
- What is the
youtube_dl
Library? - How to Download YouTube Videos with the
youtube_dl
Library
What is the youtube_dl
Library?
The youtube_dl
library is an open-source command line tool that can download YouTube videos. It is a command line tool, meaning you can run it from your terminal. In addition to having a command line interface, it also has a Python library. The Python library allows you to execute the commands normally executed in the command line with Python. The advantage of using it in Python is having a more customizable interface and compacting other options into one command.
To follow this tutorial, you’ll need to install the Python library for youtube_dl
. You can do so by using the line in the terminal below.
pip install youtube_dl
Learn more at yt-dl.org.
Downloading a YouTube Video with the youtube_dl
Library
As always, we’ll begin our program by importing libraries. In this case, the only library we need is the youtube_dl
library. Let’s create a function that will download a YouTube video from a link. The only parameter it will take is the link itself. We’ll start our function by defining some options for youtube_dl
. We’ll tell it that we want our function in mp4
format and also to save it as the id
of the video.
Next, we’ll strip the link to get the YouTube video ID. We’ll use the YoutubeDL
object to extract the info from the video including the metadata. Then we’ll print out and return the save location. The extract_info
function does the downloading part. Running the download_video
function on a link will save the video as a .mp4
file in your current directory.
import youtube_dl
def download_video(link):
ydl_opts = {
'format': 'mp4',
'outtmpl': "./%(id)s.%(ext)s",
}
_id = link.strip()
meta = youtube_dl.YoutubeDL(ydl_opts).extract_info(_id)
save_location = meta['id'] + ".mp4"
print(save_location)
return save_location
Further Reading
- Matrix Multiplication in Basic Python
- Multiprocessing Python Functions with Dependencies
- Build an AI Content Moderation System
- Python High Low Guessing Game
- Python Web Scraping with Selenium and Beautiful Soup 4
I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
DonateDonate monthlyDonate yearly