Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import gradio as gr
|
|
| 2 |
from moviepy.editor import VideoFileClip
|
| 3 |
import os
|
| 4 |
import whisper
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def extract_audio(video_path):
|
| 7 |
try:
|
|
@@ -15,23 +17,19 @@ def extract_audio(video_path):
|
|
| 15 |
except Exception as e:
|
| 16 |
return str(e)
|
| 17 |
|
| 18 |
-
def transcribe_audio_to_srt(audio_path, srt_file="
|
| 19 |
model = whisper.load_model("base.en")
|
| 20 |
result = model.transcribe(audio_path)
|
| 21 |
-
# Create a list to hold subtitle entries
|
| 22 |
subtitles = []
|
| 23 |
-
start_time = 0.0
|
| 24 |
for i, segment in enumerate(result['segments']):
|
| 25 |
start_time = segment['start']
|
| 26 |
end_time = segment['end']
|
| 27 |
content = segment['text'].strip()
|
| 28 |
-
# Create a subtitle entry
|
| 29 |
subtitle = srt.Subtitle(index=i+1,
|
| 30 |
start=timedelta(seconds=start_time),
|
| 31 |
end=timedelta(seconds=end_time),
|
| 32 |
content=content)
|
| 33 |
subtitles.append(subtitle)
|
| 34 |
-
# Write the subtitles to an SRT file
|
| 35 |
with open(srt_file, 'w', encoding='utf-8') as f:
|
| 36 |
f.write(srt.compose(subtitles))
|
| 37 |
return srt_file
|
|
@@ -39,10 +37,13 @@ def transcribe_audio_to_srt(audio_path, srt_file="out_put.srt"):
|
|
| 39 |
def process_video(video):
|
| 40 |
video_path = video
|
| 41 |
audio_path = extract_audio(video_path)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=process_video,
|
|
|
|
| 2 |
from moviepy.editor import VideoFileClip
|
| 3 |
import os
|
| 4 |
import whisper
|
| 5 |
+
import srt
|
| 6 |
+
from datetime import timedelta
|
| 7 |
|
| 8 |
def extract_audio(video_path):
|
| 9 |
try:
|
|
|
|
| 17 |
except Exception as e:
|
| 18 |
return str(e)
|
| 19 |
|
| 20 |
+
def transcribe_audio_to_srt(audio_path, srt_file="output.srt"):
|
| 21 |
model = whisper.load_model("base.en")
|
| 22 |
result = model.transcribe(audio_path)
|
|
|
|
| 23 |
subtitles = []
|
|
|
|
| 24 |
for i, segment in enumerate(result['segments']):
|
| 25 |
start_time = segment['start']
|
| 26 |
end_time = segment['end']
|
| 27 |
content = segment['text'].strip()
|
|
|
|
| 28 |
subtitle = srt.Subtitle(index=i+1,
|
| 29 |
start=timedelta(seconds=start_time),
|
| 30 |
end=timedelta(seconds=end_time),
|
| 31 |
content=content)
|
| 32 |
subtitles.append(subtitle)
|
|
|
|
| 33 |
with open(srt_file, 'w', encoding='utf-8') as f:
|
| 34 |
f.write(srt.compose(subtitles))
|
| 35 |
return srt_file
|
|
|
|
| 37 |
def process_video(video):
|
| 38 |
video_path = video
|
| 39 |
audio_path = extract_audio(video_path)
|
| 40 |
+
if audio_path.endswith('.mp3'):
|
| 41 |
+
processed_audio_path = transcribe_audio_to_srt(audio_path)
|
| 42 |
+
with open(processed_audio_path, "r") as f:
|
| 43 |
+
srt_content = f.read()
|
| 44 |
+
return srt_content
|
| 45 |
+
else:
|
| 46 |
+
return "Failed to extract audio."
|
| 47 |
|
| 48 |
iface = gr.Interface(
|
| 49 |
fn=process_video,
|