isaakkamau commited on
Commit
061f1ce
1 Parent(s): 11004c8

Add application files

Browse files
Files changed (2) hide show
  1. app.py +78 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import sys
4
+ import subprocess
5
+ #from moviepy.editor import VideoFileClip
6
+
7
+ import whisper
8
+ from whisper.utils import write_vtt
9
+
10
+ model = whisper.load_model("small")
11
+
12
+ def video2mp3(video_file, output_ext="mp3"):
13
+ filename, ext = os.path.splitext(video_file)
14
+ subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"],
15
+ stdout=subprocess.DEVNULL,
16
+ stderr=subprocess.STDOUT)
17
+ return f"{filename}.{output_ext}"
18
+
19
+
20
+ def translate(input_video):
21
+
22
+ audio_file = video2mp3(input_video)
23
+
24
+ options = dict(beam_size=5, best_of=5)
25
+ translate_options = dict(task="translate", **options)
26
+ result = model.transcribe(audio_file,**translate_options)
27
+
28
+ output_dir = '/content/'
29
+ audio_path = audio_file.split(".")[0]
30
+
31
+ with open(os.path.join(output_dir, audio_path + ".vtt"), "w") as vtt:
32
+ write_vtt(result["segments"], file=vtt)
33
+
34
+ subtitle = audio_path + ".vtt"
35
+ output_video = audio_path + "_subtitled.mp4"
36
+
37
+ os.system(f"ffmpeg -i {input_video} -vf subtitles={subtitle} {output_video}")
38
+
39
+ return output_video
40
+
41
+
42
+
43
+ title = "Add Text/Caption to your YouTube Shorts - MultiLingual"
44
+
45
+ block = gr.Blocks()
46
+
47
+ with block:
48
+
49
+ with gr.Group():
50
+ with gr.Box():
51
+
52
+
53
+
54
+ with gr.Row().style():
55
+
56
+ inp_video = gr.Video(
57
+ label="Input Video",
58
+ type="filepath",
59
+ mirror_webcam = False
60
+ )
61
+ op_video = gr.Video()
62
+ btn = gr.Button("Generate Subtitle Video")
63
+
64
+
65
+
66
+
67
+
68
+
69
+ btn.click(translate, inputs=[inp_video], outputs=[op_video])
70
+
71
+ gr.HTML('''
72
+ <div class="footer">
73
+ <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>
74
+ </p>
75
+ </div>
76
+ ''')
77
+
78
+ block.launch(debug = True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ streamlit==1.22.0
3
+ gTTS==2.2.2
4
+ googletrans==3.1.0a0
5
+ openai-whisper==20230117