How to download Videos from YouTube using Python?

YouTube is an very popular online video sharing and social media website which was launched on February 14, 2005. Video downloading from YouTube is pretty tough. From Python we can easily download Videos from YouTube using Python library Pytube.
Pytube is a lightweight, Pythonic, dependency-free, library where we can download videos from YouTube.
Pytube is not a Standard library. You have to install Pytube before using it.
You can easily install pytube using pip(It is a package manager for modules or packages).

Type the following command in the terminal or command prompt to install pytube.

pip install pytube

Download a Video from YouTube using Python

First we have to import Youtube module from pytube.
Then we have to create an object of Youtube module where we will pass the url as parameter.
After that we have to set correct extension and resolution.
Then, we have to write ‘First()’ function to return the first element in the list of video formats available and last we have to use download function to download the file in location we can set.

# importing library
from pytube import YouTube

# creating an input for pasting or writing URL
url = input("Enter YouTube Video URL:- ")
# creating object using YouTube
vid = YouTube(url)

# filtering files with mp4 extension
# progressive put video and audio in single file

Yvideo = vid.streams.filter(file_extension='mp4', progressive=True).order_by('resolution').desc()

# First() only returns the first element in the list of video formats available
# '.download' downloads the file in specific location

Download a Highest or Lowest Quality Video from YouTube using Pytube

We can also download highest or lowest quality video from youtube by simply using ‘get_highest_resolution()’ or ‘get_lowest_resolution()’ function.

For Highest quality we have to use:

# To download Highest Quality Video from YouTube

And, for Lowest quality we have to use:

# To download Lowest Quality Video from YouTube

Download YouTube Videos using Django

Django is high level web framework of python with clean and rapid development.

To create a YouTube Downloader tool. Firstly, you have to download and install Django.

After Installing Django, we have to create an html file where user can paste URL and can download the video.

We will use POST method in this Project that will give us the URL written on HTML tag securely and for using POST method we will also need csrf token.


<!DOCTYPE html>
<html lang="en">
        <h1>Download Youtube Video</h1>
        <form action="" method="post">
        {% csrf_token %}
        <input type="text" id="y_url" name="y_url">
        <input type="submit">

After, creating html file we have to create a Python file that will receives the URL and download the video from YouTube.

from django.shortcuts import render
from pytube import *

# Create your views here.
def index(request):
    if request.method == 'POST':
        link = request.POST['y_url']
        vid = YouTube(link)
        Yvideo = vid.streams.filter(file_extension='mp4', progressive=True).order_by('resolution').desc()
    return render(request, "index.html")

And Lastly, we have to specify URL Paths.

from django import views
from django.urls import path
from . import views
urlpatterns= [
    path("index", views.index, name="index")

And then, you can simply run the project by using “python runserver” in terminal or command prompt.


Clicking Submit Button to download Video from Youtube using python program

After Clicking submit button, video will be downloaded in ~/downloads file in Project.

Showing where video is downloaded in python program