In our previous Python Porject tutorial, we have explained how to make ChatBot in Python. In this tutorial, we will explain how to make YouTube video downloader in Python.
YouTube is the most popular video sharing and social media platform. The people are always willing to download the favorite videos. But it’s not always easy to download videos as it needs third party tools or website to download. So keeping this in mind, we are have tutorial for you to develop YouTube Video Downloader.
In this tutorial, we will explain how to make your own YouTube Video Downloader using Python. We will use Python library pytube
to implement video download functionality. We will use Flask
, micro web framework and Bootstrap 5 to create our YouTube Video Downloader.
So let’s proceed to make YouTube video downloader with Python and Flask.
Project Setup
We will create application directory youtube-downloader
using below command.
$ mkdir youtube-downloader
we moved to the project direcotry
$ cd youtube-downloader
Install Required Modules
We will use folloing modules in this application from Python.
- Flask: It is a micro framework from Python to create web application. So we will install this module to create web applications. We will install it using the below command:
pip install Flask
- pytube: This is Python package that can be used to YouTube videos. We will install it using the below command:
pip install pytube
- pathlib: This is Python package that offers classes representing filesystem paths with semantics appropriate for different operating systems. We will install it using the below command:
pip install pathlib
Implement YouTube Video Downloader
We will create Python file main.py
and import required modules. We will import flask
, pytube
and pathlib
module. We will also import os
module for getting path and also re
module for implementing regex to validate video url.
from flask import Flask, request, render_template from pytube import YouTube from pathlib import Path import os import re
We will create our flask application.
app = Flask(__name__)
We will create route /
for our app and create function index()
and call template file index.html
to display YouTube Video downloader form with input and a download button.
@app.route("/") def index(): return render_template('index.html')
We will create app route /download
with method POST
and GET
. We will create function downloadVideo()
to handle video downlaod functionality. We will check for form post value and then validate url. If valid YouTub video url then call method YouTube()
and handle video download functionality.
@app.route("/download", methods=["GET","POST"]) def downloadVideo(): mesage = '' errorType = 0 if request.method == 'POST' and 'video_url' in request.form: youtubeUrl = request.form["video_url"] if(youtubeUrl): validateVideoUrl = ( r'(https?://)?(www\.)?' '(youtube|youtu|youtube-nocookie)\.(com|be)/' '(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})') validVideoUrl = re.match(validateVideoUrl, youtubeUrl) if validVideoUrl: url = YouTube(youtubeUrl) video = url.streams.get_highest_resolution() downloadFolder = str(os.path.join(Path.home(), "Downloads")) video.download(downloadFolder) mesage = 'Video Downloaded Successfully!' errorType = 1 else: mesage = 'Enter Valid YouTube Video URL!' errorType = 0 else: mesage = 'Enter YouTube Video Url.' errorType = 0 return render_template('index.html', mesage = mesage, errorType = errorType)
Then we will run our flask app.
if __name__ == "__main__": app.run()
As we have already called index.html
template file, so we will create templates
directory in our project and create index.html
template file. We will include Bootstrap 5
framework files and then design video download form. Also handle message display.
<html> <head> <Title>YouTube Downloader Python</Title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script> </head> <body> <div class="container mt-3"> <h2>YouTube Video Downloader</h2> <form action="/download" method="post"> {% if mesage is defined and mesage %} {% if errorType is defined and errorType %} <div class="alert alert-success">{{ mesage }}</div> {% else %} <div class="alert alert-danger">{{ mesage }}</div> {% endif %} {% endif %} <div class="mb-3 mt-3"> <label for="video_url">Enter YouTube Video Url:</label> <input type="text" class="form-control" id="video_url" placeholder="Enter url" name="video_url"> </div> <button type="submit" class="btn btn-primary">Download</button> </form> </div> <br><br> </body> </html>
Run Application
We will run our application using below command.
python main.py
After executing above command, the application will be start running on below url in browser. You can copy url and paste in browser to load application.
http://127.0.0.1:5000
When we enter YouTube video url and press Download button, the video will be downloaded into download
directory in our computer.