Spaces:
Runtime error
Runtime error
Subtitled Video
#1
by
ChiBenevisamPas
- opened
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import whisper
|
3 |
import os
|
|
|
4 |
|
5 |
# Load the Whisper model
|
6 |
model = whisper.load_model("base") # Choose 'tiny', 'base', 'small', 'medium', or 'large'
|
@@ -28,16 +29,51 @@ def transcribe_video(video_file):
|
|
28 |
|
29 |
# Write the transcription as subtitles
|
30 |
write_srt(result, srt_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
# Gradio interface
|
35 |
iface = gr.Interface(
|
36 |
fn=transcribe_video,
|
37 |
inputs=gr.File(label="Upload Video"),
|
38 |
-
outputs=gr.File(label="Download
|
39 |
title="Video Subtitle Generator",
|
40 |
-
description="Upload a video file to generate subtitles
|
41 |
)
|
42 |
|
43 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
import whisper
|
3 |
import os
|
4 |
+
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
|
5 |
|
6 |
# Load the Whisper model
|
7 |
model = whisper.load_model("base") # Choose 'tiny', 'base', 'small', 'medium', or 'large'
|
|
|
29 |
|
30 |
# Write the transcription as subtitles
|
31 |
write_srt(result, srt_file)
|
32 |
+
|
33 |
+
# Create subtitled video
|
34 |
+
subtitled_video_file = create_subtitled_video(video_file, srt_file)
|
35 |
+
|
36 |
+
return subtitled_video_file
|
37 |
+
|
38 |
+
def create_subtitled_video(video_file, srt_file):
|
39 |
+
# Load video
|
40 |
+
video_clip = VideoFileClip(video_file)
|
41 |
+
|
42 |
+
# Read SRT file and create text clips
|
43 |
+
subs = []
|
44 |
+
with open(srt_file, 'r') as f:
|
45 |
+
lines = f.readlines()
|
46 |
+
for i in range(0, len(lines), 4):
|
47 |
+
if i + 2 < len(lines):
|
48 |
+
start_time = convert_to_seconds(lines[i + 1].split(" --> ")[0])
|
49 |
+
end_time = convert_to_seconds(lines[i + 1].split(" --> ")[1])
|
50 |
+
text = lines[i + 2].strip()
|
51 |
+
text_clip = TextClip(text, fontsize=24, color='white', bg_color='black', size=video_clip.size, print_cmd=True)
|
52 |
+
text_clip = text_clip.set_start(start_time).set_end(end_time).set_position('bottom')
|
53 |
+
subs.append(text_clip)
|
54 |
+
|
55 |
+
# Combine video and subtitles
|
56 |
+
final_video = CompositeVideoClip([video_clip] + subs)
|
57 |
|
58 |
+
# Save the final video with subtitles
|
59 |
+
subtitled_video_file = "subtitled_video.mp4"
|
60 |
+
final_video.write_videofile(subtitled_video_file, codec="libx264", audio_codec="aac")
|
61 |
+
|
62 |
+
return subtitled_video_file
|
63 |
+
|
64 |
+
def convert_to_seconds(timestamp):
|
65 |
+
"""Convert timestamp in SRT format (HH:MM:SS,mmm) to seconds."""
|
66 |
+
h, m, s = timestamp.split(':')
|
67 |
+
seconds = int(h) * 3600 + int(m) * 60 + float(s.replace(',', '.'))
|
68 |
+
return seconds
|
69 |
|
70 |
# Gradio interface
|
71 |
iface = gr.Interface(
|
72 |
fn=transcribe_video,
|
73 |
inputs=gr.File(label="Upload Video"),
|
74 |
+
outputs=gr.File(label="Download Subtitled Video"),
|
75 |
title="Video Subtitle Generator",
|
76 |
+
description="Upload a video file to generate subtitles and download a subtitled video."
|
77 |
)
|
78 |
|
79 |
if __name__ == "__main__":
|