
Hello Coders!
This tutorial will show you how to create a sample application that downloads YouTube videos. We Can do this by a small project through python and Django.YouTube Video downloader provides a GUI interface for cli based yt-downloader library for Linux systems. It has all features such as YTD or any other popular video downloaders.. It uses this library as a backend for the web application and shows information in frontend to make downloading YouTube video easier and user friendly.
What do we need for Youtube Video Downloader ?
pytube: We will be using pytube library. ‘pytube’ ( pytube.io/en/latest) is a lightweight, Pythonic, dependency-free, library (and command-line utility) for downloading YouTube Videos.
project setup
Install the basic needs By referring following blog
Now we have to install pytube by following command
pip install pytube
Now we can start our project
1.Create a project . To Create a project use following command
django-admin startproject YTVdownloader cd YTVdownloader
2.Lets Create a Video Downloader Application in our project
python manage.py startapp YTVapp
3.Now we need to add our app in INSTALLED_APPS list In settings.py file
settings.py
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'YTVapp', #Installed app ]
3.Go to views.py Create a function that receives the video link and download video from that link.
views.py
from django.shortcuts import render from pytube import YouTube def index(request): try: if request.method == 'POST': try: link = request.POST['link'] video = YouTube(link) stream = video.streams.get_lowest_resolution() stream.download() return render(request, 'index.html', {'msg':'Video downloaded'}) except: return render(request, 'index.html', {'msg':'Video not downloaded'}) return render(request, 'index.html', {'msg':''}) except: return render(request, "index.html", {"msg":"Sorry something went wrong!"})
4.Create HTML file to display it to the Users. Inside templates folder create a file ‘index.html’
in that User can Upload youtube link. To do this we will use Django POST method using csrf token for security.
index.html
<!DOCTYPE html> <html> <head> <style> .container { text-align: center; } .form { width: 100%; } </style> </head> <body> <div class="container"> <h1>Youtube video downloader</h1> <p>{{msg}}</p> <form action="" method="post" class="form"> {% csrf_token %} <label for="link">Enter URL: </label> <input type="text" id="link" name="link"><br><br> <input type="submit" value="Download"> </form> </div> </body> </html>
5.Now we will define the path or map urls to views inside the urls.py
urls.py
from django.contrib import admin from django.urls import path from YTVapp import views urlpatterns = [ path('', views.index, name='index'), path('admin/', admin.site.urls), ]
6/.We are all set to run our project: In the command line type:
python manage.py runserver
Go to localhost to check our YouTube video downloader.
Paste Your youtube video link in given field. And click on download button

Video downloaded message is going to show in window

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

Github Link :- https://github.com/saikumar248/YouTube_Downloader.git