├── Generate_English_Subtitles_Video_with_OpenAI_Whisper.ipynb ├── LICENSE ├── README.md └── app.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 amrrs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # subtitle-embedded-video-generator 2 | 3 | Powered by OpenAI Whisper & Gradio 4 | 5 | 6 | 7 | Open In Colab 8 | 9 | 10 | 11 | If you just want a Gradio App, refer this [app.py](app.py) 12 | 13 | 14 | # Demo 15 | 16 | ![whisperapp](https://user-images.githubusercontent.com/5347322/191827132-423d0a51-7db3-4486-8603-2cd2d895d921.gif) 17 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 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 | def video2mp3(video_file, output_ext="mp3"): 14 | filename, ext = os.path.splitext(video_file) 15 | subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"], 16 | stdout=subprocess.DEVNULL, 17 | stderr=subprocess.STDOUT) 18 | return f"{filename}.{output_ext}" 19 | 20 | 21 | def translate(input_video): 22 | 23 | audio_file = video2mp3(input_video) 24 | 25 | options = dict(beam_size=5, best_of=5) 26 | translate_options = dict(task="translate", **options) 27 | result = model.transcribe(audio_file,**translate_options) 28 | 29 | output_dir = '/content/' 30 | audio_path = audio_file.split(".")[0] 31 | 32 | with open(os.path.join(output_dir, audio_path + ".vtt"), "w") as vtt: 33 | write_vtt(result["segments"], file=vtt) 34 | 35 | subtitle = audio_path + ".vtt" 36 | output_video = audio_path + "_subtitled.mp4" 37 | 38 | os.system(f"ffmpeg -i {input_video} -vf subtitles={subtitle} {output_video}") 39 | 40 | return output_video 41 | 42 | 43 | 44 | title = "Add Text/Caption to your YouTube Shorts - MultiLingual" 45 | 46 | block = gr.Blocks() 47 | 48 | with block: 49 | 50 | with gr.Group(): 51 | with gr.Box(): 52 | 53 | 54 | 55 | with gr.Row().style(): 56 | 57 | inp_video = gr.Video( 58 | label="Input Video", 59 | type="filepath", 60 | mirror_webcam = False 61 | ) 62 | op_video = gr.Video() 63 | btn = gr.Button("Generate Subtitle Video") 64 | 65 | 66 | 67 | 68 | 69 | 70 | btn.click(translate, inputs=[inp_video], outputs=[op_video]) 71 | 72 | gr.HTML(''' 73 | 77 | ''') 78 | 79 | block.launch(debug = True) 80 | --------------------------------------------------------------------------------