How to download videos from YouTube with Python on Unix based machines

There are times when one needs to download videos from the famous online platform YouTube, be it a movie with episodes or material related to studies. Although many tools which can help the user to easily download videos from YouTube exist on the Internet, most of them do not offer the functionalities that meet the requirements of a heavy Python geek.

Being more correct, most of the online tools on the Internet are designed for the average user, who is not technical at all!

Having enough experience with the Python computer programming language to understand the open source tools which run on it, I will teach you guys how to make use of Youtube-dl to download videos from YouTube.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

What is Youtube-dl?

Youtube-dl is an opensource tool written in the Python computer programming language, with the main purpose of helping the coders to download videos from the internet. Not a platform specific utility, Youtube-dl needs only a Python interpreter to run. According to the official website of Youtube-dl, its codebase supports the following versions of the Python computer programming language.

  • 2.6
  • 2.7
  • 3.2 +

Although the youtube-dl utility is a command-line tool, there are also Windows builds for it which the users can easily download and setup on their machines.

For the purpose of this tutorial, we are going to download the command-line version of youtube-dl and make use of it in our machines.

Download and install youtube-dl

Based on my personal experience with the youtube-dl utility, one can install it on their machine through many different ways.

Note: The first shared in here targets only those who run Unix based machines.

The first one which I am going to share in here, is to download the source code directly from github, the site in which the youtube-dl project is being hosted.

Before one can make use of the following tool to download youtube-dl, they need to make sure that they have curl already installed on their machine.

You can easily check if curl is already part of your operating system by typing the following command.

curl

If the following output comes out after the above command gets executed in your own operating system, it means that the curl utility is already included in your machine. And for sure that you can make use of it to download files over the internet.

curl: try ‘curl –help’ or ‘curl –manual’ for more information

If the curl utility is not available by default in your own operating system, don’t worry. We will teach you another way to automatically download and install youtube-dl on your machine.

Now, make use of the curl by using the following command.

sudo curl https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl

Once the above command is successfully executed in your own operating system, the youtube-dl will be automatically stored under /usr/local/bin directory on your own unix based operating system.

Before making use of the youtube-dl, after it has been downloaded through the usage of the above command, you need to give it the right permissions.

Run the following command to give youtube-dl the right permissions so you can execute it on your system.

sudo chmod a+rx /usr/local/bin/youtube-dl

The second way, which works both on Windows and Unix based machines, is to install youtube-dl through pip.

For those of you who have no idea what the pip utility is, we recommend you read our article on it.

Once you managed to setup the pip utility on your own machine, you can make use of it to install the youtube-dl package on your machine by using the command shown below.

pip install youtube-dl

How to download videos with youtube-dl

Once you have managed to install youtube-dl on your Unix based machine by using one of the ways being shared above, you can easily make use of it to download videos from YouTube.

According to the manual of the youtube-dl project, for it to properly work, the user should follow the syntax which is being shown below.

youtube-dl [OPTIONS] URL [URL…]

Now let’s try to download a single video from YouTube by making use of the command which is shown below.

youtube-dl https://www.youtube.com/watch?v=1F_OgqRuSdI

Once the above command got executed on my own terminal console, the following output started to appear.

youtube] 1F_OgqRuSdI: Downloading webpage
[youtube] 1F_OgqRuSdI: Downloading video info webpage
[download] Destination: Lesson 1 – Python Programming (Automate the Boring Stuff with Python)-1F_OgqRuSdI.f247.webm
[download] 96.9% of 9.81MiB at 1.14MiB/s ETA 00:00

As you can see from the above output, the youtube-dl utility gives information on the size of the video, and it also offers a nice progress bar which shows the ETA and the download speed.

Not only does the youtube-dl offer the features to download a single video from YouTube, but it can also be used to download an entire playlist.

The command which can help the user to download an entire playlist by making use of youtube-dl, is the one which is being shown below.

youtube-dl –yes-playlist PLAYLIST_ID

Being an engineering student, I have to take a lot of classes online. The above feature is truly useful to me, especially when I want to download the entire playlist from a YouTube channel.

To download some tutorials on the C computer programming language, I make use of the following command.

youtube-dl –yes-playlist PLGLfVvz_LVvSaXCpKS395wbCcmsmgRea7

After the above command got executed on the terminal application of my unix based operating system, the following output came out.

download] Downloading playlist: C Video Tutorial
[youtube:playlist] playlist C Video Tutorial: Downloading 16 videos
[download] Downloading video 1 of 16
[youtube] 8Ib7nwc33uA: Downloading webpage
[youtube] 8Ib7nwc33uA: Downloading video info webpage
[download] Destination: How to Install GCC-8Ib7nwc33uA.f137.mp4
[download] 100% of 48.50MiB in 00:52
[download] Destination: How to Install GCC-8Ib7nwc33uA.f140.m4a
[download] 100% of 6.86MiB in 00:14
[ffmpeg] Merging formats into “How to Install GCC-8Ib7nwc33uA.mp4”
Deleting original file How to Install GCC-8Ib7nwc33uA.f137.mp4 (pass -k to keep)
Deleting original file How to Install GCC-8Ib7nwc33uA.f140.m4a (pass -k to keep)
[download] Downloading video 2 of 16
[youtube] nXvy5900m3M: Downloading webpage
[youtube] nXvy5900m3M: Downloading video info webpage
[download] Destination: C Video Tutorial-nXvy5900m3M.f137.mp4
[download] 100% of 53.09MiB in 02:07
[download] Destination: C Video Tutorial-nXvy5900m3M.f140.m4a
[download] 64.6% of 21.73MiB at 587.49KiB/s ETA 00:13

As you can see from the above information which is displayed by the youtube-dl utility on the terminal app, the playlist contains 16 videos.

Final thoughts

youtube-dl is very rich in features. Since the purpose of this article is to teach users how to make use of it to download videos from Youtube, we are going to cover the other features in details in future articles.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

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

*