Spaces:
Runtime error
Runtime error
isaakkamau
commited on
Commit
·
5377a79
1
Parent(s):
88d010a
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,64 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import sys
|
4 |
import subprocess
|
5 |
-
|
6 |
-
|
7 |
import whisper
|
8 |
from whisper.utils import write_vtt
|
9 |
|
10 |
-
model = whisper.load_model(
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
def translate(input_video):
|
23 |
-
|
24 |
-
audio_file = video2mp3(input_video)
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
output_dir = ''
|
31 |
-
audio_path = audio_file.split(
|
32 |
-
|
33 |
-
with open(os.path.join(output_dir, audio_path + ".vtt"), "w") as vtt:
|
34 |
-
write_vtt(result["segments"], file=vtt)
|
35 |
|
36 |
-
|
37 |
-
|
|
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
return output_video
|
42 |
|
43 |
block = gr.Blocks()
|
44 |
with block:
|
45 |
-
|
46 |
with gr.Group():
|
47 |
with gr.Box():
|
48 |
with gr.Row().style():
|
49 |
-
|
50 |
label="Input Video",
|
51 |
type="filepath",
|
52 |
-
mirror_webcam
|
53 |
)
|
54 |
-
|
55 |
-
btn = gr.Button(
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
|
61 |
-
|
62 |
-
btn.click(translate, inputs=[inp_video], outputs=[op_video])
|
63 |
-
gr.HTML('''
|
64 |
-
<div class="footer">
|
65 |
-
<p>Powered by <a href="https://openai.com/" style="text-decoration: underline;" target="_blank">OpenAI</a> - Developed by <a href="https://github.com/Isaakkamau" style="text-decoration: underline;" target="_blank">+254704205553</a>
|
66 |
-
</p>
|
67 |
-
</div>
|
68 |
-
''')
|
69 |
|
70 |
-
block.launch(enable_queue
|
|
|
1 |
+
import gradio as gr
|
|
|
|
|
2 |
import subprocess
|
3 |
+
import os
|
|
|
4 |
import whisper
|
5 |
from whisper.utils import write_vtt
|
6 |
|
7 |
+
model = whisper.load_model('tiny')
|
8 |
+
title = 'Add Captions(CC) to your videos'
|
9 |
+
|
10 |
+
def convert_mp4_mp3(file, output="mp3"):
|
11 |
+
"""
|
12 |
+
Convert the Input Video files to Audio files (MP4 -> MP3)
|
13 |
+
using FFMPEG
|
14 |
+
"""
|
15 |
+
filename, ext = os.path.splitext(file)
|
16 |
+
subprocess.call(['ffmpeg', '-y', '-i', file, f'{filename}.{output}'],
|
17 |
+
stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
return f"{filename}.{output}"
|
20 |
+
|
21 |
+
def transcribe(video):
|
22 |
+
"""
|
23 |
+
Transcribe the text in the video file using Whisper model
|
24 |
+
and write the transcribed captions to the video
|
25 |
+
"""
|
26 |
+
audio_file = convert_mp4_mp3(video)
|
27 |
+
# CFG
|
28 |
+
options = dict(beam_size=5, best_of=5, fp16=False)
|
29 |
+
translate_options = dict(task='translate', **options)
|
30 |
+
result = model.transcribe(audio_file, **translate_options)
|
31 |
|
32 |
output_dir = ''
|
33 |
+
# audio_path = audio_file.split('.')[0]
|
34 |
+
audio_path = os.path.splitext(os.path.basename(audio_file))[0]
|
|
|
|
|
35 |
|
36 |
+
# Write Subtitle onto a .vtt file
|
37 |
+
with open(os.path.join(output_dir, audio_path + '.vtt'), 'w') as f:
|
38 |
+
write_vtt(result['segments'], file=f)
|
39 |
|
40 |
+
# Write the subtitles on the input video
|
41 |
+
# subtitle = audio_path + '.vtt'
|
42 |
+
# output_video = audio_path + '_subtitled.mp4'
|
43 |
+
# os.system(f'ffmpeg -i {video} -vf subtitles={subtitle} {output_video}')
|
44 |
+
output_video = os.path.join(output_dir, f'{audio_path}_subtitled.mp4')
|
45 |
+
os.system(f'ffmpeg -i {video} -vf subtitles={os.path.join(output_dir, audio_path + ".vtt")} {output_video}')
|
46 |
|
47 |
return output_video
|
48 |
|
49 |
block = gr.Blocks()
|
50 |
with block:
|
|
|
51 |
with gr.Group():
|
52 |
with gr.Box():
|
53 |
with gr.Row().style():
|
54 |
+
input_video = gr.Video(
|
55 |
label="Input Video",
|
56 |
type="filepath",
|
57 |
+
mirror_webcam=False
|
58 |
)
|
59 |
+
output_video = gr.Video()
|
60 |
+
btn = gr.Button('Generate Subtitle Video')
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
btn.click(transcribe, inputs=[input_video], outputs=[output_video])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
block.launch(enable_queue=True)
|