YouTube Video Downloader(Python)

Hasitha Chandula
Analytics Vidhya
Published in
2 min readJan 18, 2020

In this tutorial, I’m going to show you build your own Youtube video downloader using python (cmd app). And this is a very fun project.

To start with this project first you need to install python3 in your computer and also a text editor or IDE ( vs code, pycharm ). To build this app we use pytube library. you can check this library here. To install this library open your cmd and type and press pip install pytube. And also we are using virtualenv here so we need to install that too. pip inatall virtualenv

Now Environment setup is done. Now coding part. As an IDE I’m using vs code. Now go to your project folder and open cmd and type virtualenv venv. It will create a virtual environment to build our application. And we need to activate our virtual env. if your computer Linux or mac you can activate by typing source ./venv/bin/activate and if you are a windows user you can activate source ./venv/scripts/activate and press Enter.

Now open your project using vs code and create index.py file. And code look like this.

from pytube import YouTube# Youtube video link you need to download
yt = YouTube("https://www.youtube.com/watch?v=anq3D5XFFew")
# Display all formats this video available
print(yt.streams.all())

To run the file in cmd type and Press Enter python index.py. If you are seeing something like bellow you are good.

result

But sometimes it gives errors. In that error, there is a line mixins.py copy that path and open that file. we need to modify that file. Delete all code lines in that file and paste bellow lines of codes and save.

Code is here.

And run again now all errors are gone.

As a result, you can see the itag in every Stream. It means the quality of the video. using this itag we can download videos.

from pytube import YouTubeyt = YouTube("https://www.youtube.com/watch?v=anq3D5XFFew") 
# Youtube video link you need to download
# Display all formats this video availableprint(yt.streams.all())# Download Video
yt.streams.get_by_itag(22).download()

Now run the file again. now you can see video file in your project directly.

Now you can modify this as you want. As an example, if we want to download more than one file we can use a list and for loop, code looks like this.

from pytube import YouTube# List of video links
video_list = ["https://www.youtube.com/watch?v=XjKGJf2X-U8&list=TLPQMTcwMTIwMjBHNRzgUhODbQ&index=1","https://www.youtube.com/watch?v=Dq-SpbVX8gc&list=TLPQMTcwMTIwMjBHNRzgUhODbQ&index=2","https://www.youtube.com/watch?v=anq3D5XFFew"]
for link in video_list:
yt = YouTube(link)
yt.streams.get_by_itag(22).download()
print(yt.streams.get_by_itag(22))

That's it. I hope you enjoy this small project.

Thank you and Enjoy Python.

--

--

Hasitha Chandula
Analytics Vidhya

Senior Full Stack Engineer specializing in TypeScript & Go. I create scalable web apps, love learning new tech, and thrive on teamwork.