Muhimen
Posted on June 29, 2020
So, you are on the desert with no WiFi connection but you must continue watching your favorite series on youtube. A simple solution for this will be to download the videos beforehand. While there are many YouTube video downloaders available on the net, you will surely not want to download malware with your favorite videos, will you? Therefore the preferable solution is to make your very own downloader!!
It always surprises me how easy things can be with Python. To make your own youtube video downloader you will only need 2 lines of codes(yes, you heard it right)!
And here it is. Isn't it beautiful?
from pytube import YouTube
yt = YouTube('https://www.youtube.com/watch?v=sVPYIRF9RCQ').streams[0].download()
Don't forget to run this command before you execute the code.
pip install pytube3 --upgrade
Explanation
The First line is importing the YouTube
object from the pytube
library. Then in the second line, you specify the video by adding the video URL. Then, select the first available video available streams[0]
and finally download()
it!
Isn't that simple?
Further improvements
Though those 2 lines of code will do the job for you, there is still scope for improvements.
Some slight issues
Downloading a video from the above code can create the following issues.
- No indication of how much download has been completed
- Not getting the highest quality available
- A fixed download path
Fixing the issues
Let's handle the issues one by one
1. Indication download length
We will show a small progress bar indicating the amount of video got downloaded. It's is quite simple. You just need to import one extra module.
from pytube import YouTube
from pytube.cli import on_progress
url = 'https://www.youtube.com/watch?v=sVPYIRF9RCQ'
yt = YouTube(url, on_progress_callback=on_progress)
yt = yt.streams[0].download()
Let's talk about the changes. On the second line, we are importing the progress bar. And on the fourth line, we are mention what to do when video downloads. by on_progress_callback=on_progress
, we say the program to show a progress bar while the video is downloading.
-> |██████████████████████████████████████████████████ | 97.0%
The progress bar will look something like this.
Downloading hight quality video
For this, we will need to sort the list of streams. We will need to use the built-in order_by
method to sort the streams. Take a look at the following code.
from pytube import YouTube
url = 'https://www.youtube.com/watch?v=sVPYIRF9RCQ'
yt = YouTube(url)
yt.streams.order_by('resolution').desc()
yt = yt.streams[0].download()
You can order the streams by itags and FPS too.
Including the download path
To download the video at a specific path, just add the file path as an argument in download()
just like this.
from pytube import YouTube
url = 'https://www.youtube.com/watch?v=sVPYIRF9RCQ'
yt = YouTube(url)
yt = yt.streams[0].download('/video')
You can add a full path too.
Final code
from pytube import YouTube
from pytube.cli import on_progress
url = 'https://www.youtube.com/watch?v=sVPYIRF9RCQ'
yt = YouTube(url, on_progress_callback=on_progress)
yt.streams.order_by('resolution').desc()
download_path = '/video'
yt = yt.streams[0].download(download_path)
Conclusion
So, that's it for now. If you are in a hurry and need to download a few videos, just refer to the final code. However, if you want to explore more then you can read the official documentation. I don't want to limit your imagination, but here are a few things you can try.
- Make a web app
- Make a GUI
- Add advance filtering options
I hope this post was somewhat helpful to you. And this is all I had to offer to you this time. Until next time, happy coding for you. 😊
Posted on June 29, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.