Download any YouTube Video with Python

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

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

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.

Yujian Tang
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Leave a Reply

%d bloggers like this: