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. 😊
Top comments (7)
Hi 😄, Nice. But isn't it illegal to download YouTube videos ?
Before publishing this post, I did some searching. It is not illegal to download videos. However, it's only for personal use ONLY. If you are selling the software, that's illegal(or immoral).
Thanks, but I am not able to see that on below terms:
youtube.com/static?template=terms
The following restrictions apply to your use of the Service. You are not allowed to:
access, reproduce, download, ....
There is a mixed bag of information on this topic. Some people say it's legal.
And some people say it is not.
If I prefer the ToS then YouTube itself is doing illegal stuff as it offers to make videos offline.
If you ask me is it legal or not, I will say as long as you are not establishing the contents as your own and giving the content creators the proper value, it's not that harmful.
Also, there is no past history of google suing developers for downloading videos(at least I haven't seen one).
Great good job
Thanks for the wonderful post.
Python never ceases to surprise me. in just few lines with a very easy to understand syntax you can do a lot.
I agree.