You are on page 1of 6

Forum

Donate

Learn to code — free 3,000-hour curriculum

NOVEMBER 14, 2022 / #PYTHON

How to Build a Python


Program to Download
YouTube Videos
David Fagbuyiro

YouTube is a well-known internet video streaming


service. There are millions of videos in categories
such as education, entertainment, and travel.

You can quickly watch videos with a few mouse clicks, but
downloading videos is difficult. But in a recent upgrade,
YouTube now allows you to save videos in its download folder
for offline viewing. Still, you are unable to save them locally.

In this tutorial, you will learn how to use Python code to


download YouTube videos. As you may know, one of Python's
great strengths is its huge number of modules and libraries. We
will write the Python script using the popular pytube package.

Prerequisites
Below are the basic requirements to proceed with this tutorial:
Forum
Donate

Understanding oftothe
Learn Python
code — freeProgramming language
3,000-hour curriculum

You must have Python 3+ installed on your computer

You must have installed the Python library Pytube

You should have a Python code editor such as Pycharm,


Vscode, and so on.

Pytube Overview and Installation


Pytube is a small, dependency-free Python module for accessing
videos from the internet.

The native library is not pytube – you must first install it to be


able to use it. When you have pip, installation is simple.

To install Pytube using pip, you will need to open your command
prompt CLI as an administrator and enter the following
command:

pip install pytube

The pytube library improves video downloads. Build the


YouTube module's object by supplying the URL as a parameter.
Then, obtain the video's proper extension and resolution. You
can change the name of the file at your leisure – otherwise, the
original name will be retained.
Now let's get to the main aspect of writing and implementing
Forum
Donate
the code to download our favorite videos from YouTube.
Learn to code — free 3,000-hour curriculum

from pytube import YouTube

def Download(link):
youtubeObject = YouTube(link)
youtubeObject = youtubeObject.streams.get_highest_resolution()
try:
youtubeObject.download()
except:
print("An error has occurred")
print("Download is completed successfully")

link = input("Enter the YouTube video URL: ")


Download(link)

You use the from pytube import YouTube function to import


the Python Pytube library before continuing with the other
aspects. Then you define the function download link.

The youtubeObject =
youtubeObject.streams.get_highest_resolution() command
will automatically download the highest resolution available.

Then I implemented the Try and Except to return an error


message if the download fails – else it will print out that the
download is completed successfully.

The Link function will ask for the preferred YouTube video link
to download, then immediately after you hit the enter button,
the video downloading will commence.

The Output:
Forum
Donate

Learn to code — free 3,000-hour curriculum

The video I downloaded was successful. You can see the video in
the same Python folder where the file you are working on is
located. But if you wish, you can then move the video to your
preferred storage location. In my case the video name is
"Ronaldo celebrates with Antony.mp4."

However, it would be preferable if you had a reliable internet


connection.

This library also has numerous sophisticated features, but we


have covered all of the major ones. You can learn more about
the pytube library by visiting its official well-written
documentation.

Conclusion
We have successfully built a YouTube video downloader script
of our own in Python. This helps you avoid the stress of looking
for an external website or application to get your preferred
video to your local storage.
Forum
Donate
It also saves you from having to expose your data on a third-
Learn to code — free 3,000-hour curriculum
party website or phishing link – all in the name of getting a video
from YouTube to your local storage.

Hopefully after going through this article, you will understand


the process required to download videos from YouTube without
the need to download an external application or visit any
website you don't trust.

David Fagbuyiro
Software engineer, Technical writer, and Web 3.0 enthusiasts

If you read this far, tweet to the author to show them you care.
Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum


has helped more than 40,000 people get jobs as developers.
Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit


organization (United States Federal Tax Identification Number: 82-
0779546)

Our mission: to help people learn to code for free. We accomplish this by
creating thousands of videos, articles, and interactive coding lessons - all
freely available to the public. We also have thousands of freeCodeCamp Forum
Donate
study groups around the world.
Donations to freeCodeCamp go to
Learn toward
codeour education
— free initiatives,
3,000-hour and help
curriculum
pay for servers, services, and staff.

You can make a tax-deductible donation here.

Trending Guides

Best Python IDEs Python Index

Git List Branches What is HTML?

Java Enum Example What is UTF-8?

Checkmark in HTML SQL Inner Join

What is an iframe? What is ANAME?

Python List Length HTML Dot Symbol

Python String to Int indexOf in Java

Int to String in C++ Image File Types

CSS Margin vs Padding SOLID Principles

HTML Starter Template How to Flush DNS

Rubber Duck Debugging DNS Server Not Responding

SQL vs NoSQL Databases Module Exports in Node.js

Python For Loop Example Computer Skills for Résumé

Int Object Not Iterable Force Delete a Windows File

How to Type Vertical Bar Inspect an Element in Chrome

Our Nonprofit

About Alumni Network Open Source Shop Support Sponsors Academic Honesty

Code of Conduct Privacy Policy Terms of Service Copyright Policy

You might also like