|
import gradio as gr |
|
from moviepy.editor import VideoFileClip |
|
import os |
|
import whisper |
|
import srt |
|
from datetime import timedelta |
|
|
|
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="output.srt"): |
|
model = whisper.load_model("base.en") |
|
result = model.transcribe(audio_path) |
|
subtitles = [] |
|
for i, segment in enumerate(result['segments']): |
|
start_time = segment['start'] |
|
end_time = segment['end'] |
|
content = segment['text'].strip() |
|
subtitle = srt.Subtitle(index=i+1, |
|
start=timedelta(seconds=start_time), |
|
end=timedelta(seconds=end_time), |
|
content=content) |
|
subtitles.append(subtitle) |
|
with open(srt_file, 'w', encoding='utf-8') as f: |
|
f.write(srt.compose(subtitles)) |
|
return srt_file |
|
|
|
def process_video(video): |
|
video_path = video |
|
audio_path = extract_audio(video_path) |
|
if audio_path.endswith('.mp3'): |
|
processed_audio_path = transcribe_audio_to_srt(audio_path) |
|
with open(processed_audio_path, "r") as f: |
|
srt_content = f.read() |
|
return srt_content |
|
else: |
|
return "Failed to extract audio." |
|
|
|
iface = gr.Interface( |
|
fn=process_video, |
|
inputs=gr.Video(), |
|
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() |
|
|