We are going to learn how to download youtube videos using python module.
First of all you need to install the youtube-dl module. For installation follow the steps and commands.
Note: Windows user use pip instead of pip3.
~$ pip3 install youtube-dl
Collecting youtube-dl
Downloading youtube_dl-2020.6.16.1-py2.py3-none-any.whl (1.8 MB)
|████████████████████████████████| 1.8 MB 273 kB/s
Installing collected packages: youtube-dl
Successfully installed youtube-dl-2020.6.16.1
After installation you need a single command to download video, copy the link of video form the browser. As shown below paste the link in command prompt or terminal followed by youtube-dl.
~$ youtube-dl https://www.youtube.com/watch?v=jppi2eUAq2k
[youtube] jppi2eUAq2w: Downloading webpage
[download] Destination: Stay Home And Sing With Me-jppi2eUAq2w.mp4
[download] 0.4% of 758.49MiB at 241.61KiB/s ETA 53:20^Z
Downloading started.
You can also create a python script to download multiple videos at once.
For that create a python file (eg. download_video.py) and paste the below script or code into the file.
import youtube_dl
def download():
video_url = input("Enter URL: ")
video_info = youtube_dl.YoutubeDL().extract_info(
url=video_url, download = False
)
file = f"{video_info['title']}.mp3"
options={
'format':'bestvideo+bestaudio',
'writethumbnail':'writethumbnail',
'writesubtitles':'writesubtitles',
'writedescription':'writedescription'
}
with youtube_dl.YoutubeDL(options) as ydl:
ydl.download([video_info['webpage_url']])
if __name__ == '__main__':
download()
With the help of this you can also download entire playlists, metadata, thumbnails, subtitles, annotations, descriptions, audio, and many more more easily.
I hope you successfully download the video if you face any problem plz let me know in the comment section.
Thanks for Reading!