Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import tempfile
|
4 |
import moviepy.editor as mp
|
5 |
-
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
|
6 |
import assemblyai as aai
|
7 |
from deep_translator import GoogleTranslator
|
8 |
import pysrt
|
@@ -20,9 +19,13 @@ LANGUAGES = {
|
|
20 |
"Hindi": "hi"
|
21 |
}
|
22 |
|
|
|
|
|
|
|
|
|
23 |
def extract_audio(video_path, output_dir):
|
24 |
try:
|
25 |
-
video = VideoFileClip(video_path)
|
26 |
audio_path = os.path.join(output_dir, "audio.wav")
|
27 |
video.audio.write_audiofile(audio_path)
|
28 |
return audio_path
|
@@ -60,45 +63,88 @@ def translate_subtitles(srt_path, target_langs, output_dir):
|
|
60 |
except Exception as e:
|
61 |
raise Exception(f"Translation failed: {str(e)}")
|
62 |
|
63 |
-
def
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
stroke_width=1,
|
80 |
font='Arial',
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
83 |
)
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
-
|
91 |
-
|
92 |
output_path,
|
93 |
codec='libx264',
|
94 |
-
|
|
|
95 |
threads=4,
|
96 |
-
|
97 |
-
logger=None
|
98 |
)
|
|
|
99 |
return output_path
|
100 |
except Exception as e:
|
101 |
-
|
|
|
|
|
102 |
|
103 |
def process_video(video_file, source_lang, target_langs, progress=gr.Progress()):
|
104 |
with tempfile.TemporaryDirectory() as tmpdir:
|
|
|
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
|
|
|
19 |
"Hindi": "hi"
|
20 |
}
|
21 |
|
22 |
+
# Add this at the beginning to disable ImageMagick usage
|
23 |
+
import os
|
24 |
+
os.environ["IMAGEMAGICK_BINARY"] = ""
|
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
|
|
|
63 |
except Exception as e:
|
64 |
raise Exception(f"Translation failed: {str(e)}")
|
65 |
|
66 |
+
def create_subtitle_clips(subtitles, videosize, fontsize=24):
|
67 |
+
"""Create subtitle clips based on subtitles file"""
|
68 |
+
subtitle_clips = []
|
69 |
+
|
70 |
+
for subtitle in subtitles:
|
71 |
+
start_time = subtitle.start.ordinal / 1000.0
|
72 |
+
end_time = subtitle.end.ordinal / 1000.0
|
73 |
+
duration = end_time - start_time
|
74 |
|
75 |
+
# Create a solid color clip with the dimensions of the video
|
76 |
+
# and overlay the text on it
|
77 |
+
import numpy as np
|
78 |
+
from moviepy.editor import ColorClip, TextClip, CompositeVideoClip
|
79 |
+
|
80 |
+
txt = subtitle.text.replace('\n', ' ')
|
81 |
+
|
82 |
+
# Create text overlay
|
83 |
+
try:
|
84 |
+
text_clip = mp.TextClip(
|
85 |
+
txt,
|
|
|
86 |
font='Arial',
|
87 |
+
fontsize=fontsize,
|
88 |
+
color='white',
|
89 |
+
bg_color='black',
|
90 |
+
size=(videosize[0] * 0.9, None),
|
91 |
+
method='pango' # Try to use pango instead of ImageMagick
|
92 |
)
|
93 |
+
except:
|
94 |
+
# If pango fails, try without method specification
|
95 |
+
try:
|
96 |
+
text_clip = mp.TextClip(
|
97 |
+
txt,
|
98 |
+
font='Arial',
|
99 |
+
fontsize=fontsize,
|
100 |
+
color='white',
|
101 |
+
bg_color='black',
|
102 |
+
size=(videosize[0] * 0.9, None)
|
103 |
+
)
|
104 |
+
except:
|
105 |
+
# Last resort - create a simple black background with text overlay
|
106 |
+
w, h = videosize[0], 60
|
107 |
+
txtclip = ColorClip((w, h), col=(0, 0, 0))
|
108 |
+
txt_col = ColorClip((w, h), col=(0, 0, 0))
|
109 |
+
text_clip = txtclip
|
110 |
+
|
111 |
+
# Position the text
|
112 |
+
text_clip = text_clip.set_position(('center', 'bottom'))
|
113 |
+
text_clip = text_clip.set_start(start_time)
|
114 |
+
text_clip = text_clip.set_duration(duration)
|
115 |
+
|
116 |
+
subtitle_clips.append(text_clip)
|
117 |
+
|
118 |
+
return subtitle_clips
|
119 |
+
|
120 |
+
def merge_video_subtitles(video_path, srt_path, output_path):
|
121 |
+
try:
|
122 |
+
# Load video and subtitles
|
123 |
+
video = mp.VideoFileClip(video_path)
|
124 |
+
subtitles = pysrt.open(srt_path)
|
125 |
+
|
126 |
+
# Create subtitle clips
|
127 |
+
subtitle_clips = create_subtitle_clips(subtitles, video.size)
|
128 |
+
|
129 |
+
# Overlay subtitles on video
|
130 |
+
final_video = mp.CompositeVideoClip([video] + subtitle_clips)
|
131 |
|
132 |
+
# Write output file
|
133 |
+
final_video.write_videofile(
|
134 |
output_path,
|
135 |
codec='libx264',
|
136 |
+
fps=video.fps,
|
137 |
+
preset='ultrafast', # Use ultrafast preset to speed up encoding
|
138 |
threads=4,
|
139 |
+
audio_codec='aac',
|
140 |
+
logger=None # Disable verbose logging
|
141 |
)
|
142 |
+
|
143 |
return output_path
|
144 |
except Exception as e:
|
145 |
+
import traceback
|
146 |
+
error_details = traceback.format_exc()
|
147 |
+
raise Exception(f"Video merging failed: {str(e)}\nDetails: {error_details}")
|
148 |
|
149 |
def process_video(video_file, source_lang, target_langs, progress=gr.Progress()):
|
150 |
with tempfile.TemporaryDirectory() as tmpdir:
|