Commit
·
67a18a1
1
Parent(s):
91b987a
Add detailed logging to debug subtitle generation errors
Browse files
utils.py
CHANGED
|
@@ -15,16 +15,20 @@ def process_video(video_file, language):
|
|
| 15 |
|
| 16 |
try:
|
| 17 |
# Save the uploaded file
|
|
|
|
| 18 |
with open(video_path, "wb") as f:
|
| 19 |
f.write(video_file.read())
|
|
|
|
| 20 |
|
| 21 |
# Convert the video to MP4 using ffmpeg
|
| 22 |
print("Converting video to MP4...")
|
| 23 |
subprocess.run(["ffmpeg", "-i", video_path, "-c:v", "libx264", "-preset", "fast", output_video_path], check=True)
|
|
|
|
| 24 |
|
| 25 |
# Transcribe the video
|
| 26 |
print("Transcribing video to English...")
|
| 27 |
result = model.transcribe(output_video_path, language="en")
|
|
|
|
| 28 |
|
| 29 |
# Translation logic
|
| 30 |
segments = []
|
|
@@ -48,6 +52,7 @@ def process_video(video_file, language):
|
|
| 48 |
if not model_name:
|
| 49 |
return f"Unsupported language: {language}"
|
| 50 |
|
|
|
|
| 51 |
if language == "Telugu":
|
| 52 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 53 |
translation_model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
|
@@ -72,18 +77,23 @@ def process_video(video_file, language):
|
|
| 72 |
|
| 73 |
# Create SRT file
|
| 74 |
srt_path = os.path.join(tempfile.gettempdir(), "subtitles.srt")
|
|
|
|
| 75 |
with open(srt_path, "w", encoding="utf-8") as f:
|
| 76 |
for i, segment in enumerate(segments, 1):
|
| 77 |
start = f"{segment['start']:.3f}".replace(".", ",")
|
| 78 |
end = f"{segment['end']:.3f}".replace(".", ",")
|
| 79 |
text = segment["text"].strip()
|
| 80 |
f.write(f"{i}\n00:00:{start} --> 00:00:{end}\n{text}\n\n")
|
|
|
|
| 81 |
return srt_path
|
| 82 |
|
|
|
|
|
|
|
| 83 |
except Exception as e:
|
| 84 |
-
return f"Error: {str(e)}"
|
| 85 |
finally:
|
| 86 |
# Clean up temporary files
|
|
|
|
| 87 |
if os.path.exists(video_path):
|
| 88 |
os.remove(video_path)
|
| 89 |
if os.path.exists(output_video_path):
|
|
|
|
| 15 |
|
| 16 |
try:
|
| 17 |
# Save the uploaded file
|
| 18 |
+
print("Saving uploaded video...")
|
| 19 |
with open(video_path, "wb") as f:
|
| 20 |
f.write(video_file.read())
|
| 21 |
+
print(f"Video saved to {video_path}")
|
| 22 |
|
| 23 |
# Convert the video to MP4 using ffmpeg
|
| 24 |
print("Converting video to MP4...")
|
| 25 |
subprocess.run(["ffmpeg", "-i", video_path, "-c:v", "libx264", "-preset", "fast", output_video_path], check=True)
|
| 26 |
+
print(f"Video converted and saved to {output_video_path}")
|
| 27 |
|
| 28 |
# Transcribe the video
|
| 29 |
print("Transcribing video to English...")
|
| 30 |
result = model.transcribe(output_video_path, language="en")
|
| 31 |
+
print("Transcription completed!")
|
| 32 |
|
| 33 |
# Translation logic
|
| 34 |
segments = []
|
|
|
|
| 52 |
if not model_name:
|
| 53 |
return f"Unsupported language: {language}"
|
| 54 |
|
| 55 |
+
print(f"Loading translation model for {language}: {model_name}")
|
| 56 |
if language == "Telugu":
|
| 57 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 58 |
translation_model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
|
|
|
| 77 |
|
| 78 |
# Create SRT file
|
| 79 |
srt_path = os.path.join(tempfile.gettempdir(), "subtitles.srt")
|
| 80 |
+
print(f"Creating SRT file at {srt_path}")
|
| 81 |
with open(srt_path, "w", encoding="utf-8") as f:
|
| 82 |
for i, segment in enumerate(segments, 1):
|
| 83 |
start = f"{segment['start']:.3f}".replace(".", ",")
|
| 84 |
end = f"{segment['end']:.3f}".replace(".", ",")
|
| 85 |
text = segment["text"].strip()
|
| 86 |
f.write(f"{i}\n00:00:{start} --> 00:00:{end}\n{text}\n\n")
|
| 87 |
+
print("SRT file created successfully!")
|
| 88 |
return srt_path
|
| 89 |
|
| 90 |
+
except subprocess.CalledProcessError as e:
|
| 91 |
+
return f"FFmpeg Error: {str(e)}"
|
| 92 |
except Exception as e:
|
| 93 |
+
return f"Unexpected Error: {str(e)}"
|
| 94 |
finally:
|
| 95 |
# Clean up temporary files
|
| 96 |
+
print("Cleaning up temporary files...")
|
| 97 |
if os.path.exists(video_path):
|
| 98 |
os.remove(video_path)
|
| 99 |
if os.path.exists(output_video_path):
|