Hasnain-Ali commited on
Commit
807eb1d
·
verified ·
1 Parent(s): 6cb8cfc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -4,35 +4,36 @@ import os
4
  import tempfile
5
  import subprocess
6
  import srt
 
7
 
8
-
9
-
10
- # Load Whisper model
11
  @st.cache_resource
12
  def load_model():
13
- return whisper.load_model("small") # Use "small" model for free usage
14
 
15
- # Function to transcribe video
16
  def transcribe_video(video_path):
17
  model = load_model()
18
  result = model.transcribe(video_path)
19
  return result["segments"]
20
 
21
- # Function to create SRT file
22
  def create_srt(subtitles, srt_path):
23
  subs = []
24
  for i, segment in enumerate(subtitles):
 
 
 
25
  subs.append(srt.Subtitle(
26
  index=i + 1,
27
- start=srt.srt_timestamp_to_timedelta(segment["start"]),
28
- end=srt.srt_timestamp_to_timedelta(segment["end"]),
29
  content=segment["text"]
30
  ))
31
-
32
  with open(srt_path, "w", encoding="utf-8") as f:
33
  f.write(srt.compose(subs))
34
 
35
- # Function to burn captions into video
36
  def burn_captions(video_path, srt_path, output_path):
37
  command = [
38
  "ffmpeg",
@@ -43,13 +44,13 @@ def burn_captions(video_path, srt_path, output_path):
43
  ]
44
  subprocess.run(command, check=True)
45
 
46
- # Streamlit UI
47
  st.title("🎥 AI Video Captioning App")
48
 
49
  uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "mkv", "avi", "mov"])
50
 
51
  if uploaded_file:
52
- # Save uploaded video
53
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
54
  temp_video.write(uploaded_file.read())
55
  video_path = temp_video.name
 
4
  import tempfile
5
  import subprocess
6
  import srt
7
+ import datetime
8
 
9
+ # Load Whisper model (using cache for performance)
 
 
10
  @st.cache_resource
11
  def load_model():
12
+ return whisper.load_model("small") # Use the small model for free usage
13
 
14
+ # Transcribe video using Whisper
15
  def transcribe_video(video_path):
16
  model = load_model()
17
  result = model.transcribe(video_path)
18
  return result["segments"]
19
 
20
+ # Create an SRT file from the transcription segments
21
  def create_srt(subtitles, srt_path):
22
  subs = []
23
  for i, segment in enumerate(subtitles):
24
+ # Convert float seconds to timedelta objects
25
+ start_time = datetime.timedelta(seconds=segment["start"])
26
+ end_time = datetime.timedelta(seconds=segment["end"])
27
  subs.append(srt.Subtitle(
28
  index=i + 1,
29
+ start=start_time,
30
+ end=end_time,
31
  content=segment["text"]
32
  ))
 
33
  with open(srt_path, "w", encoding="utf-8") as f:
34
  f.write(srt.compose(subs))
35
 
36
+ # Burn subtitles into the video using FFmpeg
37
  def burn_captions(video_path, srt_path, output_path):
38
  command = [
39
  "ffmpeg",
 
44
  ]
45
  subprocess.run(command, check=True)
46
 
47
+ # Streamlit app UI
48
  st.title("🎥 AI Video Captioning App")
49
 
50
  uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "mkv", "avi", "mov"])
51
 
52
  if uploaded_file:
53
+ # Save the uploaded video to a temporary file
54
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
55
  temp_video.write(uploaded_file.read())
56
  video_path = temp_video.name