Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +57 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import whisper
|
3 |
+
import moviepy.editor as mp
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# Load Whisper model for audio transcription
|
7 |
+
model_name = "base"
|
8 |
+
whisper_model = whisper.load_model(model_name)
|
9 |
+
|
10 |
+
def generate_srt(transcription_result):
|
11 |
+
"""Helper function to convert Whisper's transcription with timestamps to SRT format."""
|
12 |
+
srt_content = ""
|
13 |
+
for i, segment in enumerate(transcription_result['segments']):
|
14 |
+
# Convert start and end times to SRT time format
|
15 |
+
start = segment['start']
|
16 |
+
end = segment['end']
|
17 |
+
start_time = f"{int(start//3600):02}:{int((start%3600)//60):02}:{int(start%60):02},{int((start%1)*1000):03}"
|
18 |
+
end_time = f"{int(end//3600):02}:{int((end%3600)//60):02}:{int(end%60):02},{int((end%1)*1000):03}"
|
19 |
+
|
20 |
+
# Create the SRT entry
|
21 |
+
srt_content += f"{i+1}\n{start_time} --> {end_time}\n{segment['text'].strip()}\n\n"
|
22 |
+
|
23 |
+
return srt_content
|
24 |
+
|
25 |
+
def transcribe_and_generate_subtitles(video):
|
26 |
+
# Step 1: Extract the audio from the video using moviepy
|
27 |
+
video_clip = mp.VideoFileClip(video)
|
28 |
+
audio_path = "temp_audio.wav"
|
29 |
+
video_clip.audio.write_audiofile(audio_path, codec='pcm_s16le')
|
30 |
+
|
31 |
+
# Step 2: Transcribe the audio with timestamps using Whisper
|
32 |
+
transcription_result = whisper_model.transcribe(audio_path, task="transcribe", language="en", verbose=False)
|
33 |
+
|
34 |
+
# Step 3: Generate SRT subtitles
|
35 |
+
srt_content = generate_srt(transcription_result)
|
36 |
+
|
37 |
+
# Save SRT file
|
38 |
+
srt_file = "output_subtitles.srt"
|
39 |
+
with open(srt_file, "w", encoding="utf-8") as f:
|
40 |
+
f.write(srt_content)
|
41 |
+
|
42 |
+
return transcription_result["text"], srt_file
|
43 |
+
|
44 |
+
# Set up Gradio interface
|
45 |
+
interface = gr.Interface(
|
46 |
+
fn=transcribe_and_generate_subtitles,
|
47 |
+
inputs=gr.Video(label="Upload Video File"),
|
48 |
+
outputs=[
|
49 |
+
gr.Textbox(label="English Transcription"),
|
50 |
+
gr.File(label="Subtitle File (SRT)")
|
51 |
+
],
|
52 |
+
title="Video Subtitle Generator",
|
53 |
+
description="Upload a video file, and this will generate a transcription and subtitles in SRT format."
|
54 |
+
)
|
55 |
+
|
56 |
+
# Launch the interface
|
57 |
+
interface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
whisper
|
3 |
+
moviepy
|
4 |
+
gradio
|
5 |
+
torch
|