Pushkar0655g commited on
Commit
9ed77ee
·
verified ·
1 Parent(s): a360176

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +22 -19
utils.py CHANGED
@@ -7,16 +7,23 @@ import subprocess
7
  # Load Whisper model
8
  model = whisper.load_model("base")
9
 
10
- def process_video(video_file, language):
11
- # Save uploaded video to a temp
 
12
 
13
- # Convert the video to MP4 using ffmpeg
 
14
  print("Converting video to MP4...")
15
- subprocess.run(["ffmpeg", "-i", video_path, "-c:v", "libx264", "-preset", "fast", output_video_path], check=True)
16
- print(f"Video converted and saved to {output_video_path}")
 
 
 
 
 
17
 
18
- # Transcribe the video
19
- print("Transcribing video to English...")
20
  result = model.transcribe(output_video_path, language="en")
21
  print("Transcription completed!")
22
 
@@ -25,7 +32,6 @@ def process_video(video_file, language):
25
  if language == "English":
26
  segments = result["segments"]
27
  else:
28
- # Define translation models
29
  model_map = {
30
  "Hindi": "Helsinki-NLP/opus-mt-en-hi",
31
  "Spanish": "Helsinki-NLP/opus-mt-en-es",
@@ -42,12 +48,11 @@ def process_video(video_file, language):
42
  if not model_name:
43
  return f"Unsupported language: {language}"
44
 
45
- print(f"Loading translation model for {language}: {model_name}")
46
  if language == "Telugu":
47
  tokenizer = AutoTokenizer.from_pretrained(model_name)
48
  translation_model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
49
  tgt_lang = "tel_Telu"
50
- print(f"Translating to Telugu using NLLB-200 Distilled...")
51
  for segment in result["segments"]:
52
  inputs = tokenizer(segment["text"], return_tensors="pt", padding=True)
53
  translated_tokens = translation_model.generate(
@@ -58,7 +63,6 @@ def process_video(video_file, language):
58
  else:
59
  tokenizer = MarianTokenizer.from_pretrained(model_name)
60
  translation_model = MarianMTModel.from_pretrained(model_name)
61
- print(f"Translating to {language}...")
62
  for segment in result["segments"]:
63
  inputs = tokenizer(segment["text"], return_tensors="pt", padding=True)
64
  translated = translation_model.generate(**inputs)
@@ -66,8 +70,6 @@ def process_video(video_file, language):
66
  segments.append({"text": translated_text, "start": segment["start"], "end": segment["end"]})
67
 
68
  # Create SRT file
69
- srt_path = os.path.join(tempfile.gettempdir(), "subtitles.srt")
70
- print(f"Creating SRT file at {srt_path}")
71
  with open(srt_path, "w", encoding="utf-8") as f:
72
  for i, segment in enumerate(segments, 1):
73
  start = f"{segment['start']:.3f}".replace(".", ",")
@@ -78,13 +80,14 @@ def process_video(video_file, language):
78
  return srt_path
79
 
80
  except subprocess.CalledProcessError as e:
81
- return f"FFmpeg Error: {str(e)}"
 
82
  except Exception as e:
83
- return f"Unexpected Error: {str(e)}"
 
84
  finally:
85
  # Clean up temporary files
86
- print("Cleaning up temporary files...")
87
- if os.path.exists(video_path):
88
- os.remove(video_path)
89
  if os.path.exists(output_video_path):
90
- 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)