Introduction
We will go over a Python YouTube Downloader – Audio, Playlist, High Res Video.
Why use browser extensions and paid tools when you can see the full code to download your YouTube files for free?
Today I’m going to give you everything you need to do this at your computer without risking installing anything weird. It will be a clean, fast and easy solution to get going with no ads or things that slow down your computer.
We will break down this in the following sections:
- How To Download A High Resolution YouTube Video
- How To Download A List Of YouTube Videos From A Text File
- How To Download Audio MP3 Only From YouTube List
- How To Download A YouTube Playlist
I will be providing full source code and an executable file for you to use to download the YouTube data you need.
Please keep in mind you should only use this for your own files or royalty free YouTube Videos.
I have used this successfully in various projects and it works very well and has saved me a ton of time just downloading the data in the background while I do other things in my computer.
We will go point by point on getting you up and running in less than 5mins, you do not need to have any programming knowledge to run this tool.
This is a complete guide and should cover all your questions on using Python to download YouTube files.
All code and examples on how to do this can be found in the Github link here.
How To Setup Environment To Run PyTube
We will quickly go over how to setup your environment to get going with PyTube.
The fastest route if you want to get started is to install the pip python dependencies using the command shown below.
$ pip install pytube python-dotenv Successfully installed python-dotenv-0.20.0 pytube-12.0
The more elaborate way to do it is to use my script which includes also a fix for version 12.0 of Pytube that is failing with the latest google updates that were recently made to YouTube.
If you are encountering the following error:
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple
Then you need to use the script that I have written that works in both Linux and OSX. If you are on windows simply change the copy command or copy the file manually. If you can’t figure it out and you are using Windows drop me a line below and I’ll help you out.
The script we will be using to perform this is the following:
#!/bin/bash echo "Cleaning up old directories" rm -rf venv pytube export ROOT_DIR=`pwd` echo "Setting up virtual environment" virtualenv venv . venv/bin/activate echo "Installing python requirements" $ROOT_DIR/venv/bin/pip install -r requirements.txt echo "Cloning Pytube" git clone https://github.com/pytube/pytube echo "Patching cipher.py to work with latest youtube" cp cipher.py ./pytube/pytube/cipher.py cd pytube echo "Building Pytube" $ROOT_DIR/venv/bin/python setup.py build $ROOT_DIR/venv/bin/python setup.py install
If you were to go ahead and execute it you’d get the following output.
$ ./install.sh Cleaning up old directories Setting up virtual environment created virtual environment CPython3.9.12.final.0-64 in 202ms ... Installing collected packages: python-dotenv Successfully installed python-dotenv-0.20.0 Cloning Pytube ... Resolving deltas: 100% (4500/4500), done. Patching cipher.py to work with latest youtube Building Pytube 12.0.0 .... Copying pytube-12.0.0-py3.9.egg to /Users/alex/code/unbiased/python-youtube-downloader/venv/lib/python3.9/site-packages Adding pytube 12.0.0 to easy-install.pth file Installing pytube script to /Users/alex/code/unbiased/python-youtube-downloader/venv/bin Installed /Users/alex/code/unbiased/python-youtube-downloader/venv/lib/python3.9/site-packages/pytube-12.0.0-py3.9.egg Processing dependencies for pytube==12.0.0 Finished processing dependencies for pytube==12.0.0 $ source venv/bin/activate $ ipython In [1]: import pytube In [2]: pytube.__version__ Out[2]: '12.0.0'
The script above automates the following tasks for you so you don’t need to do the manually:
- Sets up and creates a virtual environment to install the python pip packages
- Activates automatically the Python environment that was created
- Installs dotenv Python dependency using the requirements.txt file which is included in the Git repo here.
- Gets the latest version of Pytube from GitHub and patches the cipher.py which has the issue mentioned earlier
- Sets up and builds the patched Pytube code
- Tests to ensure Pytube and the dependencies were successfully installed.
If you want you can follow every step above manually too but if you are like me you can skip the process and just run the script as show above.
How To Download A High Resolution YouTube Video
The first example of code we are going to write is to download a high resolution YouTube Video. The location of the video can be defined in our environment file .env a sample of which has been provided in the Github repo. For your quick reference I’m including an example below.
YOUTUBE_VIDEO=https://www.youtube.com/watch?v=SOMEVIDEOHERE YOUTUBE_PLAYLIST=https://www.youtube.com/watch?v=SOMEPLAYLISTHERE
We have defined two environment variables one is the Youtube video we will be using here as an example to download the high resolution and the other one is a YouTube playlist which we will demonstrate below on how we can download everything from.
It’s important to note here that you need to replace the urls with real ones that you control or they are royalty free or you have permission to get from the authors.
Now that we have our example Youtube video and playlists defined we can go ahead and examine the code that downloads a high resolution version of the YouTube video you specify in the environment file.
import os from pytube import YouTube from dotenv import load_dotenv load_dotenv() video_location = os.getenv('YOUTUBE_VIDEO') yt_obj = YouTube(video_location) yt_stream = yt_obj.streams.get_highest_resolution() print (f'Found stream:\nNAME: {yt_stream.title}\nRES: {yt_stream.resolution}\nFPS: {yt_stream.fps}\nTYPE: {yt_stream.subtype}') print ('Starting to download, please wait...') r = yt_stream.download() print ('File saved: ', r)
The code above downloads automatically for us the file and finds also which version of the YouTube video has the highest resolution.
The steps that are followed by the code are:
- We load our environment variables into memory to make them available for Python
- We read the YouTube location from the environment variable
- We then acquire an object from the YouTube location such as the metadata etc
- From that we examine all the available YouTube formats of the stream and find the highest resolution of it
- We print out some of the metadata and then download the stream into disk
- This is saved in the running folder
If we were to execute this for the example we had we will see the following output:
$ python ./download_youtube_highres_video.py Found stream: NAME: COSTA RICA IN 4K 60fps HDR (ULTRA HD) RES: 720p FPS: 30 TYPE: mp4 Starting to download, please wait... File saved: /Users/alex/code/unbiased/python-youtube-downloader/COSTA RICA IN 4K 60fps HDR (ULTRA HD).mp4
As you can see the file is successfully saved in the home directory of where the code was executed from in mp4 format. The video above was downloaded in 720p/30fps resolution.
And to verify everything works we try to play the video locally to ensure that it works as it can be seen the video plays normally without issues.
How To Download Audio MP3/MP4 Only From YouTube List
The next step we will demonstrate is how we can download only the audio from a particular YouTube file. This is a very common situation as you may only want sound from the files you download and not the video portion of it.
The code below works in a similar way as above but in this case it filters out only the audio streams and from those it simply picks the first instance to download.
import os import pprint from pytube import YouTube from dotenv import load_dotenv load_dotenv() video_location = os.getenv('YOUTUBE_VIDEO') yt_obj = YouTube(video_location) yt_stream = yt_obj.streams.filter(only_audio=True, mime_type='audio/mp4') if yt_stream: yt_stream = yt_stream[0] print (f'Found stream:\nNAME: {yt_stream.title}\nBITRATE: {yt_stream.abr}\nTYPE: {yt_stream.subtype}') print ('Starting to download, please wait...') r = yt_stream.download() print ('File saved: ', r)
To break down this code the steps it follows are:
- Similar as the download high resolution file steps but in this case instead of picking the highest resolution stream we add a filter
- The filter ensures the mime type is of type audio and mp4 (you can adjust to download other audio formats too if you remove that but I prefer to have my audio files in mp3/mp4)
- Then we print out some audio file meta data such as the bitrte and codec
- Finally it downloads the audio file and saves it in the current executing folder
If we were to execute this we will see the following output:
$ python ./download_youtube_audio.py Found stream: NAME: COSTA RICA IN 4K 60fps HDR (ULTRA HD) BITRATE: 48kbps TYPE: mp4 Starting to download, please wait... File saved: /Users/alex/code/unbiased/python-youtube-downloader/COSTA RICA IN 4K 60fps HDR (ULTRA HD).mp4
As you can see we are using the same video file as previously but in this case we are only looking for audio stream from it since we do not care for the video file. In the case above the code picked from the audio list of streams the first one which is 48kbps. Do note if we iterated all of them we can also find a higher bitrate of 128kbps but for simplicity of the code we choose always the first entry.
If you would like to adjust the loop above to pick the best one you can do so by examining the bitrate field of the metadata of the Youtube stream object.
To ensure this finally works without issues we simply try to play our audio file.
As shown above the audio file was successfully downloaded and it plays the audio track that it found.
How To Download A List Of YouTube Videos From A Text File
One thing you may want to do is download a list of YouTube files from a text file. To do this in Python it’s fairly easy. You can simply modify the code above (if you want audio use the audio downloader, if you want video the video downloader). What you need to do is something like this:
for youtube_url in open('youtube_list.txt', 'r').readlines(): ....
The code above simply goes line by line for every URL you want to download. So adding this as a high level wrapper to the code will let you download everything in the file you selected.
If you need help to adjust the code I’d be happy to help you out. You can drop me a line below in the comments or in my twitter profile.
How To Download A YouTube Playlist
The last thing I want to demonstrate here is how to download a YouTube playlist. This is a popular request that I keep getting asked about so I decided to add it here to help you out.
One of the coolest features of Pytube is that it has a Playlist parser built into their SDK of the library. So in order to implement this below we will be making use of it. Please note the code below uses the playlist url you specified earlier in your environment file.
import os from pytube import Playlist from dotenv import load_dotenv load_dotenv() playlist_location = os.getenv('YOUTUBE_PLAYLIST') playlist_obj = Playlist(playlist_location) for video in playlist_obj.videos: r = video.streams.first().download() print('Downloaded: ', r)
The steps the code above follows are:
- Loads the environment variables into the python code
- Reads the YouTube playlist url to use
- Initializes a playlist pytube object with the playlist YouTube location url
- Iterates through all the videos in the playlist one by one using the videos variable of the playlist object
- Finds the streams available for every video
- In this case for simplicity we take the first one and download it.
- If you would like to get the highest resolution or audio only stream you can re-use the code above from the previous two downloaders we demonstrated.
- Note all files are saved like earlier in the current executing directory.
So now we will go ahead and execute it and see what the output is. In the case below I simply picked a Queens playlist of files that I later download as I already own all of their originals (Yes I’m a Queens fan).
$ python ./download_youtube_playlist.py Downloaded: /Users/alex/code/unbiased/python-youtube-downloader/Queen - We Will Rock You (Official Video).3gpp Downloaded: /Users/alex/code/unbiased/python-youtube-downloader/Queen - We Are The Champions (Official Video).3gpp Downloaded: /Users/alex/code/unbiased/python-youtube-downloader/Queen - I Want It All (Official Video).3gpp Downloaded: /Users/alex/code/unbiased/python-youtube-downloader/Queen – Bohemian Rhapsody (Official Video Remastered).3gpp ...
As you can see the code is picking the first audio from each playlist YouTube stream. It happens that the first one in all the cases is of 3gpp format which you can easily change if you apply a filter as we showed earlier to download the mp3/mp4 format. If you need help with that I can show you how to adjust the code to do it.
And to test it out we will randomly open up one of the files we just downloaded.
As you can see above it’s showing the video file playing from Queens so the code was successful.
Conclusion
We were able to successfully go over Python YouTube Downloader – Audio, Playlist, High Res Video, hopefully I answered any questions you may have had and helped you get started on your quest of downloading YouTube videos.
If you found this useful and you think it may have helped you please drop me a cheer below I would appreciate it.
If you have any questions, comments please post them below or send me a note on my twitter. I check periodically and try to answer them in the priority they come in. Also if you have any corrections please do let me know and I’ll update the article with new updates or mistakes I did.
What do you currently use to download YouTube videos?
I personally like to use my code because I trust it, it’s clean, open source and freely available to everyone with no strings attached to it.
If you would like to visit the official PyTube documentation here.
If you would like to find more articles related to automating computer tasks with Python you can check the list below: