Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import whisper
|
2 |
+
import moviepy.editor as mp
|
3 |
+
import gradio as gr
|
4 |
+
import torch
|
5 |
+
import subprocess
|
6 |
+
|
7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
8 |
+
model_name = "tiny"
|
9 |
+
whisper_model = whisper.load_model(model_name).to(device)
|
10 |
+
|
11 |
+
def generate_srt(transcription_result):
|
12 |
+
srt_content = ""
|
13 |
+
for i, segment in enumerate(transcription_result['segments']):
|
14 |
+
start = segment['start']
|
15 |
+
end = segment['end']
|
16 |
+
start_time = f"{int(start//3600):02}:{int((start%3600)//60):02}:{int(start%60):02},{int((start%1)*1000):03}"
|
17 |
+
end_time = f"{int(end//3600):02}:{int((end%3600)//60):02}:{int(end%60):02},{int((end%1)*1000):03}"
|
18 |
+
srt_content += f"{i+1}\n{start_time} --> {end_time}\n{segment['text'].strip()}\n\n"
|
19 |
+
return srt_content
|
20 |
+
|
21 |
+
def extract_audio_ffmpeg(video_file, audio_output):
|
22 |
+
subprocess.run([
|
23 |
+
'ffmpeg',
|
24 |
+
'-i', video_file,
|
25 |
+
'-vn',
|
26 |
+
'-acodec', 'pcm_s16le',
|
27 |
+
'-ar', '16000',
|
28 |
+
audio_output,
|
29 |
+
'-y'
|
30 |
+
])
|
31 |
+
|
32 |
+
def transcribe_and_generate_subtitles(video):
|
33 |
+
audio_path = "temp_audio.wav"
|
34 |
+
extract_audio_ffmpeg(video, audio_path)
|
35 |
+
transcription_result = whisper_model.transcribe(audio_path, language="en", verbose=False)
|
36 |
+
detected_language = transcription_result['language']
|
37 |
+
if detected_language == "hau":
|
38 |
+
transcription_result = whisper_model.transcribe(audio_path, task="translate", verbose=False)
|
39 |
+
elif detected_language == "yor":
|
40 |
+
transcription_result = whisper_model.transcribe(audio_path, task="translate", language="yor", verbose=False)
|
41 |
+
elif detected_language == "ibo":
|
42 |
+
transcription_result = whisper_model.transcribe(audio_path, task="translate", language="ibo", verbose=False)
|
43 |
+
srt_content = generate_srt(transcription_result)
|
44 |
+
srt_file = "output_subtitles.srt"
|
45 |
+
with open(srt_file, "w", encoding="utf-8") as f:
|
46 |
+
f.write(srt_content)
|
47 |
+
output_video = "video_with_subtitles.mp4"
|
48 |
+
subprocess.run([
|
49 |
+
'ffmpeg',
|
50 |
+
'-i', video,
|
51 |
+
'-vf', f"subtitles={srt_file}",
|
52 |
+
output_video,
|
53 |
+
'-y'
|
54 |
+
])
|
55 |
+
return transcription_result["text"], output_video
|
56 |
+
|
57 |
+
interface = gr.Interface(
|
58 |
+
fn=transcribe_and_generate_subtitles,
|
59 |
+
inputs=gr.Video(label="Upload Video File"),
|
60 |
+
outputs=[
|
61 |
+
gr.Textbox(label="Transcription or Translation"),
|
62 |
+
gr.File(label="Download Video with Subtitles")
|
63 |
+
],
|
64 |
+
title="Video Subtitle Generator",
|
65 |
+
description="Upload a video in either English, Hausa, Yoruba, or Igbo. The system will detect the language, transcribe or translate if necessary, and generate a video with subtitles embedded.",
|
66 |
+
live=False
|
67 |
+
)
|
68 |
+
interface.launch()
|