Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,42 +3,32 @@ import subprocess
|
|
| 3 |
import os
|
| 4 |
from moviepy.editor import VideoFileClip
|
| 5 |
import whisper
|
| 6 |
-
import tempfile
|
| 7 |
|
|
|
|
| 8 |
model = whisper.load_model("small")
|
| 9 |
|
| 10 |
-
def process_video(
|
| 11 |
try:
|
| 12 |
-
# Handle Gradio file input
|
| 13 |
-
if isinstance(video_file, str) and video_file.startswith("http"):
|
| 14 |
-
return None, "❌ Direct URLs not supported in Space API. Please upload file."
|
| 15 |
-
|
| 16 |
-
# Save to temp
|
| 17 |
-
tmp_video = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
|
| 18 |
-
tmp_video.write(video_file.read())
|
| 19 |
-
tmp_video.close()
|
| 20 |
-
|
| 21 |
# Extract audio
|
| 22 |
-
video = VideoFileClip(
|
| 23 |
-
audio_path =
|
| 24 |
video.audio.write_audiofile(audio_path, codec="pcm_s16le")
|
| 25 |
|
| 26 |
# Transcribe
|
| 27 |
result = model.transcribe(audio_path, language="en")
|
| 28 |
transcript = result["text"]
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
with open(srt_path, "w", encoding="utf-8") as f:
|
| 33 |
for i, seg in enumerate(result["segments"]):
|
| 34 |
start = seg["start"]
|
| 35 |
end = seg["end"]
|
| 36 |
text = seg["text"].strip()
|
| 37 |
f.write(f"{i+1}\n{format_time(start)} --> {format_time(end)}\n{text}\n\n")
|
| 38 |
|
| 39 |
-
# Burn subtitles
|
| 40 |
-
output_path =
|
| 41 |
-
burn_subtitles(
|
| 42 |
|
| 43 |
return output_path, transcript
|
| 44 |
|
|
@@ -61,10 +51,14 @@ def burn_subtitles(video_path, srt_path, output_path):
|
|
| 61 |
]
|
| 62 |
subprocess.run(cmd, check=True)
|
| 63 |
|
|
|
|
| 64 |
iface = gr.Interface(
|
| 65 |
fn=process_video,
|
| 66 |
-
inputs=gr.Video(label="Upload
|
| 67 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
| 68 |
title="🎬 Whisper Subtitle Burner",
|
| 69 |
description="Uploads a video, generates English subtitles using Whisper, and burns them into the video."
|
| 70 |
)
|
|
|
|
| 3 |
import os
|
| 4 |
from moviepy.editor import VideoFileClip
|
| 5 |
import whisper
|
|
|
|
| 6 |
|
| 7 |
+
# Load the smaller model for speed
|
| 8 |
model = whisper.load_model("small")
|
| 9 |
|
| 10 |
+
def process_video(video_path):
|
| 11 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Extract audio
|
| 13 |
+
video = VideoFileClip(video_path)
|
| 14 |
+
audio_path = "audio.wav"
|
| 15 |
video.audio.write_audiofile(audio_path, codec="pcm_s16le")
|
| 16 |
|
| 17 |
# Transcribe
|
| 18 |
result = model.transcribe(audio_path, language="en")
|
| 19 |
transcript = result["text"]
|
| 20 |
|
| 21 |
+
# Save transcript to file
|
| 22 |
+
with open("transcript.srt", "w", encoding="utf-8") as f:
|
|
|
|
| 23 |
for i, seg in enumerate(result["segments"]):
|
| 24 |
start = seg["start"]
|
| 25 |
end = seg["end"]
|
| 26 |
text = seg["text"].strip()
|
| 27 |
f.write(f"{i+1}\n{format_time(start)} --> {format_time(end)}\n{text}\n\n")
|
| 28 |
|
| 29 |
+
# Burn subtitles using ffmpeg
|
| 30 |
+
output_path = "output_with_subs.mp4"
|
| 31 |
+
burn_subtitles(video_path, "transcript.srt", output_path)
|
| 32 |
|
| 33 |
return output_path, transcript
|
| 34 |
|
|
|
|
| 51 |
]
|
| 52 |
subprocess.run(cmd, check=True)
|
| 53 |
|
| 54 |
+
# Gradio UI
|
| 55 |
iface = gr.Interface(
|
| 56 |
fn=process_video,
|
| 57 |
+
inputs=gr.Video(label="Upload your video"),
|
| 58 |
+
outputs=[
|
| 59 |
+
gr.Video(label="Video with Subtitles"),
|
| 60 |
+
gr.Textbox(label="Transcript", lines=8)
|
| 61 |
+
],
|
| 62 |
title="🎬 Whisper Subtitle Burner",
|
| 63 |
description="Uploads a video, generates English subtitles using Whisper, and burns them into the video."
|
| 64 |
)
|