YouTube is a rich source of videos and downloading videos from YouTube is a little difficult. There are some extensions and downloaders available but those are sometimes not recommended. But in Python, it is quite easy to download video from YouTube.
So, in this blog we will learn how to download videos from YouTube directly using video link.
- To download videos from YouTube, we will use a python library named ‘pytube’. First you need to install ‘pytube’ using following command.
1 |
pip install pytube |
- Now we are having all necessary libraries required for this task. Now import the required module from ‘pytube’ library.
1 |
from pytube import YouTube |
- The only thing that you will need from video is it’s video link.
1 |
video_link = 'https://www.youtube.com/watch?v=6FGVFYaz_S0' |
- Then create an object of the earlier imported module ‘YouTube’ using this video link.
1 |
vid = YouTube(video_link) |
- After getting object we need to get stream and from that get the first result of the stream which will be our required video.
1 |
stream = vid.streams.first() |
- Now we have our video stream, to download it just call the following function.
1 |
stream.download() |
The above code will download the video in the current working directory with the name of the video as it was in the YouTube. To download it to a specific folder with specific name you need pass two arguments in the above code as below.
1 |
stream.download(output_path='D:/Downloads', filename='test') |
That was a simple code to download videos from YouTube. Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.