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
- Importing required libraries for our Youtube Subtitles Downloader Flask App.
import cv2 import os import requests from fpdf import FPDF from flask import Flask,request,render_template
Step 2 – Getting API_KEY from RapidAPI
- Get your API KEY, API HOST, and links from RapidAPI.
- Open RapidAPI and search for YouTube Media Downloader.
- Now click on Subscribe to Test and Subscribe to the Basic Plan which is FREE 🙂
- Now copy your API_KEY, API_HOST, and video_details_url from here.
- For subtitle_details_url click on Translate, Convert, and Download Subtitle in the most left corner and again copy the URL from there.
- Finally, define the Flask app.
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
- This function will simply extract the VideoID from the URL.
- Suppose the URL is https://www.youtube.com/watch?v=abc123 then the videoID here will be abc123.
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
- This function takes the videoID and returns the subtitle URL for the video.
- This URL will show the subtitles in XML format.
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
- Now we will extract Text from this URL which is having subtitles in XML format.
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
- This function will help us put subtitle text in a PDF File and download it.
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
- home() function will render home.html as our homepage.
- downloadsubs() will take the URL, extract its VideoID, extract its Subtitle URL, extract Subtitle text from the URL, put it in a PDF File, and finally save it.
@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
- To download subtitles in XML format, right-click and click ‘Save As’.
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>
Download Source Code
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 projects, deep learning projects, computer vision projects, NLP projects, and Flask projects at machinelearningprojects.net.