Spaces:
Runtime error
Runtime error
Add application file
Browse files- app.py +74 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
title = "Add Text/Caption to your YouTube Shorts - MultiLingual"
|
14 |
+
|
15 |
+
def video2mp3(video_file, output_ext="mp3"):
|
16 |
+
filename, ext = os.path.splitext(video_file)
|
17 |
+
subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"],
|
18 |
+
stdout=subprocess.DEVNULL,
|
19 |
+
stderr=subprocess.STDOUT)
|
20 |
+
return f"{filename}.{output_ext}"
|
21 |
+
|
22 |
+
|
23 |
+
def translate(input_video):
|
24 |
+
|
25 |
+
audio_file = video2mp3(input_video)
|
26 |
+
|
27 |
+
options = dict(beam_size=5, best_of=5, fp16 = False)
|
28 |
+
translate_options = dict(task="translate", **options)
|
29 |
+
result = model.transcribe(audio_file,**translate_options)
|
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 |
+
block = gr.Blocks()
|
45 |
+
with block:
|
46 |
+
|
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 |
+
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
btn.click(translate, inputs=[inp_video], outputs=[op_video])
|
64 |
+
gr.HTML('''
|
65 |
+
<h1>This app give english subtitle to your short video (less than 30sec) in english, in any major language </h1>
|
66 |
+
''')
|
67 |
+
gr.HTML('''
|
68 |
+
<div class="footer">
|
69 |
+
<p>Model by <a href="https://github.com/openai/whisper" style="text-decoration: underline;" target="_blank">OpenAI</a> - Gradio App by <a href="https://www.linkedin.com/in/oluwafunbiadeneye?lipi=urn%3Ali%3Apage%3Ad_flagship3_profile_view_base_contact_details%3BJhODLsMxQTeoJI7nYzDzNw%3D%3D" style="text-decoration: underline;" target="_blank">Oluwafunbi</a> inspired by <a href= href="https://twitter.com/1littlecoder" style="text-decoration: underline;" target="_blank">1littlecoder</a>
|
70 |
+
</p>
|
71 |
+
</div>
|
72 |
+
''')
|
73 |
+
|
74 |
+
block.launch(debug = True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
tensorflow
|
2 |
+
git+https://github.com/openai/whisper.git
|