pratikshahp
commited on
Commit
•
489d8c3
1
Parent(s):
d58b8c0
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from moviepy.editor import VideoFileClip
|
3 |
+
from transformers import pipeline
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Initialize the Whisper model
|
7 |
+
whisper_model = pipeline("automatic-speech-recognition", model="openai/whisper-large")
|
8 |
+
|
9 |
+
def convert_video_to_wav(video_path):
|
10 |
+
# Extract audio from video using moviepy and save as WAV
|
11 |
+
video_clip = VideoFileClip(video_path)
|
12 |
+
audio = video_clip.audio
|
13 |
+
wav_file = "temp_audio.wav"
|
14 |
+
audio.write_audiofile(wav_file, codec='pcm_s16le') # Write as WAV format
|
15 |
+
return wav_file
|
16 |
+
|
17 |
+
def convert_audio_to_srt(wav_file):
|
18 |
+
# Transcribe the audio using the Whisper model
|
19 |
+
transcription = whisper_model(wav_file)
|
20 |
+
|
21 |
+
# Save the transcription to an SRT file with simple formatting
|
22 |
+
srt_file = "transcription.srt"
|
23 |
+
with open(srt_file, "w", encoding="utf-8") as f:
|
24 |
+
for i, segment in enumerate(transcription['text'].split('.')):
|
25 |
+
f.write(f"{i+1}\n") # Subtitle index
|
26 |
+
f.write(f"00:00:{i*2:02d},000 --> 00:00:{i*2+2:02d},000\n") # Timestamp (basic)
|
27 |
+
f.write(f"{segment.strip()}\n\n") # Transcription text
|
28 |
+
|
29 |
+
# Clean up temp audio file
|
30 |
+
os.remove(wav_file)
|
31 |
+
|
32 |
+
return srt_file
|
33 |
+
|
34 |
+
def process_video(video):
|
35 |
+
# Save the uploaded video file to a temporary location
|
36 |
+
video_path = video.name
|
37 |
+
|
38 |
+
# Process the video to extract audio and convert to srt
|
39 |
+
wav_file = convert_video_to_wav(video_path) # Convert video to WAV
|
40 |
+
srt_file = convert_audio_to_srt(wav_file) # Convert WAV to SRT
|
41 |
+
|
42 |
+
return srt_file # Return the path of the generated SRT file
|
43 |
+
|
44 |
+
# Gradio Interface
|
45 |
+
interface = gr.Interface(
|
46 |
+
fn=process_video,
|
47 |
+
inputs=gr.File(label="Upload video file", file_types=['mp4', 'avi', 'mkv']), # Video file input
|
48 |
+
outputs=gr.File(label="Download SRT File"), # Output the SRT file for download
|
49 |
+
title="Video to SRT Subtitle Generator",
|
50 |
+
description="Upload a video file (e.g., .mp4), and the app will generate a subtitle file (SRT format) using Whisper model."
|
51 |
+
)
|
52 |
+
|
53 |
+
interface.launch()
|