Dubber / app.py
imseldrith's picture
Update app.py
cae88b4
from flask import Flask, request, render_template, send_file
import moviepy.editor as mp
import os
import cv2
import tempfile
from gtts import gTTS
import numpy as np
from progress.bar import Bar
import requests
app = Flask(__name__)
app.config["TEMP_FOLDER"] = "temp"
@app.route("/")
def index():
return render_template("index.html")
@app.route("/dub", methods=["POST"])
def dub():
# Check if a video was uploaded
if "video" in request.files:
video = request.files["video"]
filename = tempfile.NamedTemporaryFile(dir=app.config["TEMP_FOLDER"], delete=False).name
video.save(filename)
#filename = video.filename
#video.save(os.path.join(app.config["TEMP_FOLDER"], filename))
else:
# Get video from URL
url = request.form["url"]
filename = url.split("/")[-1]
response = requests.get(url)
with open(os.path.join(app.config["TEMP_FOLDER"], filename), "wb") as f:
f.write(response.content)
# Load video
clip = mp.VideoFileClip(os.path.join(app.config["TEMP_FOLDER"], filename))
# Get audio
audio = clip.audio
if isinstance(audio.fps, int):
n = audio.fps
else:
n = len(audio.fps)
bar = Bar("Dubbing", max=n)
text = ""
for i in range(n):
bar.next()
frame = audio.get_frame(i/audio.fps)
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
text += " ".join([str(x) for x in gray.flatten()]) + " "
bar.finish()
# Convert audio to text-to-speech in Hindi
tts = gTTS(text, lang="hi")
tts.save("temp/dubbed_audio.mp3")
# Add dubbed audio to video
dubbed_audio = mp.AudioFileClip("temp/dubbed_audio.mp3")
dubbed_video = clip.set_audio(dubbed_audio)
dubbed_video.write_videofile(os.path.join(app.config["TEMP_FOLDER"], "dubbed_" + filename))
return render_template("dub.html", filename="dubbed_" + filename)
@app.route("/download/<filename>")
def download(filename):
return send_file(os.path.join(app.config["TEMP_FOLDER"], filename), as_attachment=True)
if __name__ == "__main__":
app.run(host="0.0.0.0",port=7860,debug=True)