abdullah637 commited on
Commit
092da9f
·
verified ·
1 Parent(s): e3e9acd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import whisper
2
+ model = whisper.load_model("base")
3
+
4
+ import gradio as gr
5
+ from moviepy.editor import VideoFileClip
6
+ import os
7
+
8
+ def generate_subtitles(video_path):
9
+ # Extract audio from video
10
+ video = VideoFileClip(video_path)
11
+ audio_path = "temp_audio.wav"
12
+ video.audio.write_audiofile(audio_path)
13
+
14
+ # Transcribe audio using Whisper
15
+ result = model.transcribe(audio_path)
16
+
17
+ # Clean up temporary audio file
18
+ os.remove(audio_path)
19
+
20
+ # Format the subtitles in .srt format
21
+ srt_content = ""
22
+ for index, segment in enumerate(result['segments']):
23
+ start_time = segment['start']
24
+ end_time = segment['end']
25
+ text = segment['text']
26
+
27
+ # Convert seconds to SRT time format (HH:MM:SS,mmm)
28
+ def format_time(seconds):
29
+ hours = int(seconds // 3600)
30
+ minutes = int((seconds % 3600) // 60)
31
+ seconds = seconds % 60
32
+ milliseconds = int((seconds - int(seconds)) * 1000)
33
+ return f"{hours:02}:{minutes:02}:{int(seconds):02},{milliseconds:03}"
34
+
35
+ srt_content += f"{index + 1}\n"
36
+ srt_content += f"{format_time(start_time)} --> {format_time(end_time)}\n"
37
+ srt_content += f"{text}\n\n"
38
+
39
+ # Save the .srt file
40
+ srt_filename = "subtitles.srt"
41
+ with open(srt_filename, "w", encoding="utf-8") as srt_file:
42
+ srt_file.write(srt_content)
43
+
44
+ return srt_content, srt_filename