Spaces:
Runtime error
Runtime error
added app.py and requirements.txt
Browse files- app.py +60 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
# import sys
|
4 |
+
import subprocess
|
5 |
+
|
6 |
+
import whisper
|
7 |
+
from whisper.utils import write_vtt
|
8 |
+
|
9 |
+
model = whisper.load_model("base")
|
10 |
+
|
11 |
+
title = "Add multilingual text/caption to your video"
|
12 |
+
|
13 |
+
|
14 |
+
def video2mp3(video_file, output_ext="mp3"):
|
15 |
+
filename, ext = os.path.splitext(video_file)
|
16 |
+
subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"],
|
17 |
+
stdout=subprocess.DEVNULL,
|
18 |
+
stderr=subprocess.STDOUT)
|
19 |
+
return f"{filename}.{output_ext}"
|
20 |
+
|
21 |
+
|
22 |
+
def transcribe(input_video):
|
23 |
+
audio_file = video2mp3(input_video)
|
24 |
+
|
25 |
+
# options = dict(beam_size=5, best_of=5, fp16=False)
|
26 |
+
# translate_options = dict(task="translate", **options)
|
27 |
+
# result = model.transcribe(audio_file, **translate_options)
|
28 |
+
|
29 |
+
result = model.transcribe(audio_file)
|
30 |
+
|
31 |
+
output_dir = ''
|
32 |
+
audio_path = audio_file.split(".")[0]
|
33 |
+
|
34 |
+
with open(os.path.join(output_dir, audio_path + ".vtt"), "w") as vtt:
|
35 |
+
write_vtt(result["segments"], file=vtt)
|
36 |
+
|
37 |
+
subtitle = audio_path + ".vtt"
|
38 |
+
output_video = audio_path + "_subtitled.mp4"
|
39 |
+
|
40 |
+
os.system(f"ffmpeg -i {input_video} -vf subtitles={subtitle} {output_video}")
|
41 |
+
|
42 |
+
return output_video
|
43 |
+
|
44 |
+
|
45 |
+
block = gr.Blocks()
|
46 |
+
with block:
|
47 |
+
with gr.Group():
|
48 |
+
with gr.Box():
|
49 |
+
with gr.Row().style():
|
50 |
+
inp_video = gr.Video(
|
51 |
+
label="Input Video",
|
52 |
+
type="filepath",
|
53 |
+
mirror_webcam=False
|
54 |
+
)
|
55 |
+
op_video = gr.Video()
|
56 |
+
btn = gr.Button("Generate Subtitle Video")
|
57 |
+
|
58 |
+
btn.click(transcribe, inputs=[inp_video], outputs=[op_video])
|
59 |
+
|
60 |
+
block.launch(enable_queue=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
tensorflow
|
2 |
+
git+https://github.com/openai/whisper.git
|