Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,18 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import tempfile
|
4 |
import moviepy.editor as mp
|
|
|
5 |
import assemblyai as aai
|
6 |
from deep_translator import GoogleTranslator
|
7 |
import pysrt
|
8 |
from gtts import gTTS
|
9 |
from pydub import AudioSegment
|
10 |
|
|
|
|
|
|
|
11 |
# Configuration
|
12 |
-
aai.settings.api_key = os.getenv("ASSEMBLYAI_API_KEY")
|
13 |
LANGUAGES = {
|
14 |
"English": "en",
|
15 |
"Spanish": "es",
|
@@ -20,17 +24,15 @@ LANGUAGES = {
|
|
20 |
}
|
21 |
|
22 |
def extract_audio(video_path, output_dir):
|
23 |
-
"""Extract audio from video file"""
|
24 |
try:
|
25 |
video = mp.VideoFileClip(video_path)
|
26 |
-
audio_path = os.path.join(output_dir, "
|
27 |
video.audio.write_audiofile(audio_path)
|
28 |
return audio_path
|
29 |
except Exception as e:
|
30 |
raise Exception(f"Audio extraction failed: {str(e)}")
|
31 |
|
32 |
def generate_subtitles(audio_path, output_dir):
|
33 |
-
"""Generate subtitles from audio file"""
|
34 |
try:
|
35 |
transcriber = aai.Transcriber()
|
36 |
transcript = transcriber.transcribe(audio_path)
|
@@ -42,7 +44,6 @@ def generate_subtitles(audio_path, output_dir):
|
|
42 |
raise Exception(f"Subtitle generation failed: {str(e)}")
|
43 |
|
44 |
def translate_subtitles(srt_path, target_langs, output_dir):
|
45 |
-
"""Translate subtitles to multiple languages"""
|
46 |
try:
|
47 |
subs = pysrt.open(srt_path)
|
48 |
results = {}
|
@@ -63,32 +64,41 @@ def translate_subtitles(srt_path, target_langs, output_dir):
|
|
63 |
raise Exception(f"Translation failed: {str(e)}")
|
64 |
|
65 |
def merge_video_subtitles(video_path, srt_path, output_path):
|
66 |
-
"""Merge video with subtitles"""
|
67 |
try:
|
|
|
68 |
video = mp.VideoFileClip(video_path)
|
69 |
-
|
70 |
|
71 |
-
|
72 |
-
for sub in
|
73 |
txt_clip = mp.TextClip(
|
74 |
sub.text,
|
75 |
fontsize=24,
|
76 |
color='white',
|
77 |
font='Arial',
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
).
|
82 |
-
|
|
|
|
|
|
|
83 |
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
return output_path
|
87 |
except Exception as e:
|
88 |
raise Exception(f"Video merging failed: {str(e)}")
|
89 |
|
90 |
def process_video(video_file, source_lang, target_langs, progress=gr.Progress()):
|
91 |
-
"""Main processing function"""
|
92 |
with tempfile.TemporaryDirectory() as tmpdir:
|
93 |
try:
|
94 |
progress(0.1, "Extracting audio...")
|
@@ -117,13 +127,13 @@ def process_video(video_file, source_lang, target_langs, progress=gr.Progress())
|
|
117 |
except Exception as e:
|
118 |
raise gr.Error(f"Processing failed: {str(e)}")
|
119 |
|
120 |
-
#
|
121 |
-
with gr.Blocks(
|
122 |
-
gr.Markdown("
|
123 |
|
124 |
with gr.Row():
|
125 |
with gr.Column():
|
126 |
-
video_input = gr.
|
127 |
source_lang = gr.Dropdown(
|
128 |
label="Source Language",
|
129 |
choices=list(LANGUAGES.keys()),
|
@@ -132,13 +142,13 @@ with gr.Blocks(title="Video Translator") as demo:
|
|
132 |
target_langs = gr.CheckboxGroup(
|
133 |
label="Target Languages",
|
134 |
choices=list(LANGUAGES.keys()),
|
135 |
-
value=["Spanish"
|
136 |
)
|
137 |
submit_btn = gr.Button("Translate")
|
138 |
|
139 |
with gr.Column():
|
140 |
-
output_videos = gr.
|
141 |
-
|
142 |
submit_btn.click(
|
143 |
process_video,
|
144 |
inputs=[video_input, source_lang, target_langs],
|
|
|
2 |
import os
|
3 |
import tempfile
|
4 |
import moviepy.editor as mp
|
5 |
+
from moviepy.config import change_settings
|
6 |
import assemblyai as aai
|
7 |
from deep_translator import GoogleTranslator
|
8 |
import pysrt
|
9 |
from gtts import gTTS
|
10 |
from pydub import AudioSegment
|
11 |
|
12 |
+
# Fix ImageMagick security policy
|
13 |
+
change_settings({"IMAGEMAGICK_BINARY": "/usr/bin/convert"})
|
14 |
+
|
15 |
# Configuration
|
16 |
+
aai.settings.api_key = os.getenv("ASSEMBLYAI_API_KEY")
|
17 |
LANGUAGES = {
|
18 |
"English": "en",
|
19 |
"Spanish": "es",
|
|
|
24 |
}
|
25 |
|
26 |
def extract_audio(video_path, output_dir):
|
|
|
27 |
try:
|
28 |
video = mp.VideoFileClip(video_path)
|
29 |
+
audio_path = os.path.join(output_dir, "audio.wav")
|
30 |
video.audio.write_audiofile(audio_path)
|
31 |
return audio_path
|
32 |
except Exception as e:
|
33 |
raise Exception(f"Audio extraction failed: {str(e)}")
|
34 |
|
35 |
def generate_subtitles(audio_path, output_dir):
|
|
|
36 |
try:
|
37 |
transcriber = aai.Transcriber()
|
38 |
transcript = transcriber.transcribe(audio_path)
|
|
|
44 |
raise Exception(f"Subtitle generation failed: {str(e)}")
|
45 |
|
46 |
def translate_subtitles(srt_path, target_langs, output_dir):
|
|
|
47 |
try:
|
48 |
subs = pysrt.open(srt_path)
|
49 |
results = {}
|
|
|
64 |
raise Exception(f"Translation failed: {str(e)}")
|
65 |
|
66 |
def merge_video_subtitles(video_path, srt_path, output_path):
|
|
|
67 |
try:
|
68 |
+
# Create a black background for text
|
69 |
video = mp.VideoFileClip(video_path)
|
70 |
+
txt_clips = []
|
71 |
|
72 |
+
# Add subtitles as simple text overlays
|
73 |
+
for sub in pysrt.open(srt_path):
|
74 |
txt_clip = mp.TextClip(
|
75 |
sub.text,
|
76 |
fontsize=24,
|
77 |
color='white',
|
78 |
font='Arial',
|
79 |
+
bg_color='black',
|
80 |
+
size=(video.w*0.9, None),
|
81 |
+
method='caption' # Simpler rendering method
|
82 |
+
).set_position(('center', 'bottom')
|
83 |
+
.set_duration((sub.end.ordinal - sub.start.ordinal)/1000.0)
|
84 |
+
.set_start(sub.start.ordinal/1000.0)
|
85 |
+
|
86 |
+
txt_clips.append(txt_clip)
|
87 |
|
88 |
+
# Composite video with subtitles
|
89 |
+
final = mp.CompositeVideoClip([video] + txt_clips)
|
90 |
+
final.write_videofile(
|
91 |
+
output_path,
|
92 |
+
codec='libx264',
|
93 |
+
audio_codec='aac',
|
94 |
+
threads=4,
|
95 |
+
ffmpeg_params=['-crf', '23']
|
96 |
+
)
|
97 |
return output_path
|
98 |
except Exception as e:
|
99 |
raise Exception(f"Video merging failed: {str(e)}")
|
100 |
|
101 |
def process_video(video_file, source_lang, target_langs, progress=gr.Progress()):
|
|
|
102 |
with tempfile.TemporaryDirectory() as tmpdir:
|
103 |
try:
|
104 |
progress(0.1, "Extracting audio...")
|
|
|
127 |
except Exception as e:
|
128 |
raise gr.Error(f"Processing failed: {str(e)}")
|
129 |
|
130 |
+
# Create interface
|
131 |
+
with gr.Blocks() as demo:
|
132 |
+
gr.Markdown("# Video Translation System")
|
133 |
|
134 |
with gr.Row():
|
135 |
with gr.Column():
|
136 |
+
video_input = gr.Video(label="Upload Video")
|
137 |
source_lang = gr.Dropdown(
|
138 |
label="Source Language",
|
139 |
choices=list(LANGUAGES.keys()),
|
|
|
142 |
target_langs = gr.CheckboxGroup(
|
143 |
label="Target Languages",
|
144 |
choices=list(LANGUAGES.keys()),
|
145 |
+
value=["Spanish"]
|
146 |
)
|
147 |
submit_btn = gr.Button("Translate")
|
148 |
|
149 |
with gr.Column():
|
150 |
+
output_videos = gr.Video(label="Translated Video")
|
151 |
+
|
152 |
submit_btn.click(
|
153 |
process_video,
|
154 |
inputs=[video_input, source_lang, target_langs],
|