Spaces:
Build error
Build error
Commit
·
415735d
1
Parent(s):
4128a8b
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,58 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import IPython.display as ipd
|
5 |
-
import librosa
|
6 |
-
import soundfile as sf
|
7 |
from gtts import gTTS
|
8 |
-
from
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
audio = video.audio
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import uuid
|
3 |
+
from flask import Flask, request, render_template, redirect, url_for
|
|
|
|
|
|
|
4 |
from gtts import gTTS
|
5 |
+
from moviepy.editor import VideoFileClip, AudioFileClip
|
6 |
+
from tqdm import tqdm
|
7 |
+
import requests
|
8 |
+
|
9 |
+
app = Flask(__name__)
|
10 |
+
app.secret_key = os.environ.get('SECRET_KEY') or str(uuid.uuid4())
|
11 |
+
|
12 |
+
@app.route('/', methods=['GET', 'POST'])
|
13 |
+
def index():
|
14 |
+
if request.method == 'POST':
|
15 |
+
if 'video_file' in request.files:
|
16 |
+
video_file = request.files['video_file']
|
17 |
+
elif 'video_url' in request.form:
|
18 |
+
video_url = request.form['video_url']
|
19 |
+
response = requests.get(video_url)
|
20 |
+
video_file = response.content
|
21 |
+
|
22 |
+
language = request.form['language']
|
23 |
+
dubbed_video_file = generate_dubbed_video(video_file, language)
|
24 |
+
|
25 |
+
return redirect(url_for('download', video_id=dubbed_video_file))
|
26 |
+
|
27 |
+
return render_template('index.html')
|
28 |
+
|
29 |
+
@app.route('/download/<video_id>')
|
30 |
+
def download(video_id):
|
31 |
+
return render_template('download.html', video_id=video_id)
|
32 |
+
|
33 |
+
def generate_dubbed_video(video_file, language):
|
34 |
+
video_id = str(uuid.uuid4())
|
35 |
+
video = VideoFileClip(video_file)
|
36 |
audio = video.audio
|
37 |
+
text = audio.to_soundarray().tobytes()
|
38 |
+
|
39 |
+
try:
|
40 |
+
tts = gTTS(text=text.decode(), lang=language)
|
41 |
+
except UnicodeDecodeError:
|
42 |
+
tts = gTTS(text=text.decode(errors='ignore'), lang=language)
|
43 |
+
|
44 |
+
tts.save(f'{video_id}.mp3')
|
45 |
+
audio_clip = AudioFileClip(f'{video_id}.mp3')
|
46 |
+
|
47 |
+
with tqdm(total=100, desc="Dubbing Progress", unit="%") as progress_bar:
|
48 |
+
for i, frame in enumerate(video.iter_frames()):
|
49 |
+
video.set_frame(i)
|
50 |
+
audio_frame = audio_clip.get_frame(i / video.fps)
|
51 |
+
video.audio = audio_frame
|
52 |
+
progress_bar.update(i / len(video.iter_frames()) * 100)
|
53 |
+
|
54 |
+
video.write_videofile(f'{video_id}.mp4', fps=video.fps)
|
55 |
+
return video_id
|
56 |
+
|
57 |
+
if __name__ == '__main__':
|
58 |
+
app.run(host="0.0.0.0",port=7860,debug=True)
|