Site icon Machine Learning Projects

Youtube Subtitles Downloader Flask App – Interesting Project – 2023

Machine Learning Projects

In today’s blog, we will build a Youtube Subtitles Downloader Flask App which we can later deploy on the web for free either on Heroku or Pythonanywhere.

Check out the video here – https://youtu.be/N7-mR8jfP-U

Sneak Peek at our Youtube Subtitles Downloader Flask App

Subtitles in XML Format

This is going to be a very interesting blog, so without any further due, let’s do it…

Step 1 – Importing required libraries

import cv2
import os
import requests
from fpdf import FPDF
from flask import Flask,request,render_template

Step 2 – Getting API_KEY from RapidAPI

API_KEY = "YOUR API KEY"
API_HOST = "youtube-media-downloader.p.rapidapi.com"
video_details_url = "https://youtube-media-downloader.p.rapidapi.com/v2/video/details"
subtitle_details_url = "https://youtube-media-downloader.p.rapidapi.com/v2/video/subtitles"

app = Flask(__name__)

Step 3 – Function for getting VideoID

def get_video_id(videoURL):
    # split YouTube URL by '/' so we can get its ID
    videoID = str(videoURL).split('/')
    # get the last part of the list which is the ID
    videoID = videoID[-1]
    if '=' in videoID:
        videoID = videoID.split('=')[-1]
    if not videoID[0].isalpha():
        videoID = videoID[1:]
    print(videoID)
    return videoID

Step 4 – Function for getting Video Details

def get_video_detail(videoID):
    # access the API
    url = video_details_url
    headers = {
        'x-rapidapi-host': API_HOST,
        'x-rapidapi-key': API_KEY
    }
    # send a get request to the API 
    querystring = {"videoId": videoID}
    response = requests.request("GET", url, headers=headers, params=querystring)
    # conver the response to json format
    json_response = response.json()
    # obtain the subtitle url (in XML format)
    try:
        subtitleURL = json_response['subtitles']['items'][0]['url']
        print(subtitleURL)
        return subtitleURL
    except:
        print('No subtitles in this video...')
        return None

Step 5 – Function for getting Subtitle Text

def get_subtitle_text(subtitleURL):
    # access the API
    url = subtitle_details_url
    headers = {
        'x-rapidapi-host': API_HOST,
        'x-rapidapi-key': API_KEY
    }
    # send a get subtitle text request to the API 
    querystring = {"subtitleUrl": subtitleURL}
    response = requests.request("GET", url, headers=headers, params=querystring)
    # return the text response
    return response.text

Step 6 – Function for Downloading Subtitle File

def Convert_And_Download_Subtitle(text):
    # create a pdf object
    pdf = FPDF()
    # add a page to the pdf
    pdf.add_page()
    # set font and size of the font
    pdf.set_font("Arial", size=12)
    # for evey line in the text
    for line in text.split('\n'):
        # add the line to the pdf
        pdf.cell(200, 5, txt=line, ln=1)
    
    # save and download the pdf with a custom file name
    pdf.output("subtitle.pdf")
    print('Subtitles PDF Saved!!!')

Step 7 – Defining Routing Functions for our Flask App

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/downloadsubs',methods=['POST'])
def downloadsubs():
    if request.method == 'POST':
        videoURL = request.form['yturl']
        videoID = get_video_id(videoURL)
        subtitleURL = get_video_detail(videoID)
        if subtitleURL:
            subtitles = get_subtitle_text(subtitleURL)
            Convert_And_Download_Subtitle(subtitles)

    return render_template('home.html',url=subtitleURL,subtitletext=subtitles)


if __name__ == '__main__':
    app.run(debug=True)

Final Look of our Youtube Subtitles Downloader Flask App

Subtitles in XML Format

Downloaded PDF Subtitle File

Code files for our Youtube Subtitles Downloader Flask App

app.py

import cv2
import os
import requests
from fpdf import FPDF
from flask import Flask,request,render_template

API_KEY = "YOUR API KEY"
API_HOST = "youtube-media-downloader.p.rapidapi.com"
video_details_url = "https://youtube-media-downloader.p.rapidapi.com/v2/video/details"
subtitle_details_url = "https://youtube-media-downloader.p.rapidapi.com/v2/video/subtitles"

app = Flask(__name__)

