Spaces:
Build error
Build error
Commit
·
7a33b3e
1
Parent(s):
1c3ba84
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,60 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
import
|
6 |
-
import soundfile as sf
|
7 |
from gtts import gTTS
|
8 |
-
from
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
tts = gTTS(audio, lang='hi')
|
13 |
-
audio = tts.get_wav_data()
|
14 |
-
return audio
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
video = mp.VideoFileClip(video_file)
|
20 |
audio = video.audio
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
|
|
|
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
return
|
32 |
|
33 |
-
|
34 |
-
def
|
35 |
-
|
36 |
-
video_dubbed = dub_video(video_file)
|
37 |
-
return video_dubbed
|
38 |
|
39 |
-
|
40 |
-
|
|
|
1 |
+
import os
|
2 |
+
import uuid
|
3 |
+
import requests
|
4 |
+
|
5 |
+
from flask import Flask, render_template, request, redirect, url_for, flash
|
|
|
6 |
from gtts import gTTS
|
7 |
+
from moviepy.editor import VideoFileClip, AudioFileClip
|
8 |
|
9 |
+
app = Flask(__name__)
|
10 |
+
app.secret_key = "secret_key"
|
|
|
|
|
|
|
11 |
|
12 |
+
def generate_dubbed_video(video_file, language):
|
13 |
+
# Extract audio from video
|
14 |
+
video = VideoFileClip(video_file)
|
|
|
15 |
audio = video.audio
|
16 |
+
audio_file = f"{uuid.uuid4()}.mp3"
|
17 |
+
audio.write_audiofile(audio_file)
|
18 |
+
|
19 |
+
# Generate dubbed audio
|
20 |
+
tts = gTTS(text=audio.to_soundarray().tobytes().decode(), lang=language)
|
21 |
+
dubbed_audio_file = f"{uuid.uuid4()}.mp3"
|
22 |
+
tts.save(dubbed_audio_file)
|
23 |
|
24 |
+
# Replace video audio with dubbed audio
|
25 |
+
dubbed_audio = AudioFileClip(dubbed_audio_file)
|
26 |
+
dubbed_video = video.set_audio(dubbed_audio)
|
27 |
+
dubbed_video_file = f"{uuid.uuid4()}.mp4"
|
28 |
+
dubbed_video.write_videofile(dubbed_video_file)
|
29 |
|
30 |
+
# Clean up temporary audio and video files
|
31 |
+
os.remove(audio_file)
|
32 |
+
os.remove(dubbed_audio_file)
|
33 |
+
|
34 |
+
return dubbed_video_file
|
35 |
+
|
36 |
+
@app.route("/", methods=["GET", "POST"])
|
37 |
+
def index():
|
38 |
+
if request.method == "POST":
|
39 |
+
if "video_url" in request.form:
|
40 |
+
video_url = request.form["video_url"]
|
41 |
+
video_file = f"{uuid.uuid4()}.mp4"
|
42 |
+
response = requests.get(video_url)
|
43 |
+
open(video_file, "wb").write(response.content)
|
44 |
+
elif "video_file" in request.files:
|
45 |
+
video_file = request.files["video_file"]
|
46 |
+
video_file.save(f"{uuid.uuid4()}.mp4")
|
47 |
+
|
48 |
+
language = request.form.get("language")
|
49 |
+
dubbed_video_file = generate_dubbed_video(video_file, language)
|
50 |
+
flash("Dubbed video generated successfully!")
|
51 |
+
return redirect(url_for("download", dubbed_video_file=dubbed_video_file))
|
52 |
|
53 |
+
return render_template("index.html")
|
54 |
|
55 |
+
@app.route("/download/<dubbed_video_file>")
|
56 |
+
def download(dubbed_video_file):
|
57 |
+
return redirect(url_for("static", filename=dubbed_video_file))
|
|
|
|
|
58 |
|
59 |
+
if __name__ == "__main__":
|
60 |
+
app.run(host="0.0.0.0",port=7860,debug=True)
|