(This article was originally published on my blog)
Recently, I saw a Python script written by a beginner for donwloading videos from facebook. He did it to learn some web scraping and files handling. I thought it is a really cool project for beginners to do in order to get better at Python and coding in general. So I decided to do a tutorial on how to write such a code for Youtube. Also, when finishing this short article, you will feel the joy of making somehting complete, coz I know it is important 😄.
(TL;DR 😞: You can skip this and jump to see the final code in this GitHub repo)
Short and Simple
I generally tend to keep my writings simple, short, and beginner-friendly
. So I decided to skip the scraping bit and use a package to deal with YT videos. Youtube, just like any other tech giant, sure does not make it easy
to access and use their data. And in order to download a video with python, you need to access the source streaming that video. So we will be using a package called pytube3
to access videos from links and download them. So go ahead and download the package with this command and make a python file main.py
.
pip install pytube3
Then in main.py
lets import the YouTube
from pytube
package.
from pytube import YouTube
# Dont worry about the number 3 part, it is just an upgrade for the package.
Now here is the checklist we want to follow:
- input yt link and store the video.
- filter stream options and choose the desired resolution.
- Download the video.
- Polish the mess.
Link and Video stored 🔗
First things first. We need the user to input a link to the video they want to download. Then use the YouTube package to store the video in a variable. The package will throw an error if the link is not from YouTube so we will handle the error too using try & except
.
#main.py
from pytube import YouTube
link = input(" Enter video's link ...\n ")
try:
video = YouTube(str(link))
except:
print(' Something went wrong')
print(' Check if your link is valid')
# Exit the code if there is an error
exit(' Exiting app ...')
# LOL, please mind the extra spaces.
# I just dont like the text displayed in a terminal to start next to the window border 😆.
input yt link and store the video ✔️.
Now we need to let the user choose what quality they want the downloaded video to have, by index.
We will print a message with the options then use another input for the user to choose. Then store the available options in an array.
#main.py
from pytube import YouTube
link = input(" Enter video's link ...\n ")
try:
video = YouTube(str(link))
except:
print(' Something went wrong')
print(' Check if your link is valid')
# Exit the code if there is an error
exit(' Exiting app ...')
# LOL, please mind the extra spaces.
# I just dont like the text displayed in a terminal to start next to the window border 😆.
res = input(' Choose the number of the resolution you like!\n 0: 360p\n 1: 720p\n 2: 1080p\n ')
options = [ '360p', '720p', '1080p' ]
Wait! Why did we name the options this way and store them in an array?
We will now use the number entered by the user to access the index of the chosen resolution in the array and pass it to the package.
Great. Now that we know what the user wants, we will call the package to filter the streams and get us the one with the chosen resolution. To do so, we will use the video.streams.filter
method. We will pass them two params. First, the file extension of the video (we will use mp4
), and the resolution value. If the resolution is not available, we will also throw an error.
#main.py
from pytube import YouTube
link = input(" Enter video's link ...\n ")
try:
video = YouTube(str(link))
except:
print(' Something went wrong')
print(' Check if your link is valid')
# Exit the code if there is an error
exit(' Exiting app ...')
# LOL, please mind the extra spaces.
# I just dont like the text displayed in a terminal to start next to the window border 😆.
res = input(' Choose the number of the resolution you like!\n 0: 360p\n 1: 720p\n 2: 1080p\n ')
options = [ '360p', '720p', '1080p' ]
try:
chosen_stream = video.streams.filter(file_extension = 'mp4', resolution = options[int(res)])
except:
print('This resolution is not available')
exit('Exiting app ...')
You can notice that the resolution param value is options[int(res)]
. Which is the index of the value in our res
array, entered by the user previously. We also wrapped res with int()
to make sure the input was an integer not a string.
GREAT.
filter stream options and choose the desired resolution ✔️.
FINAL TOUCH
Now all that is left is to download the video from the chosen stream. To do so we will use the .download()
method. It takes only one input which is the location you want to download the video to. We will dowload it in the same directory.
Now before we use the download method, there something little to notice. If you print the chosen_stream variable print(chosen_stream) you will see that it is actually stored in an array. So we will use download on chosen_stream[0]
.
#main.py
from pytube import YouTube
link = input(" Enter video's link ...\n ")
try:
video = YouTube(str(link))
except:
print(' Something went wrong')
print(' Check if your link is valid')
# Exit the code if there is an error
exit(' Exiting app ...')
# LOL, please mind the extra spaces.
# I just dont like the text displayed in a terminal to start next to the window border 😆.
res = input(' Choose the number of the resolution you like!\n 0: 360p\n 1: 720p\n 2: 1080p\n ')
options = [ '360p', '720p', '1080p' ]
try:
chosen_stream = video.streams.filter(file_extension = 'mp4', resolution = options[int(res)])
except:
print('This resolution is not available')
exit('Exiting app ...')
download_stream = chosen_stream[0].download('./')
Download the video ✔️.
Now lets print some cool stuff to the terminal and some more clearifying messages to polish this.
# main.py
from pytube import YouTube
# print a cool PD (Python Downloader)
print('''
|=======\ |=======\
| ------ | | ---- |
| | | | | | | |
| ------ | | | | |
|=======/ | | | |
| | ---- |
| |=======/
''')
link = input(" Enter video's link ...\n ")
print('\n')
try:
video = YouTube(str(link))
except:
print(' Something went wrong')
print(' Check if your link is valid')
exit(' Exiting app ...')
res = input(' Choose the number of the resolution you like!\n 0: 360\n 1: 720\n 2: 1080\n ')
options = [ '360p', '720p', '1080p' ]
try:
chosen_stream = video.streams.filter(file_extension = 'mp4', resolution = options[int(res)])
except:
print('This resolution is not available')
exit('Exiting app ...')
print(' Now downloading ', video.title)
download_stream = chosen_stream[0].download('./')
Polish ✔️
Follow me on twitter!
Top comments (1)
what if someone wanted to automatically download a video/audio once it's added to it's favorite list on youtube ?