Spaces:
Paused
Paused
mrfakename
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) 2024, please contact before redistribution/modification
|
2 |
+
|
3 |
+
FONT_URL = 'https://fonts.gstatic.com/s/inter/v13/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyfAZ9hjQ.ttf'
|
4 |
+
from moviepy.editor import *
|
5 |
+
import whisper
|
6 |
+
from cached_path import cached_path
|
7 |
+
from moviepy.video.tools.subtitles import SubtitlesClip
|
8 |
+
import torch
|
9 |
+
import tempfile
|
10 |
+
import gradio as gr
|
11 |
+
mdl = whisper.load_model("base")
|
12 |
+
|
13 |
+
def subtitle(input):
|
14 |
+
status = "**Starting...**"
|
15 |
+
yield status, gr.update()
|
16 |
+
gr.Info("Transcribing...")
|
17 |
+
status += "\n\n[1/5] Transcribing... (may take a while)"
|
18 |
+
yield status, gr.update()
|
19 |
+
transcript = mdl.transcribe(
|
20 |
+
word_timestamps=True,
|
21 |
+
audio=input
|
22 |
+
)
|
23 |
+
status += "\n\n[2/5] Processing subtitles..."
|
24 |
+
yield status, gr.update()
|
25 |
+
gr.Info("Processing subtitles...")
|
26 |
+
subs = []
|
27 |
+
for segment in transcript['segments']:
|
28 |
+
for word in segment['words']:
|
29 |
+
subs.append(((word['start'], word['end'],), word['word'].strip(),))
|
30 |
+
status += "\n\n[3/5] Loading video..."
|
31 |
+
yield status, gr.update()
|
32 |
+
gr.Info("Loading video...")
|
33 |
+
video = VideoFileClip(input)
|
34 |
+
width, height = video.size
|
35 |
+
gr.Info(width)
|
36 |
+
generator = lambda txt: TextClip(txt, size=(width * (3 / 4) + 8, None), color='white', stroke_color='black', stroke_width=8, method='caption', fontsize=min(width / 7, height / 7), font=str(cached_path(FONT_URL)))
|
37 |
+
generator1 = lambda txt: TextClip(txt, size=(width * (3 / 4), None), color='white', method='caption', fontsize=min(width / 7, height / 7), font=str(cached_path(FONT_URL)))
|
38 |
+
status += "\n\n[4/5] Loading video clip..."
|
39 |
+
yield status, gr.update()
|
40 |
+
gr.Info("Loading video clip...")
|
41 |
+
subtitles = SubtitlesClip(subs, generator)
|
42 |
+
subtitles2 = SubtitlesClip(subs, generator1)
|
43 |
+
result_1 = CompositeVideoClip([video, subtitles.set_pos(('center','center'))])
|
44 |
+
result = CompositeVideoClip([result_1, subtitles2.set_pos(('center','center'))])
|
45 |
+
status += "\n\n[5/5] Writing video... (may take a while)"
|
46 |
+
yield status, gr.update()
|
47 |
+
gr.Info("Writing video...")
|
48 |
+
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as f:
|
49 |
+
result.write_videofile(f.name, codec='h264_videotoolbox', audio_codec='aac', threads=64)
|
50 |
+
status += "\n\n**Done!**"
|
51 |
+
yield status, f.name
|
52 |
+
return
|
53 |
+
|
54 |
+
with gr.Blocks() as demo:
|
55 |
+
gr.Markdown("""
|
56 |
+
# AutoSubs
|
57 |
+
|
58 |
+
Automatically add on-screen subtitles to your videos.
|
59 |
+
|
60 |
+
**NOTE:** Uploading copyrighted/NSFW content to this service is strictly prohibited.
|
61 |
+
|
62 |
+
The maximum length of video is 15 minutes. This service probably won't work well on non-English videos.
|
63 |
+
|
64 |
+
Powered by OAI Whisper & MoviePy!
|
65 |
+
""")
|
66 |
+
status = gr.Markdown("**Status updates will appear here.**")
|
67 |
+
vid_inp = gr.Video(interactive=True, label="Upload or record video", max_length=900)
|
68 |
+
go_btn = gr.Button("Transcribe!", variant="primary")
|
69 |
+
vid_out = gr.Video(interactive=False, label="Result")
|
70 |
+
go_btn.click(subtitle, inputs=[vid_inp], outputs=[status, vid_out])
|
71 |
+
demo.queue(api_open=False).launch(show_api=False)
|