article

Thursday, June 8, 2023

Python Flask YouTube video Downloader

Python Flask YouTube video Downloader

Python Flask 

https://flask.palletsprojects.com/en/2.3.x/installation/
 
Create an environment
C:\flask_dev>py -3 -m venv venv

Activate the environment
C:\flask_dev>venv\Scripts\activate

Install Flask
venv C:\flask_dev>pip install Flask

Install requirements

pytube
pytube is a genuine, lightweight, dependency-free Python library (and command-line utility) for downloading YouTube videos.
https://pypi.org/project/pytube/

(venv) PS C:\flask_dev\myapp>pip install pytube

pathlib
pathlib offers a set of classes to handle filesystem paths. It offers the following advantages over using string objects:
https://pypi.org/project/pathlib/

(venv) PS C:\flask_dev\myapp>pip install pathlib

C:\flask_dev\myapp\app.py
 
#
from flask import Flask, request, render_template
from pytube import YouTube #pip install pytube https://pypi.org/project/pytube/
from pathlib import Path #pip install pathlib https://pypi.org/project/pathlib/
import os
import re

app = Flask(__name__)

@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/Youtube_download"))
                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('youtube.html', mesage = mesage, errorType = errorType) 

if __name__ == "__main__":
    app.run()
templates/youtube.html
 
#templates/youtube.html
<html>
<head>
    <Title>Python Flask YouTube video Downloader</Title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>    
    <div class="container mt-3">
        <h2>Python Flask YouTube 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 (venv) C:\flask_dev\myapp>flask run
http://127.0.0.1:5000/download

Related Post