Spaces:
Build error
Build error
Commit
·
ca57463
1
Parent(s):
3afd733
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|