Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,27 @@ from indic_transliteration import sanscript
|
|
12 |
from indic_transliteration.sanscript import transliterate
|
13 |
import azure.cognitiveservices.speech as speechsdk
|
14 |
import ffmpeg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Tamil-specific voice configurations
|
17 |
TAMIL_VOICES = {
|
@@ -25,7 +46,6 @@ class TamilTextProcessor:
|
|
25 |
@staticmethod
|
26 |
def normalize_tamil_text(text):
|
27 |
"""Normalize Tamil text for better pronunciation"""
|
28 |
-
# Convert Tamil numerals to English numerals
|
29 |
tamil_numerals = {'௦': '0', '௧': '1', '௨': '2', '௩': '3', '௪': '4',
|
30 |
'௫': '5', '௬': '6', '௭': '7', '௮': '8', '௯': '9'}
|
31 |
for tamil_num, eng_num in tamil_numerals.items():
|
@@ -35,9 +55,7 @@ class TamilTextProcessor:
|
|
35 |
@staticmethod
|
36 |
def process_for_tts(text):
|
37 |
"""Process Tamil text for TTS"""
|
38 |
-
# Remove any unsupported characters
|
39 |
text = ''.join(char for char in text if ord(char) < 65535)
|
40 |
-
# Normalize whitespace
|
41 |
text = ' '.join(text.split())
|
42 |
return text
|
43 |
|
@@ -75,11 +93,8 @@ class TamilDubber:
|
|
75 |
video = VideoFileClip(video_path)
|
76 |
audio_path = self.create_temp_file(".wav")
|
77 |
video.audio.write_audiofile(audio_path)
|
78 |
-
|
79 |
-
# Transcribe using Whisper
|
80 |
result = self.whisper_model.transcribe(audio_path)
|
81 |
return result["segments"], video.duration
|
82 |
-
|
83 |
except Exception as e:
|
84 |
st.error(f"Error in audio extraction: {e}")
|
85 |
raise
|
@@ -103,7 +118,6 @@ class TamilDubber:
|
|
103 |
})
|
104 |
except Exception as e:
|
105 |
st.warning(f"Translation warning for segment: {str(e)}")
|
106 |
-
# Keep original text if translation fails
|
107 |
translated_segments.append({
|
108 |
"text": segment["text"],
|
109 |
"start": segment["start"],
|
@@ -138,7 +152,27 @@ class TamilDubber:
|
|
138 |
st.error(f"Error creating subtitles: {e}")
|
139 |
raise
|
140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
def main():
|
|
|
|
|
|
|
142 |
st.title("Tamil Movie Dubbing System")
|
143 |
st.sidebar.header("டப்பிங் அமைப்புகள்") # Dubbing Settings in Tamil
|
144 |
|
@@ -182,7 +216,6 @@ def main():
|
|
182 |
status_text.text("Generating Tamil audio...")
|
183 |
output_segments = []
|
184 |
video = VideoFileClip(temp_video_path)
|
185 |
-
final_audio_path = dubber.create_temp_file(".mp3")
|
186 |
|
187 |
for idx, segment in enumerate(translated_segments):
|
188 |
audio_path = dubber.generate_audio(segment["text"])
|
@@ -193,44 +226,39 @@ def main():
|
|
193 |
})
|
194 |
progress_bar.progress(0.50 + (0.25 * (idx + 1) / len(translated_segments)))
|
195 |
|
196 |
-
# Generate subtitles if requested
|
197 |
-
if generate_subtitles:
|
198 |
-
subtitle_path = dubber.create_temp_file(".srt")
|
199 |
-
dubber.create_subtitles(translated_segments, subtitle_path)
|
200 |
-
|
201 |
# Create final video
|
202 |
status_text.text("Creating final video...")
|
203 |
output_path = dubber.create_temp_file(".mp4")
|
204 |
|
205 |
# Add subtitles if enabled
|
206 |
if generate_subtitles:
|
207 |
-
def create_subtitle_clip(txt):
|
208 |
-
return TextClip(
|
209 |
-
txt=txt,
|
210 |
-
fontsize=subtitle_size,
|
211 |
-
color=subtitle_color,
|
212 |
-
stroke_color='black',
|
213 |
-
stroke_width=1
|
214 |
-
)
|
215 |
-
|
216 |
subtitle_clips = []
|
217 |
for segment in translated_segments:
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
223 |
|
224 |
final_video = CompositeVideoClip([video] + subtitle_clips)
|
225 |
else:
|
226 |
final_video = video
|
227 |
|
228 |
-
# Write final video
|
229 |
final_video.write_videofile(
|
230 |
output_path,
|
231 |
codec='libx264',
|
232 |
audio_codec='aac',
|
233 |
-
fps=video.fps
|
|
|
|
|
234 |
)
|
235 |
progress_bar.progress(1.0)
|
236 |
|
|
|
12 |
from indic_transliteration.sanscript import transliterate
|
13 |
import azure.cognitiveservices.speech as speechsdk
|
14 |
import ffmpeg
|
15 |
+
from PIL import Image
|
16 |
+
import imageio
|
17 |
+
|
18 |
+
# Configure MoviePy to use imageio for reading images
|
19 |
+
imageio.plugins.ffmpeg.download()
|
20 |
+
|
21 |
+
# Configure ImageMagick policy to allow PDF and text file handling
|
22 |
+
def configure_imagemagick():
|
23 |
+
"""Configure ImageMagick policy to allow text operations"""
|
24 |
+
policy_file = "/etc/ImageMagick-6/policy.xml"
|
25 |
+
if os.path.exists(policy_file):
|
26 |
+
try:
|
27 |
+
with open(policy_file, 'r') as f:
|
28 |
+
policy_content = f.read()
|
29 |
+
# Modify policy to allow text file handling
|
30 |
+
policy_content = policy_content.replace('rights="none" pattern="@*"', 'rights="read|write" pattern="@*"')
|
31 |
+
with open(policy_file, 'w') as f:
|
32 |
+
f.write(policy_content)
|
33 |
+
except Exception as e:
|
34 |
+
st.warning(f"Unable to configure ImageMagick policy: {e}")
|
35 |
+
st.info("You may need to run this application with sudo privileges to modify ImageMagick policy")
|
36 |
|
37 |
# Tamil-specific voice configurations
|
38 |
TAMIL_VOICES = {
|
|
|
46 |
@staticmethod
|
47 |
def normalize_tamil_text(text):
|
48 |
"""Normalize Tamil text for better pronunciation"""
|
|
|
49 |
tamil_numerals = {'௦': '0', '௧': '1', '௨': '2', '௩': '3', '௪': '4',
|
50 |
'௫': '5', '௬': '6', '௭': '7', '௮': '8', '௯': '9'}
|
51 |
for tamil_num, eng_num in tamil_numerals.items():
|
|
|
55 |
@staticmethod
|
56 |
def process_for_tts(text):
|
57 |
"""Process Tamil text for TTS"""
|
|
|
58 |
text = ''.join(char for char in text if ord(char) < 65535)
|
|
|
59 |
text = ' '.join(text.split())
|
60 |
return text
|
61 |
|
|
|
93 |
video = VideoFileClip(video_path)
|
94 |
audio_path = self.create_temp_file(".wav")
|
95 |
video.audio.write_audiofile(audio_path)
|
|
|
|
|
96 |
result = self.whisper_model.transcribe(audio_path)
|
97 |
return result["segments"], video.duration
|
|
|
98 |
except Exception as e:
|
99 |
st.error(f"Error in audio extraction: {e}")
|
100 |
raise
|
|
|
118 |
})
|
119 |
except Exception as e:
|
120 |
st.warning(f"Translation warning for segment: {str(e)}")
|
|
|
121 |
translated_segments.append({
|
122 |
"text": segment["text"],
|
123 |
"start": segment["start"],
|
|
|
152 |
st.error(f"Error creating subtitles: {e}")
|
153 |
raise
|
154 |
|
155 |
+
def create_subtitle_clip(self, txt, size, color):
|
156 |
+
"""Create subtitle clip with proper configuration"""
|
157 |
+
try:
|
158 |
+
return TextClip(
|
159 |
+
txt=txt,
|
160 |
+
font='DejaVu-Sans', # Use a system font that supports Tamil
|
161 |
+
fontsize=size,
|
162 |
+
color=color,
|
163 |
+
stroke_color='black',
|
164 |
+
stroke_width=1,
|
165 |
+
method='caption', # Use caption method instead of label
|
166 |
+
size=(720, None) # Set width, let height adjust automatically
|
167 |
+
)
|
168 |
+
except Exception as e:
|
169 |
+
st.error(f"Error creating subtitle clip: {e}")
|
170 |
+
raise
|
171 |
+
|
172 |
def main():
|
173 |
+
# Configure ImageMagick at startup
|
174 |
+
configure_imagemagick()
|
175 |
+
|
176 |
st.title("Tamil Movie Dubbing System")
|
177 |
st.sidebar.header("டப்பிங் அமைப்புகள்") # Dubbing Settings in Tamil
|
178 |
|
|
|
216 |
status_text.text("Generating Tamil audio...")
|
217 |
output_segments = []
|
218 |
video = VideoFileClip(temp_video_path)
|
|
|
219 |
|
220 |
for idx, segment in enumerate(translated_segments):
|
221 |
audio_path = dubber.generate_audio(segment["text"])
|
|
|
226 |
})
|
227 |
progress_bar.progress(0.50 + (0.25 * (idx + 1) / len(translated_segments)))
|
228 |
|
|
|
|
|
|
|
|
|
|
|
229 |
# Create final video
|
230 |
status_text.text("Creating final video...")
|
231 |
output_path = dubber.create_temp_file(".mp4")
|
232 |
|
233 |
# Add subtitles if enabled
|
234 |
if generate_subtitles:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
subtitle_clips = []
|
236 |
for segment in translated_segments:
|
237 |
+
try:
|
238 |
+
clip = dubber.create_subtitle_clip(
|
239 |
+
segment["text"],
|
240 |
+
subtitle_size,
|
241 |
+
subtitle_color
|
242 |
+
)
|
243 |
+
clip = clip.set_position(('center', 'bottom'))
|
244 |
+
clip = clip.set_start(segment["start"])
|
245 |
+
clip = clip.set_duration(segment["duration"])
|
246 |
+
subtitle_clips.append(clip)
|
247 |
+
except Exception as e:
|
248 |
+
st.warning(f"Skipping subtitle for segment due to error: {e}")
|
249 |
|
250 |
final_video = CompositeVideoClip([video] + subtitle_clips)
|
251 |
else:
|
252 |
final_video = video
|
253 |
|
254 |
+
# Write final video with proper codec settings
|
255 |
final_video.write_videofile(
|
256 |
output_path,
|
257 |
codec='libx264',
|
258 |
audio_codec='aac',
|
259 |
+
fps=video.fps,
|
260 |
+
threads=4,
|
261 |
+
preset='medium'
|
262 |
)
|
263 |
progress_bar.progress(1.0)
|
264 |
|