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