Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,7 @@ import pysrt
|
|
9 |
from gtts import gTTS
|
10 |
from pydub import AudioSegment
|
11 |
|
12 |
-
#
|
13 |
change_settings({"IMAGEMAGICK_BINARY": "/usr/bin/convert"})
|
14 |
|
15 |
# Configuration
|
@@ -65,34 +65,33 @@ def translate_subtitles(srt_path, target_langs, output_dir):
|
|
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'
|
82 |
-
)
|
83 |
-
|
84 |
-
|
|
|
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:
|
@@ -122,12 +121,11 @@ def process_video(video_file, source_lang, target_langs, progress=gr.Progress())
|
|
122 |
output_files.append(output_path)
|
123 |
|
124 |
progress(1.0, "Done!")
|
125 |
-
return output_files
|
126 |
|
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 |
|
@@ -147,12 +145,12 @@ with gr.Blocks() as demo:
|
|
147 |
submit_btn = gr.Button("Translate")
|
148 |
|
149 |
with gr.Column():
|
150 |
-
|
151 |
|
152 |
submit_btn.click(
|
153 |
process_video,
|
154 |
inputs=[video_input, source_lang, target_langs],
|
155 |
-
outputs=
|
156 |
)
|
157 |
|
158 |
if __name__ == "__main__":
|
|
|
9 |
from gtts import gTTS
|
10 |
from pydub import AudioSegment
|
11 |
|
12 |
+
# Configure ImageMagick policy
|
13 |
change_settings({"IMAGEMAGICK_BINARY": "/usr/bin/convert"})
|
14 |
|
15 |
# Configuration
|
|
|
65 |
|
66 |
def merge_video_subtitles(video_path, srt_path, output_path):
|
67 |
try:
|
|
|
68 |
video = mp.VideoFileClip(video_path)
|
69 |
txt_clips = []
|
70 |
|
|
|
71 |
for sub in pysrt.open(srt_path):
|
72 |
+
txt_clip = (mp.TextClip(
|
73 |
sub.text,
|
74 |
fontsize=24,
|
75 |
color='white',
|
76 |
font='Arial',
|
77 |
bg_color='black',
|
78 |
size=(video.w*0.9, None),
|
79 |
+
method='caption'
|
80 |
+
)
|
81 |
+
.set_position(('center', 'bottom'))
|
82 |
+
.set_duration((sub.end.ordinal - sub.start.ordinal)/1000.0)
|
83 |
+
.set_start(sub.start.ordinal/1000.0)
|
84 |
|
85 |
txt_clips.append(txt_clip)
|
86 |
|
|
|
87 |
final = mp.CompositeVideoClip([video] + txt_clips)
|
88 |
final.write_videofile(
|
89 |
output_path,
|
90 |
codec='libx264',
|
91 |
audio_codec='aac',
|
92 |
threads=4,
|
93 |
+
ffmpeg_params=['-crf', '23'],
|
94 |
+
logger=None
|
95 |
)
|
96 |
return output_path
|
97 |
except Exception as e:
|
|
|
121 |
output_files.append(output_path)
|
122 |
|
123 |
progress(1.0, "Done!")
|
124 |
+
return output_files[0] # Return first video only for demo
|
125 |
|
126 |
except Exception as e:
|
127 |
raise gr.Error(f"Processing failed: {str(e)}")
|
128 |
|
|
|
129 |
with gr.Blocks() as demo:
|
130 |
gr.Markdown("# Video Translation System")
|
131 |
|
|
|
145 |
submit_btn = gr.Button("Translate")
|
146 |
|
147 |
with gr.Column():
|
148 |
+
output_video = gr.Video(label="Translated Video")
|
149 |
|
150 |
submit_btn.click(
|
151 |
process_video,
|
152 |
inputs=[video_input, source_lang, target_langs],
|
153 |
+
outputs=output_video
|
154 |
)
|
155 |
|
156 |
if __name__ == "__main__":
|