Have you ever felt the need to download the entirety of the videos published by someone on YouTube? Whether that is because you fear a channel will get deleted or that you want to save its content to view it offline or that you want to make sure you save all of your channels’ videos before starting from a blank slate, there are multiple reasons why one would want to archive the content of an entire channel.
This project is more about Technology than Marketing but I know many of you are into Python and Automation and so I thought this could be of interest to you.
In this article, I am going to show you how to use Python and two different libraries in order to download Youtube videos locally at the best quality possible, how to download entire playlists and channels, how to easily download videos and more.
If you are just interested in the script to get an entire channel, feel free to scroll down to the corresponding part of this article using the Table of Content. However, I highly suggest you follow the previous parts of this write up as they explain exactly how we got to the last point and will help you understand the whole process in case you want to adjust some parts of the script’s behaviour.
Without further ado, let’s get into it!
The first thing we are going to need is to install the two libraries that we will use throughout this project.
The first one is called pytube and is available with pip. It’s a simple interface that allows us to retrieve information from Youtube videos feeds and download those videos. It supports thumbnail download, supports caption track downloading as .srt files and many more features. For the purpose of this exercise, we will only focus on how to download videos.
The second library we are going to need is called FFmpeg and is also available with pip. If you are familiar with the eponym command-line tool, it is a simple interface between the actual tool and the Python language.
Once you have installed these two Python libraries, you also need to download the actual FFmpeg tool. Just head on to their downloads page and pick the software appropriate for your operating system.
Now we can really get started.
The fastest way to download a Youtube video with Python
You can download a single video with Python pretty easily and quickly. The only downside of this method is that it may not be in the best quality available, but if you are looking for an easy way to export a playlist, then this is the code you should use:
When being run, these two lines will download the video specified in the link between the parentheses in the folder where the script is located.
There is one downside to this method. The biggest resolution we can achieve by following this method is going to be mostly 360p, sometimes 720p but not more.
If that is enough for you, then go ahead. If you are like me, you are going to want to download your videos at the highest resolution available.
Download a Youtube Video with Python – Highest Resolution Available
The reason why the method presented above caps at a certain resolution is because Youtube has two ways of distributing videos over their platform:
- Progressive Streaming
- Dynamic Adaptive Streaming over HTTP (DASH)
The second one is what we are aiming to download but to achieve this, Youtube distributes two different feeds for each video: an audio feed and a video feed.
The challenge here is to download the video and the audio feed separately, each having their own quality options and merge the two feeds into one .mp4 file that your video player will be able to easily decode.
This is the code we use to download a video feed at the highest quality available:
This will create a .webm file where your script is located that only consists of a video. Next up on the list, downloading the audio feed of the Youtube video. The same way we chose the best video quality available, we are going to choose the audio feed with the bigger bitrate available with this command:
This will create a .webm file where your script is located with a filename prefix “audio_” so that you can differentiate.
Storing the results of the download in a variable is as important as doing the actual command since we are going to need the information of these variables in order to merge the two feeds into one unique .mp4 file that any Media Player worth anything will be able to read.
As you can see, the first two lines of code are making use of the FFmpeg library. The goal here is to load the two separate streams into FFmpeg in order to then concatenate them in line 4.
You will also notice the output function that gives you the opportunity to name your export. You can also export in a different folder by writing the full path in this string value.
Upon running this command, your computer will start using the FFmpeg library in the background in order to realize this task. This method is going to be heavy on your CPU and every video can take a bit of time before being built.
Overall though, this is the most efficient way I know of to locally download in the best quality available any Youtube video.
Download Youtube Video at Chosen Resolution with Python
In this section, I want to show you how you can set a resolution limit on the video stream download. I use this method when I do not need the highest video quality possible when downloading a channel. 720p is plenty of quality for some type of content.
This is the code I’m using to restrain the resolution:
By using the filter function after the streams function, you can effectively filter the results to only show you streams that are in the mentioned resolution.
After that, you can download the audio file and concatenate the two feeds as I explained in the section above.
How to Download a Public Playlist on Youtube with Python
If you someone shared with you a public playlist, the library pytube has a pretty neat class that allows you to retrieve all of the URL from it in order to give you the opportunity to download it. From then on, you can choose to loop through the list using either the “fastest way to download a Youtube video with Python” depicted above or use the slightly more complicated way also depicted above in order to tune up your download settings.
In order to retrieve this list of URLs, this is the code you need to use:
As you are going to get into Youtube channels downloads, you are going to realize that some videos are unavailable. This can be for multiple reasons: They got deleted, they became private, they are not available in your country, … In order to pally that, I included a small try/except argument that will specify in the logs which videos are unavailable and automatically skip to the next one without stopping your script from running.
As I said at the beginning of this section, this script is only to download as fast as possible. This script will not give you the best quality possible but it will get the job done. If you’re looking to download at a higher resolution, you can tweak the try argument with the code I shared above in this article.
How to Download a Youtube Channel’s Videos with Python
Now, to the reason I started to write this article. I will share with you the script I am using in order to download entire channels at once.
The only thing you will need is to create a csv file called “youtube_export_history.csv” where you intend to save your script. After that, you can edit the bottom part of the script with the link of the youtube channel you’re interested in, the folder you want to save your downloads in and it will automatically download all of the videos.
In the event that the script stops or if your internet connection drops, don’t panic. The beauty of the CSV file makes it that the script will remember every single video it has downloaded before.
Without further ado, here is the code:
What we are interested in here are the last three lines of code. this is where you are going to set the three arguments this script is going to need in order to work properly.
The first thing you want to set is the channel_link variable. In order to work, this script requires you to go to the Youtube channel you want do download and go in the “Videos” tab. This link is the one needed here.
The second argument is the folder path. Just copy and paste the full folder path you’re interested in storing your videos in WITHOUT the last slash. I wrote the script on Mac OS so I hope that is not going to cause any kind of trouble if you’re using another OS. If it does, don’t hesitate to let me know so I can look for a fix in Windows.
Finally, the last argument required if the maximum resolution you want do download at.
As a disclaimer, do not put crazy resolutions in there such as 1080p or 2160p. If you set a resolution higher than the lowest quality that a video has on a Youtube channel you are interested in, this might cause the script to entirely skip the mentioned videos. To be on the safe side, you want to use this argument when you want to cap the downloads to 720p or 480p.
And that is pretty much it, you are then free to launch the .py file and watch as the video unfolds themselves. Good luck stacking those hard drives, and don’t hesitate to join us on r/DataHoarder if you’re looking to share or read about tips and tricks form other fellow content hoarders around the world.
I hope this helped in any way. If you found this article helpful, I’d really appreciate if you could share this URL with your team or your friends. That would mean a lot to me. If you’re interested in more tutorials like this one, you can also add this website to your RSS reader or join my newsletter which I use once a week maximum to keep you up-to-date on my latest articles and trends I uncovered.
Thank you for your time,