Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
import subprocess
|
6 |
+
#from moviepy.editor import VideoFileClip
|
7 |
+
|
8 |
+
import whisper
|
9 |
+
from whisper.utils import write_vtt
|
10 |
+
|
11 |
+
model = whisper.load_model("medium")
|
12 |
+
|
13 |
+
def video2mp3(video_file, output_ext="mp3"):
|
14 |
+
filename, ext = os.path.splitext(video_file)
|
15 |
+
subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"],
|
16 |
+
stdout=subprocess.DEVNULL,
|
17 |
+
stderr=subprocess.STDOUT)
|
18 |
+
return f"{filename}.{output_ext}"
|
19 |
+
|
20 |
+
|
21 |
+
def translate(video):
|
22 |
+
|
23 |
+
audio_file = video2mp3(video)
|
24 |
+
|
25 |
+
options = dict(beam_size=5, best_of=5, fp_16 = False)
|
26 |
+
translate_options = dict(task="translate", **options)
|
27 |
+
result = model.transcribe(audio_file,**translate_options)
|
28 |
+
|
29 |
+
#output_dir = '/content/'
|
30 |
+
audio_path = audio_file.split(".")[0]
|
31 |
+
|
32 |
+
with open(os.path.join(output_dir, audio_path + ".vtt"), "w") as vtt:
|
33 |
+
write_vtt(result["segments"], file=vtt)
|
34 |
+
|
35 |
+
subtitle = audio_path + ".vtt"
|
36 |
+
output_video = audio_path + "_subtitled.mp4"
|
37 |
+
|
38 |
+
os.system(f"ffmpeg -i {input_video} -vf subtitles={subtitle} {output_video}")
|
39 |
+
|
40 |
+
return output_video
|
41 |
+
|
42 |
+
block = gr.Blocks()
|
43 |
+
with block:
|
44 |
+
|
45 |
+
with gr.Group():
|
46 |
+
with gr.Box():
|
47 |
+
with gr.Row().style(mobile_collapse=False, equal_height=True):
|
48 |
+
inp_video = gr.Video(
|
49 |
+
label="Input Video",
|
50 |
+
type="filepath"
|
51 |
+
)
|
52 |
+
op_video = gr.Video()
|
53 |
+
btn = gr.Button("Generate Subtitle Video")
|
54 |
+
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
|
60 |
+
btn.click(translate, inputs=[inp_video], outputs=[op_video])
|
61 |
+
|
62 |
+
gr.HTML('''
|
63 |
+
<div class="footer">
|
64 |
+
<p>Model by <a href="https://github.com/openai/whisper" style="text-decoration: underline;" target="_blank">OpenAI</a> - Gradio App by <a href="https://twitter.com/1littlecoder" style="text-decoration: underline;" target="_blank">1littlecoder</a>
|
65 |
+
</p>
|
66 |
+
</div>
|
67 |
+
''')
|
68 |
+
|
69 |
+
block.launch(debug = True)
|