Commit ·
beae951
1
Parent(s): 67a18a1
Fix file type validation and improve UI
Browse files
utils.py
CHANGED
|
@@ -7,26 +7,23 @@ import subprocess
|
|
| 7 |
# Load Whisper model
|
| 8 |
model = whisper.load_model("base")
|
| 9 |
|
| 10 |
-
def process_video(
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
video_path = os.path.join(temp_dir, "input_video") # No extension
|
| 14 |
-
output_video_path = os.path.join(temp_dir, "converted_video.mp4") # Convert to MP4 for compatibility
|
| 15 |
|
| 16 |
try:
|
| 17 |
-
#
|
| 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(
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# Transcribe
|
| 29 |
-
print("Transcribing video
|
| 30 |
result = model.transcribe(output_video_path, language="en")
|
| 31 |
print("Transcription completed!")
|
| 32 |
|
|
@@ -35,7 +32,6 @@ def process_video(video_file, language):
|
|
| 35 |
if language == "English":
|
| 36 |
segments = result["segments"]
|
| 37 |
else:
|
| 38 |
-
# Define translation models
|
| 39 |
model_map = {
|
| 40 |
"Hindi": "Helsinki-NLP/opus-mt-en-hi",
|
| 41 |
"Spanish": "Helsinki-NLP/opus-mt-en-es",
|
|
@@ -52,12 +48,11 @@ def process_video(video_file, language):
|
|
| 52 |
if not model_name:
|
| 53 |
return f"Unsupported language: {language}"
|
| 54 |
|
| 55 |
-
print(f"Loading translation model
|
| 56 |
if language == "Telugu":
|
| 57 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 58 |
translation_model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 59 |
tgt_lang = "tel_Telu"
|
| 60 |
-
print(f"Translating to Telugu using NLLB-200 Distilled...")
|
| 61 |
for segment in result["segments"]:
|
| 62 |
inputs = tokenizer(segment["text"], return_tensors="pt", padding=True)
|
| 63 |
translated_tokens = translation_model.generate(
|
|
@@ -68,7 +63,6 @@ def process_video(video_file, language):
|
|
| 68 |
else:
|
| 69 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 70 |
translation_model = MarianMTModel.from_pretrained(model_name)
|
| 71 |
-
print(f"Translating to {language}...")
|
| 72 |
for segment in result["segments"]:
|
| 73 |
inputs = tokenizer(segment["text"], return_tensors="pt", padding=True)
|
| 74 |
translated = translation_model.generate(**inputs)
|
|
@@ -76,8 +70,6 @@ def process_video(video_file, language):
|
|
| 76 |
segments.append({"text": translated_text, "start": segment["start"], "end": segment["end"]})
|
| 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(".", ",")
|
|
@@ -88,13 +80,14 @@ def process_video(video_file, language):
|
|
| 88 |
return srt_path
|
| 89 |
|
| 90 |
except subprocess.CalledProcessError as e:
|
| 91 |
-
|
|
|
|
| 92 |
except Exception as e:
|
| 93 |
-
|
|
|
|
| 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):
|
| 100 |
-
os.remove(output_video_path)
|
|
|
|
|
|
|
|
|
| 7 |
# Load Whisper model
|
| 8 |
model = whisper.load_model("base")
|
| 9 |
|
| 10 |
+
def process_video(video_path, language): # Accept file path, not file object
|
| 11 |
+
output_video_path = os.path.join(tempfile.gettempdir(), "converted_video.mp4")
|
| 12 |
+
srt_path = os.path.join(tempfile.gettempdir(), "subtitles.srt")
|
|
|
|
|
|
|
| 13 |
|
| 14 |
try:
|
| 15 |
+
# Convert video to MP4 using ffmpeg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
print("Converting video to MP4...")
|
| 17 |
+
subprocess.run(
|
| 18 |
+
["ffmpeg", "-i", video_path, "-c:v", "libx264", "-preset", "fast", output_video_path],
|
| 19 |
+
check=True, # Raise error if ffmpeg fails
|
| 20 |
+
stdout=subprocess.PIPE,
|
| 21 |
+
stderr=subprocess.PIPE
|
| 22 |
+
)
|
| 23 |
+
print("Video converted successfully!")
|
| 24 |
|
| 25 |
+
# Transcribe video
|
| 26 |
+
print("Transcribing video...")
|
| 27 |
result = model.transcribe(output_video_path, language="en")
|
| 28 |
print("Transcription completed!")
|
| 29 |
|
|
|
|
| 32 |
if language == "English":
|
| 33 |
segments = result["segments"]
|
| 34 |
else:
|
|
|
|
| 35 |
model_map = {
|
| 36 |
"Hindi": "Helsinki-NLP/opus-mt-en-hi",
|
| 37 |
"Spanish": "Helsinki-NLP/opus-mt-en-es",
|
|
|
|
| 48 |
if not model_name:
|
| 49 |
return f"Unsupported language: {language}"
|
| 50 |
|
| 51 |
+
print(f"Loading translation model: {model_name}")
|
| 52 |
if language == "Telugu":
|
| 53 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 54 |
translation_model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 55 |
tgt_lang = "tel_Telu"
|
|
|
|
| 56 |
for segment in result["segments"]:
|
| 57 |
inputs = tokenizer(segment["text"], return_tensors="pt", padding=True)
|
| 58 |
translated_tokens = translation_model.generate(
|
|
|
|
| 63 |
else:
|
| 64 |
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 65 |
translation_model = MarianMTModel.from_pretrained(model_name)
|
|
|
|
| 66 |
for segment in result["segments"]:
|
| 67 |
inputs = tokenizer(segment["text"], return_tensors="pt", padding=True)
|
| 68 |
translated = translation_model.generate(**inputs)
|
|
|
|
| 70 |
segments.append({"text": translated_text, "start": segment["start"], "end": segment["end"]})
|
| 71 |
|
| 72 |
# Create SRT file
|
|
|
|
|
|
|
| 73 |
with open(srt_path, "w", encoding="utf-8") as f:
|
| 74 |
for i, segment in enumerate(segments, 1):
|
| 75 |
start = f"{segment['start']:.3f}".replace(".", ",")
|
|
|
|
| 80 |
return srt_path
|
| 81 |
|
| 82 |
except subprocess.CalledProcessError as e:
|
| 83 |
+
print(f"FFmpeg Error: {e.stderr.decode()}")
|
| 84 |
+
return None
|
| 85 |
except Exception as e:
|
| 86 |
+
print(f"Unexpected Error: {str(e)}")
|
| 87 |
+
return None
|
| 88 |
finally:
|
| 89 |
# Clean up temporary files
|
|
|
|
|
|
|
|
|
|
| 90 |
if os.path.exists(output_video_path):
|
| 91 |
+
os.remove(output_video_path)
|
| 92 |
+
if os.path.exists(video_path):
|
| 93 |
+
os.remove(video_path)
|