Spaces:
Runtime error
Runtime error
import gradio as gr | |
import whisper | |
import os | |
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip | |
# Load the Whisper model | |
model = whisper.load_model("base") # Choose 'tiny', 'base', 'small', 'medium', or 'large' | |
def transcribe_video(video_file): | |
# Transcribe the video to generate subtitles | |
result = model.transcribe(video_file) | |
# Create a list of (start_time, end_time, text) tuples for subtitles | |
subtitles = [(segment['start'], segment['end'], segment['text'].strip()) for segment in result['segments']] | |
# Create a subtitled video | |
subtitled_video_file = create_subtitled_video(video_file, subtitles) | |
return subtitled_video_file | |
def create_subtitled_video(video_file, subtitles): | |
# Load the original video | |
video = VideoFileClip(video_file) | |
# Create a list of TextClips for each subtitle | |
text_clips = [] | |
for start, end, text in subtitles: | |
text_clip = (TextClip(text, fontsize=24, color='white', bg_color='black', size=video.size) | |
.set_start(start) | |
.set_duration(end - start) | |
.set_position(('center', 'bottom'))) # Position the subtitle at the bottom center | |
text_clips.append(text_clip) | |
# Overlay the subtitles on the video | |
final_video = CompositeVideoClip([video] + text_clips) | |
# Save the final video | |
subtitled_video_file = "subtitled_video.mp4" | |
final_video.write_videofile(subtitled_video_file, codec='libx264', audio_codec='aac') | |
return subtitled_video_file | |
# Gradio interface | |
iface = gr.Interface( | |
fn=transcribe_video, | |
inputs=gr.File(label="Upload Video"), | |
outputs=gr.File(label="Download Subtitled Video"), | |
title="Video Subtitle Generator", | |
description="Upload a video file to generate a subtitled video using Whisper." | |
) | |
if __name__ == "__main__": | |
iface.launch() | |