Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import whisper
|
| 3 |
+
import tempfile
|
| 4 |
+
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def generate_subtitles(video, language="hi"):
|
| 8 |
+
if video is None:
|
| 9 |
+
return "Please upload a video first!"
|
| 10 |
+
|
| 11 |
+
# Temporary file for video
|
| 12 |
+
temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 13 |
+
temp_video.write(video.read())
|
| 14 |
+
temp_video.close()
|
| 15 |
+
|
| 16 |
+
# Load whisper model
|
| 17 |
+
model = whisper.load_model("small")
|
| 18 |
+
result = model.transcribe(temp_video.name, language=language)
|
| 19 |
+
|
| 20 |
+
# Process video
|
| 21 |
+
video_clip = VideoFileClip(temp_video.name)
|
| 22 |
+
subtitle_clips = []
|
| 23 |
+
|
| 24 |
+
for seg in result["segments"]:
|
| 25 |
+
txt = seg["text"]
|
| 26 |
+
subtitle = TextClip(
|
| 27 |
+
txt,
|
| 28 |
+
fontsize=60,
|
| 29 |
+
font="DejaVu-Sans-Bold",
|
| 30 |
+
color="white",
|
| 31 |
+
bg_color="green",
|
| 32 |
+
method="caption",
|
| 33 |
+
size=(video_clip.w, None)
|
| 34 |
+
)
|
| 35 |
+
subtitle = subtitle.set_start(seg["start"]).set_duration(seg["end"] - seg["start"]).set_pos(("center", "bottom"))
|
| 36 |
+
subtitle_clips.append(subtitle)
|
| 37 |
+
|
| 38 |
+
final = CompositeVideoClip([video_clip, *subtitle_clips])
|
| 39 |
+
output_path = os.path.join(tempfile.gettempdir(), "output_with_subs.mp4")
|
| 40 |
+
final.write_videofile(output_path, codec="libx264", audio_codec="aac")
|
| 41 |
+
|
| 42 |
+
return output_path
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
interface = gr.Interface(
|
| 46 |
+
fn=generate_subtitles,
|
| 47 |
+
inputs=[
|
| 48 |
+
gr.Video(label="Upload your video"),
|
| 49 |
+
gr.Dropdown(choices=["hi", "en"], value="hi", label="Language")
|
| 50 |
+
],
|
| 51 |
+
outputs=gr.Video(label="Output with Subtitles"),
|
| 52 |
+
title="🎬 VOZO-style Subtitle Generator (Free)",
|
| 53 |
+
description="Upload a short Hindi or English video and get VOZO-style subtitles with green background automatically!"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
interface.launch()
|