Pushkar0655g commited on
Commit
7371a9e
Β·
verified Β·
1 Parent(s): 2670e49

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +14 -9
utils.py CHANGED
@@ -7,25 +7,27 @@ import subprocess
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
 
30
  # Translation logic
31
  segments = []
@@ -46,9 +48,9 @@ def process_video(video_path, language): # Accept file path, not file object
46
  }
47
  model_name = model_map.get(language)
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)
@@ -70,13 +72,16 @@ def process_video(video_path, language): # Accept file path, not file object
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(".", ",")
76
  end = f"{segment['end']:.3f}".replace(".", ",")
77
  text = segment["text"].strip()
78
  f.write(f"{i}\n00:00:{start} --> 00:00:{end}\n{text}\n\n")
79
- print("SRT file created successfully!")
 
80
  return srt_path
81
 
82
  except subprocess.CalledProcessError as e:
 
7
  # Load Whisper model
8
  model = whisper.load_model("base")
9
 
10
+ def process_video(video_path, language, progress=None):
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
+ if progress:
17
+ progress(0.2, desc="πŸ”„ Converting video to MP4...")
18
  subprocess.run(
19
  ["ffmpeg", "-i", video_path, "-c:v", "libx264", "-preset", "fast", output_video_path],
20
+ check=True,
21
  stdout=subprocess.PIPE,
22
  stderr=subprocess.PIPE
23
  )
 
24
 
25
  # Transcribe video
26
+ if progress:
27
+ progress(0.4, desc="πŸ“ Transcribing audio...")
28
  result = model.transcribe(output_video_path, language="en")
29
+ if progress:
30
+ progress(0.6, desc="🌐 Translating subtitles...")
31
 
32
  # Translation logic
33
  segments = []
 
48
  }
49
  model_name = model_map.get(language)
50
  if not model_name:
51
+ return None
52
 
53
+ # Load translation model
54
  if language == "Telugu":
55
  tokenizer = AutoTokenizer.from_pretrained(model_name)
56
  translation_model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
 
72
  segments.append({"text": translated_text, "start": segment["start"], "end": segment["end"]})
73
 
74
  # Create SRT file
75
+ if progress:
76
+ progress(0.8, desc="πŸ“ Generating SRT file...")
77
  with open(srt_path, "w", encoding="utf-8") as f:
78
  for i, segment in enumerate(segments, 1):
79
  start = f"{segment['start']:.3f}".replace(".", ",")
80
  end = f"{segment['end']:.3f}".replace(".", ",")
81
  text = segment["text"].strip()
82
  f.write(f"{i}\n00:00:{start} --> 00:00:{end}\n{text}\n\n")
83
+ if progress:
84
+ progress(1.0, desc="βœ… Done!")
85
  return srt_path
86
 
87
  except subprocess.CalledProcessError as e: