Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,25 +4,32 @@ import tempfile
|
|
| 4 |
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
def
|
| 8 |
if video is None:
|
| 9 |
return "Please upload a video first!"
|
| 10 |
|
| 11 |
-
# Temporary
|
| 12 |
temp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 13 |
temp_video.write(video.read())
|
| 14 |
temp_video.close()
|
| 15 |
|
| 16 |
-
# Load
|
| 17 |
model = whisper.load_model("small")
|
| 18 |
-
result = model.transcribe(temp_video.name, language=language)
|
| 19 |
|
| 20 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
|
@@ -35,22 +42,23 @@ def generate_subtitles(video, language="hi"):
|
|
| 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(), "
|
| 40 |
-
final.write_videofile(output_path, codec="libx264", audio_codec="aac")
|
| 41 |
-
|
| 42 |
-
return output_path
|
| 43 |
|
|
|
|
| 44 |
|
|
|
|
| 45 |
interface = gr.Interface(
|
| 46 |
-
fn=
|
| 47 |
-
inputs=
|
| 48 |
-
|
| 49 |
-
gr.
|
|
|
|
| 50 |
],
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
description="Upload a short Hindi or English video and get VOZO-style subtitles with green background automatically!"
|
| 54 |
)
|
| 55 |
|
| 56 |
interface.launch()
|
|
|
|
| 4 |
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
def generate_auto_subtitles(video):
|
| 8 |
if video is None:
|
| 9 |
return "Please upload a video first!"
|
| 10 |
|
| 11 |
+
# Temporary save 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 |
|
| 19 |
+
# Auto language detection + transcription
|
| 20 |
+
result = model.transcribe(temp_video.name)
|
| 21 |
+
detected_lang = result.get("language", "unknown").upper()
|
| 22 |
+
|
| 23 |
+
# Prepare video
|
| 24 |
video_clip = VideoFileClip(temp_video.name)
|
| 25 |
subtitle_clips = []
|
| 26 |
|
| 27 |
+
# Generate subtitle overlays
|
| 28 |
for seg in result["segments"]:
|
| 29 |
+
txt = seg["text"].strip()
|
| 30 |
+
if not txt:
|
| 31 |
+
continue
|
| 32 |
+
|
| 33 |
subtitle = TextClip(
|
| 34 |
txt,
|
| 35 |
fontsize=60,
|
|
|
|
| 42 |
subtitle = subtitle.set_start(seg["start"]).set_duration(seg["end"] - seg["start"]).set_pos(("center", "bottom"))
|
| 43 |
subtitle_clips.append(subtitle)
|
| 44 |
|
| 45 |
+
# Combine subtitles with video
|
| 46 |
final = CompositeVideoClip([video_clip, *subtitle_clips])
|
| 47 |
+
output_path = os.path.join(tempfile.gettempdir(), "vozo_auto_subs.mp4")
|
| 48 |
+
final.write_videofile(output_path, codec="libx264", audio_codec="aac", verbose=False, logger=None)
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
return output_path, f"Detected Language: {detected_lang}"
|
| 51 |
|
| 52 |
+
# Gradio UI
|
| 53 |
interface = gr.Interface(
|
| 54 |
+
fn=generate_auto_subtitles,
|
| 55 |
+
inputs=gr.Video(label="π₯ Upload your video (Hindi / English / Hinglish)"),
|
| 56 |
+
outputs=[
|
| 57 |
+
gr.Video(label="π¬ Video with Auto Subtitles"),
|
| 58 |
+
gr.Textbox(label="π Detected Language")
|
| 59 |
],
|
| 60 |
+
title="ποΈ Auto Subtitle Generator (VOZO-style)",
|
| 61 |
+
description="Upload any short Hindi, English, Hinglish, or Indian English video β this AI auto-detects language and adds VOZO-style subtitles with a green background."
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
interface.launch()
|