def get_video_id(videoURL):
    # split YouTube URL by '/' so we can get its ID
    videoID = str(videoURL).split('/')
    # get the last part of the list which is the ID
    videoID = videoID[-1]
    if '=' in videoID:
        videoID = videoID.split('=')[-1]
    if not videoID[0].isalpha():
        videoID = videoID[1:]
    print(videoID)
    return videoID

def get_video_detail(videoID):
    # access the API
    url = video_details_url
    headers = {
        'x-rapidapi-host': API_HOST,
        'x-rapidapi-key': API_KEY
    }
    # send a get request to the API 
    querystring = {"videoId": videoID}
    response = requests.request("GET", url, headers=headers, params=querystring)
    # conver the response to json format
    json_response = response.json()
    # obtain the subtitle url (in XML format)
    try:
        subtitleURL = json_response['subtitles']['items'][0]['url']
        print(subtitleURL)
        return subtitleURL
    except:
        print('No subtitles in this video...')
        return None
        
def get_subtitle_text(subtitleURL):
    # access the API
    url = subtitle_details_url
    headers = {
        'x-rapidapi-host': API_HOST,
        'x-rapidapi-key': API_KEY
    }
    # send a get subtitle text request to the API 
    querystring = {"subtitleUrl": subtitleURL}
    response = requests.request("GET", url, headers=headers, params=querystring)
    # return the text response
    return response.text

def Convert_And_Download_Subtitle(text):
    # create a pdf object
    pdf = FPDF()
    # add a page to the pdf
    pdf.add_page()
    # set font and size of the font
    pdf.set_font("Arial", size=12)
    # for evey line in the text
    for line in text.split('\n'):
        # add the line to the pdf
        pdf.cell(200, 5, txt=line, ln=1)
    
    # save and download the pdf with a custom file name
    pdf.output("subtitle.pdf")
    print('Subtitles PDF Saved!!!')

@app.route('/')
def home():
    return render_template('home.html')

@app.route('/downloadsubs',methods=['POST'])
def downloadsubs():
    if request.method == 'POST':
        videoURL = request.form['yturl']
        videoID = get_video_id(videoURL)
        subtitleURL = get_video_detail(videoID)
        if subtitleURL:
            subtitles = get_subtitle_text(subtitleURL)
            Convert_And_Download_Subtitle(subtitles)

    return render_template('home.html',url=subtitleURL,subtitletext=subtitles)


if __name__ == '__main__':
    app.run(debug=True)

home.html

<!doctype html>
<html lang="en">

<style type='text/css'>
    body {
        font-family: sans-serif;
        margin-top: 40px;
    }

    .regform {
        width: 800px;
        background-color: rgb(0, 0, 0, 0.8);
        margin: auto;
        color: #FFFFFF;
        padding: 10px 0px 10px 0px;
        text-align: center;
        border-radius: 15px 15px 0px 0px;

    }

    .main-form {
        width: 800px;
        margin: auto;
        background-color: rgb(0, 0, 0, 0.7);
        padding-left: 50px;
        padding-right: 50px;
        padding-bottom: 20px;
        color: #FFFFFF;
    }
</style>


<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">

    <title>Youtube Subtitle Downloader</title>
</head>

<body>

    <div class='regform mt-3'>
        <h1>Youtube Subtitle Downloader</h1>
    </div>

    <form action='/downloadsubs' class='main-form' method="POST">

        <div class='text-center'>
            <label> Paste Youtube Video URL</label>
            <input type="text" id="yturl" name='yturl' style="margin-top:10px;margin-bottom:10px;width: 700px;">
            <button type='submit' class='btn btn-outline-success' style="margin-top:10px;margin-bottom:10px;width: 700px;"> Download Subtitles
            </button>
        </div>

    </form>

    {% if url %}
    <div class="text-center">
        <h3>PDF Downloaded in your Folder as subtitle.pdf</h3>
        <a href='{{ url }}'><h2>Download Subtitles XML File</h2></a>
    </div>
    
    <div class="text-center">
        <p>{{ l }}</p>
    </div>
    {% endif %}

</body>

</html>

And this is how you can build a Youtube Subtitles Downloader Flask App…

You can deploy it online also: How to Deploy a Flask app online using Pythonanywhere

So this is all for this blog folks. Thanks for reading it and I hope you are taking something with you after reading this and till the next time …

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projects, and Flask projects at machinelearningprojects.net.

Exit mobile version