Python Youtube video downloader with Pytube | Only 4 line source code

  Step by Step Procedure to create Python Youtube video downloader with Pytube

Hello friends how are you, Today in this post "Python Youtube video downloader with Pytube" i am going to teach you how you can create your own Youtube video downloader using only 4 lines of code. Python has very strong collection of libraries that make it very different from other programming languages. Using these libraries we can do a very big task or program with very few lines of code.

If you are a computer science students or teacher and want to learn something interesting in programming then just go through this post. This can also be a simple and interesting project for you.

Now  i am going to explain it step by step so just go through this post to understand this completely.

If you want to understand this through video then watch this video i have explained it step by step live

Step 1: Install Python : Click here to watch a video on how to install python or Open any browser and type Download Python and click the first link you will get official website of python here you will get a Download button and after clicking on this button you will get exe of latest python version just install it into your system. 

Step 2: Install Pycharm | Create Project | Install LibraryClick here to watch a single video on How to install Pycharm | Create Project | Install Library  or To install Pycharm IDE Open any browser and type Download Pycharm and click the first link you will get official website of Pycharm  here you will get a black download button and after clicking on this button you will get an exe of Pycharm , just install it into your system. If you are facing problem to install then watch the above video i have explained step by step.

Step 3: Create Project : Open Pycharm  click on File which is in top left of screen and select New Project. Then you will get a screen. Here first you need to select directory of your project and then type a name for your project like "YoutubeDownloader" after directory name, and at last click the create button to create this project. 

Step 4: Create Python file: To create a python file for coding in your project just right click on project name "YoutubeDownloader" and select new and click on Python File , you will get popup screen in which you have to type a name like "Program" for python file and press enter, it will create a python file with name Program.py inside the project YoutubeDownloader. 

Step 5: Install library (pytube) : Now we have to install library pytube which is used to access and download video from Youtube url . To install this library just click on Terminal which will be in Left bottom of screen , you will get a console screen and in this console screen you have to type pip install pytube and then press enter. After some seconds you will get a message like library is installed successfully. Still facing problem then click here to watch  video on How to install libraries.


Step 6: Download Youtube videoHere is the complete code for downloading a video from youtube url. Here the first line is to import library pytube which is very strong library/package of python. The main purpose behind  the development of this library is to download the video from the biggest video websites YouTube.

#import library
from pytube import  YouTube
#access video from url using YouTube()
url=YouTube("https://www.youtube.com/watch?v=1DF6sx5qlQc")
#download video using downlod()
video=url.streams.first().download()
#print success message
print("video downloaded successfully")

In the second line of the above code , you have to just paste the url of Youtube video inside YouTube("url"), YouTube() function will access the video from url and in the next line it will be downloaded and stored in the current directory where your python file is stored. 

But if you want to change the storage location of downloaded video then you can pass the directory name inside download() function. for example if you want to store downloaded video in E drive then just write video=url.streams.first().download("E:/") 

The list line is optional message that you can display it to user that video is downloaded successfully. When you will run this code then you will get the video file of the given url inside your project folder and then you can play it.

Step 7: Download Audio of Youtube video: Suppose that if you want to download only audio of the video of url then it is also possible with only few changes in the above code. Here is the following code to download only audio from the video.

#import library
from pytube import  YouTube
#access video from url using YouTube()
url=YouTube("https://www.youtube.com/watch?v=1DF6sx5qlQc")
#download video using downlod()
video=url.streams.get_audio_only().download()
#print success message
print("Audio downloaded successfully")

Here in the above code you will notice that a new function get_audio_only() function is used and this function extracts the audio from the video.


GUI based Youtube Video Downloader :i have designed a GUI based Youtube video downloader . Here is the complete code for this GUI based downloader

#import library
from tkinter import *
from pytube import YouTube
#creating window
root = Tk()
#setting width and height of window
root.geometry('500x250')
#title for window
root.title("krazyprogrammers presents")
#set background color
root["bg"]="#D55B06"
#Label for heading
Label(root,text = 'Krazy Youtube Video Downloader', font ='arial 18 bold',fg="white",bg="#D55B06").pack()
#declaring variable
link = StringVar()
#label for heading
Label(root, text = 'Paste video Link Here:', font = 'arial 15 bold',bg="#D55B06").place(x= 160 , y = 60)
#textbox/entrybox for getting link of video
link_enter = Entry(root, width = 60,textvariable = link).place(x = 72, y = 90)
#function for downloading
def YouTubeDownloader():
    #applying validation
    if link.get()!="":
     #getting url
     url =YouTube(str(link.get()))
     video = url.streams.first()
     #download video
     video.download()
     #show messsage
     Label(root, text = 'Video is downloaded successfully', font = 'arial 15',bg="#D55B06",fg="white").place(x= 70 , y = 210)
    else:
        Label(root, text='Link is empty', font='arial 15',bg="#D55B06",fg="white").place(x=180, y=210)
#button for download
Button(root,text = 'Download', font = 'arial 15 bold' ,bg = 'red',fg="white", padx = 1, command = YouTubeDownloader).place(x=180 ,y = 150)
#Run Application
root.mainloop()

Here you just need to type this your code into program.py file or you can copy this code for your personal use only.

Step 8: Run Code: To run this code just right click on the coding area and click on Run Program and you will get a output screen like below.

Python Youtube video downloader with Pytube

Here you have to just paste the Youtube video url in the textbox then click on the download button and wait for sometime, Download time depends upon the duration and quality of the video and when it will be downloaded you will get a message like below.
 
Python Youtube video downloader with Pytube

Now visit your project directory you will get  a video file Lets Play that video and Enjoy.😊

I hope now you can  create "Python Youtube video downloader with Pytube". If you have any doubt regarding this post  or you want something more in this post then let me know by comment below i will work on it definitely.
 

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

If you like my post then share it with your friends. Thanks😊Happy Coding.

Post a Comment

0 Comments