Ahmadkhan12 commited on
Commit
5725cca
·
verified ·
1 Parent(s): 2d3ea34

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -0
app.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import moviepy.editor as mp
3
+ from faster_whisper import WhisperModel
4
+ from googletrans import Translator
5
+ import srt
6
+ import datetime
7
+ import tempfile
8
+ import os
9
+
10
+ # ------------------------------
11
+ # Load Whisper (small model)
12
+ # ------------------------------
13
+ model = WhisperModel("small", device="cpu", compute_type="float32")
14
+ translator = Translator()
15
+
16
+
17
+ # ------------------------------
18
+ # Generate SRT subtitles
19
+ # ------------------------------
20
+ def generate_subtitles(video_file, target_lang):
21
+ # Extract audio
22
+ video = mp.VideoFileClip(video_file)
23
+ temp_audio = "temp_audio.wav"
24
+ video.audio.write_audiofile(temp_audio, verbose=False, logger=None)
25
+
26
+ # Transcribe with Whisper
27
+ segments, info = model.transcribe(temp_audio)
28
+
29
+ # Build SRT
30
+ subtitles = []
31
+ for i, seg in enumerate(segments):
32
+ start = datetime.timedelta(seconds=seg.start)
33
+ end = datetime.timedelta(seconds=seg.end)
34
+ original_text = seg.text.strip()
35
+
36
+ # Translate text
37
+ if target_lang != "original":
38
+ translated_text = translator.translate(original_text, dest=target_lang).text
39
+ else:
40
+ translated_text = original_text
41
+
42
+ subtitles.append(
43
+ srt.Subtitle(
44
+ index=i + 1,
45
+ start=start,
46
+ end=end,
47
+ content=translated_text
48
+ )
49
+ )
50
+
51
+ # Create SRT file
52
+ srt_data = srt.compose(subtitles)
53
+ srt_path = "output_subtitles.srt"
54
+ with open(srt_path, "w", encoding="utf-8") as srt_file:
55
+ srt_file.write(srt_data)
56
+
57
+ return srt_path
58
+
59
+
60
+ # ------------------------------
61
+ # Burn subtitles onto video
62
+ # ------------------------------
63
+ def burn_subtitles(video_file, srt_file):
64
+ output_path = "video_with_subtitles.mp4"
65
+
66
+ video = mp.VideoFileClip(video_file)
67
+
68
+ # Read SRT file
69
+ with open(srt_file, "r", encoding="utf-8") as f:
70
+ srt_content = f.read()
71
+
72
+ subs = list(srt.parse(srt_content))
73
+
74
+ subtitle_clips = []
75
+ for sub in subs:
76
+ txt = mp.TextClip(
77
+ sub.content,
78
+ fontsize=30,
79
+ color='white',
80
+ stroke_color='black',
81
+ stroke_width=2
82
+ )
83
+ txt = txt.set_start(sub.start.total_seconds()) \
84
+ .set_end(sub.end.total_seconds()) \
85
+ .set_position(('center', 'bottom'))
86
+
87
+ subtitle_clips.append(txt)
88
+
89
+ final = mp.CompositeVideoClip([video] + subtitle_clips)
90
+ final.write_videofile(output_path, codec="libx264", audio_codec="aac")
91
+
92
+ return output_path
93
+
94
+
95
+ # ------------------------------
96
+ # Main function (Gradio pipeline)
97
+ # ------------------------------
98
+ def process(video, language, burn):
99
+ if video is None:
100
+ return None, None
101
+
102
+ # Create subtitles
103
+ srt_path = generate_subtitles(video, language)
104
+
105
+ # Burn subtitles if selected
106
+ if burn:
107
+ video_out = burn_subtitles(video, srt_path)
108
+ return srt_path, video_out
109
+
110
+ return srt_path, None
111
+
112
+
113
+ # ------------------------------
114
+ # Gradio Interface UI
115
+ # ------------------------------
116
+ lang_options = {
117
+ "original": "Original (No Translation)",
118
+ "en": "English",
119
+ "ur": "Urdu",
120
+ "hi": "Hindi",
121
+ "ps": "Pashto",
122
+ "ar": "Arabic",
123
+ }
124
+
125
+ ui = gr.Interface(
126
+ fn=process,
127
+ inputs=[
128
+ gr.Video(label="Upload Video"),
129
+ gr.Dropdown(choices=list(lang_options.keys()), label="Translate To", value="original"),
130
+ gr.Checkbox(label="Burn Subtitles on Video", value=False)
131
+ ],
132
+ outputs=[
133
+ gr.File(label="Download SRT File"),
134
+ gr.Video(label="Video with Subtitles")
135
+ ],
136
+ title="AI Video Subtitle Generator",
137
+ description="Upload any video → Auto-transcribe + translate → Generate subtitles → Optional subtitle-burned video."
138
+ )
139
+
140
+ # ------------------------------
141
+ # Launch App
142
+ # ------------------------------
143
+ ui.launch()