Spaces:
Running
Running
import gradio as gr | |
from moviepy.editor import VideoFileClip | |
import os | |
def extract_audio(video_path): | |
try: | |
video = VideoFileClip(video_path) | |
audio_path = 'audio.mp3' | |
audio = video.audio | |
audio.write_audiofile(audio_path) | |
audio.close() | |
video.close() | |
return audio_path | |
except Exception as e: | |
return str(e) | |
def transcribe_audio_to_srt(audio_path, srt_file=out_put.srt): | |
model = whisper.load_model("base.en") | |
result = model.transcribe(audio_path) | |
# Create a list to hold subtitle entries | |
subtitles = [] | |
start_time = 0.0 | |
for i, segment in enumerate(result['segments']): | |
start_time = segment['start'] | |
end_time = segment['end'] | |
content = segment['text'].strip() | |
# Create a subtitle entry | |
subtitle = srt.Subtitle(index=i+1, | |
start=timedelta(seconds=start_time), | |
end=timedelta(seconds=end_time), | |
content=content) | |
subtitles.append(subtitle) | |
# Write the subtitles to an SRT file | |
with open(srt_file, 'w', encoding='utf-8') as f: | |
f.write(srt.compose(subtitles)) | |
return srt_file | |
def process_video(video): | |
video_path = os.path.join("uploads", video.name) | |
video.save(video_path) | |
audio_path = extract_audio(video_path) | |
processed_audio_path = process_audio(audio_path) | |
with open(processed_audio_path, "r") as f: | |
srt_content = f.read() | |
return srt_content | |
iface = gr.Interface( | |
fn=process_video, | |
inputs=gr.Video(type="file"), | |
outputs=gr.Textbox(label="Generated SRT File Content"), | |
title="Extract and Process Audio from Video", | |
description="Upload a video file to extract and process the audio, and view the generated SRT file content.", | |
allow_flagging="never" | |
iface.launch() | |