Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -258,22 +258,31 @@ def translate_audio(audio_file_path, prompt, model):
|
|
258 |
# subtitles maker
|
259 |
|
260 |
# helper function convert json transcription to srt
|
|
|
|
|
261 |
def create_srt_from_text(transcription_text):
|
262 |
-
"""Converts Groq text transcription to SRT format (assuming no timestamps)."""
|
263 |
srt_lines = []
|
264 |
-
duration = timedelta(seconds=2) # Adjust the duration as needed
|
265 |
-
text_parts = transcription_text.split(".")
|
266 |
start_time = timedelta(seconds=0)
|
267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
for i, text_part in enumerate(text_parts):
|
269 |
text_part = text_part.strip()
|
270 |
if text_part:
|
|
|
271 |
end_time = start_time + duration
|
272 |
-
|
273 |
-
|
274 |
-
end_timestamp = f"{end_time.seconds:02d}:{end_time.microseconds // 1000:03d}"
|
275 |
srt_lines.append(f"{i + 1}\n{start_timestamp} --> {end_timestamp}\n{text_part.strip()}\n\n")
|
276 |
start_time = end_time # Move to the next time slot
|
|
|
277 |
return "".join(srt_lines)
|
278 |
|
279 |
|
@@ -310,29 +319,15 @@ def generate_subtitles(audio_file_path, prompt, language, auto_detect_language,
|
|
310 |
# Generate subtitles and add to video if input is video
|
311 |
if audio_file_path.lower().endswith((".mp4", ".webm")):
|
312 |
try:
|
313 |
-
# Use ffmpeg to
|
314 |
output_file_path = audio_file_path.replace(os.path.splitext(audio_file_path)[1], "_with_subs" + os.path.splitext(audio_file_path)[1])
|
315 |
subprocess.run(
|
316 |
[
|
317 |
"ffmpeg",
|
318 |
"-i",
|
319 |
audio_file_path,
|
320 |
-
"-
|
321 |
-
temp_srt_path,
|
322 |
-
"-map",
|
323 |
-
"0:v",
|
324 |
-
"-map",
|
325 |
-
"0:a",
|
326 |
-
"-map",
|
327 |
-
"1",
|
328 |
-
"-c:v",
|
329 |
-
"copy",
|
330 |
-
"-c:a",
|
331 |
-
"copy",
|
332 |
-
"-c:s",
|
333 |
-
"mov_text",
|
334 |
-
"-metadata:s:s:0",
|
335 |
-
"language=eng",
|
336 |
output_file_path,
|
337 |
],
|
338 |
check=True,
|
|
|
258 |
# subtitles maker
|
259 |
|
260 |
# helper function convert json transcription to srt
|
261 |
+
from datetime import timedelta
|
262 |
+
|
263 |
def create_srt_from_text(transcription_text):
|
|
|
264 |
srt_lines = []
|
|
|
|
|
265 |
start_time = timedelta(seconds=0)
|
266 |
|
267 |
+
# Define a function to calculate the duration based on text length
|
268 |
+
def calculate_duration(text):
|
269 |
+
words_per_minute = 150 # Average reading speed
|
270 |
+
words = len(text.split())
|
271 |
+
duration_seconds = (words / words_per_minute) * 60
|
272 |
+
return timedelta(seconds=duration_seconds)
|
273 |
+
|
274 |
+
text_parts = transcription_text.split(".")
|
275 |
+
|
276 |
for i, text_part in enumerate(text_parts):
|
277 |
text_part = text_part.strip()
|
278 |
if text_part:
|
279 |
+
duration = calculate_duration(text_part)
|
280 |
end_time = start_time + duration
|
281 |
+
start_timestamp = str(start_time).split('.')[0] + ',' + str(start_time.microseconds // 1000).zfill(3)
|
282 |
+
end_timestamp = str(end_time).split('.')[0] + ',' + str(end_time.microseconds // 1000).zfill(3)
|
|
|
283 |
srt_lines.append(f"{i + 1}\n{start_timestamp} --> {end_timestamp}\n{text_part.strip()}\n\n")
|
284 |
start_time = end_time # Move to the next time slot
|
285 |
+
|
286 |
return "".join(srt_lines)
|
287 |
|
288 |
|
|
|
319 |
# Generate subtitles and add to video if input is video
|
320 |
if audio_file_path.lower().endswith((".mp4", ".webm")):
|
321 |
try:
|
322 |
+
# Use ffmpeg to burn subtitles into the video
|
323 |
output_file_path = audio_file_path.replace(os.path.splitext(audio_file_path)[1], "_with_subs" + os.path.splitext(audio_file_path)[1])
|
324 |
subprocess.run(
|
325 |
[
|
326 |
"ffmpeg",
|
327 |
"-i",
|
328 |
audio_file_path,
|
329 |
+
"-vf",
|
330 |
+
f"subtitles={temp_srt_path}",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
331 |
output_file_path,
|
332 |
],
|
333 |
check=True,
|