Download YouTube videos By Python in 5 lines !!!

Howdy, today we’ll create a program by python in 5 line can download YouTube videos, so let’s get started.

Pre-requisites

Let’s Begin

We’ll create two files, &

This file will download your video with pictures & audio

And This’ll download the video only with audio.

Open your favorite editor, and type

$ touch video.py audio.py

We’re going to install two packages

&

so in terminal

$ pip install pafy youtube.dl

Little remains, in we'll import

from pafy import new

is a function download your video by add the URL on it, so create variable and it's well be input

url = input("Enter the url: ")

now create variable and it's value is function

video = new(url)

the video has different quality, we want the best quality, define new variable

dl = video.getbest()
dl.download()

you can test it

$ py video.py

now, go to , it's like but has some differences

from pafy import new
url = input("Enter the link: ")
video = new(url)

let’s define variable

audio = video.audiostreams
audio[0].download()

that’s it, you can now go to YouTube and download your favorite video/s.

good bye.

source code

Founder of secman-team. full-stack dev. enjoys working on cli apps, websites, automation (CI/CD), and databases.