Spaces:
Paused
Paused
Commit
·
625fdcb
1
Parent(s):
cb7c94c
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,64 @@
|
|
| 1 |
-
from flask import Flask,
|
| 2 |
import moviepy.editor as mp
|
|
|
|
| 3 |
import cv2
|
| 4 |
from gtts import gTTS
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
import uuid
|
| 8 |
import requests
|
| 9 |
-
from bs4 import BeautifulSoup
|
| 10 |
|
| 11 |
app = Flask(__name__)
|
|
|
|
| 12 |
|
| 13 |
-
@app.route(
|
| 14 |
def index():
|
| 15 |
-
return render_template(
|
| 16 |
|
| 17 |
-
@app.route(
|
| 18 |
def dub():
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
response = requests.get(url)
|
| 25 |
-
|
| 26 |
-
with open(file, "wb") as f:
|
| 27 |
f.write(response.content)
|
| 28 |
-
else:
|
| 29 |
-
file = request.files['file']
|
| 30 |
-
filename = str(uuid.uuid4().hex) + ".mp4"
|
| 31 |
-
file.save(filename)
|
| 32 |
-
|
| 33 |
-
# Load the video using moviepy
|
| 34 |
-
video = mp.VideoFileClip(file)
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
audio
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
-
return
|
| 53 |
|
| 54 |
-
@app.route(
|
| 55 |
def download(filename):
|
| 56 |
-
return send_file(filename, as_attachment=True)
|
| 57 |
|
| 58 |
-
if __name__ ==
|
| 59 |
app.run(host="0.0.0.0",port=7860,debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template, send_file
|
| 2 |
import moviepy.editor as mp
|
| 3 |
+
import os
|
| 4 |
import cv2
|
| 5 |
from gtts import gTTS
|
| 6 |
+
import numpy as np
|
| 7 |
+
from progress.bar import Bar
|
|
|
|
| 8 |
import requests
|
|
|
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
+
app.config["TEMP_FOLDER"] = "temp"
|
| 12 |
|
| 13 |
+
@app.route("/")
|
| 14 |
def index():
|
| 15 |
+
return render_template("index.html")
|
| 16 |
|
| 17 |
+
@app.route("/dub", methods=["POST"])
|
| 18 |
def dub():
|
| 19 |
+
# Check if a video was uploaded
|
| 20 |
+
if "video" in request.files:
|
| 21 |
+
video = request.files["video"]
|
| 22 |
+
filename = video.filename
|
| 23 |
+
video.save(os.path.join(app.config["TEMP_FOLDER"], filename))
|
| 24 |
+
else:
|
| 25 |
+
# Get video from URL
|
| 26 |
+
url = request.form["url"]
|
| 27 |
+
filename = url.split("/")[-1]
|
| 28 |
response = requests.get(url)
|
| 29 |
+
with open(os.path.join(app.config["TEMP_FOLDER"], filename), "wb") as f:
|
|
|
|
| 30 |
f.write(response.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Load video
|
| 33 |
+
clip = mp.VideoFileClip(os.path.join(app.config["TEMP_FOLDER"], filename))
|
| 34 |
+
|
| 35 |
+
# Get audio
|
| 36 |
+
audio = clip.audio
|
| 37 |
+
n = len(audio.fps)
|
| 38 |
|
| 39 |
+
bar = Bar("Dubbing", max=n)
|
| 40 |
+
text = ""
|
| 41 |
+
for i in range(n):
|
| 42 |
+
bar.next()
|
| 43 |
+
frame = audio.get_frame(i/audio.fps)
|
| 44 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
|
| 45 |
+
text += " ".join([str(x) for x in gray.flatten()]) + " "
|
| 46 |
+
bar.finish()
|
| 47 |
|
| 48 |
+
# Convert audio to text-to-speech in Hindi
|
| 49 |
+
tts = gTTS(text, lang="hi")
|
| 50 |
+
tts.save("temp/dubbed_audio.mp3")
|
| 51 |
|
| 52 |
+
# Add dubbed audio to video
|
| 53 |
+
dubbed_audio = mp.AudioFileClip("temp/dubbed_audio.mp3")
|
| 54 |
+
dubbed_video = clip.set_audio(dubbed_audio)
|
| 55 |
+
dubbed_video.write_videofile(os.path.join(app.config["TEMP_FOLDER"], "dubbed_" + filename))
|
| 56 |
|
| 57 |
+
return render_template("dub.html", filename="dubbed_" + filename)
|
| 58 |
|
| 59 |
+
@app.route("/download/<filename>")
|
| 60 |
def download(filename):
|
| 61 |
+
return send_file(os.path.join(app.config["TEMP_FOLDER"], filename), as_attachment=True)
|
| 62 |
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
app.run(host="0.0.0.0",port=7860,debug=True)
|