Skip to content
Related Articles

Related Articles

Youtube video downloader using Django

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 25 May, 2022
Improve Article
Save Article

In this article, we will see how to make a YouTube video downloader tool in Django. We will be using pytube module for that.

Prerequisite:

  • pytube: It is python’s lightweight and dependency-free module, which is used to download YouTube Videos.
  • Django: It is python’s framework to make web-applications.

Here, we will be using Django as a backend along with pytube module to create this tool. We can install pytube module by typing the below command in the terminal.

pip install pytube

So, let’s dive in to make our YouTube video downloader tool.

First, we will create an HTML design (form) where the user can come and enter the URL of a video which he/she wants to download. We will use Django’s POST method to get that URL (because it is secure). We also need to use csrf token if we are using the POST method. Syntax for csrf token is: 

{% csrf_token %}

HTML




<!DOCTYPE html>
<html>
<body>
  
<h1>Youtube video downloader</h1>
  
  
<form action="" method="post">
  {% csrf_token %}
  
  <label for="link">Enter URL:</label>
  <input type="text" id="link" name="link"><br><br>
  <input type="submit" value="Submit">
</form>
  
</body>
</html>

 

 

Now, it’s time to create a function that receives the video link and downloads that video. You need to import function YouTube from module pytube in views.py file.  Now we can define the function to download video.

 

views.py

 

Python3




# importing all the required modules
from django.shortcuts import render, redirect
from pytube import *
  
  
# defining function
def youtube(request):
  
    # checking whether request.method is post or not
    if request.method == 'POST':
        
        # getting link from frontend
        link = request.POST['link']
        video = YouTube(link)
  
        # setting video resolution
        stream = video.streams.get_lowest_resolution()
          
        # downloads video
        stream.download()
  
        # returning HTML page
        return render(request, 'youtube.html')
    return render(request, 'youtube.html')

 

 

Now, we have to define the URL (path) for this function inside urls.py.

 

Python3




from django.contrib import admin
from django.urls import path
from . import views
  
urlpatterns = [
    path('admin/', admin.site.urls),
    path('youtube', views.youtube, name='youtube'),
]

 

 

That is it for the coding part, now you can run the project by python manage.py runserver and head over to http://localhost:8000/youtube to see the output.

 

Output:

 

 

When you click on submit a video will be downloaded in your Django project’s directory.

 

django youtube downloader

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!