isaakkamau commited on
Commit
20c9059
·
1 Parent(s): 0a318ac

Updated translator

Browse files
Files changed (1) hide show
  1. app.py +53 -2
app.py CHANGED
@@ -141,11 +141,12 @@ remove_files(7)
141
 
142
 
143
 
144
- from pydub import AudioSegment
145
 
146
  #Download the model
147
- model = whisper.load_model("tiny")
148
 
 
149
 
150
  def video2mp3(video_file, output_ext="mp3"):
151
  filename, ext = os.path.splitext(video_file)
@@ -199,6 +200,56 @@ if uploaded_file is not None:
199
 
200
  # Remove temporary files
201
  os.remove("temp_video.mp4")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
 
203
  st.markdown(
204
  '''
 
141
 
142
 
143
 
144
+ #from pydub import AudioSegment
145
 
146
  #Download the model
147
+ model = whisper.load_model("small")
148
 
149
+ '''
150
 
151
  def video2mp3(video_file, output_ext="mp3"):
152
  filename, ext = os.path.splitext(video_file)
 
200
 
201
  # Remove temporary files
202
  os.remove("temp_video.mp4")
203
+ '''
204
+
205
+ def video2mp3(video_file, output_ext="mp3"):
206
+ filename, ext = os.path.splitext(video_file)
207
+ subprocess.call(["ffmpeg", "-y", "-i", video_file, f"{filename}.{output_ext}"],
208
+ stdout=subprocess.DEVNULL,
209
+ stderr=subprocess.STDOUT)
210
+ return f"{filename}.{output_ext}"
211
+
212
+
213
+ def translate(input_video):
214
+ audio_file = video2mp3(input_video)
215
+
216
+ options = dict(beam_size=5, best_of=5)
217
+ translate_options = dict(task="translate", **options)
218
+ result = model.transcribe(audio_file, **translate_options)
219
+
220
+ output_dir = '/content/'
221
+ audio_path = audio_file.split(".")[0]
222
+
223
+ with open(os.path.join(output_dir, audio_path + ".vtt"), "w") as vtt:
224
+ write_vtt(result["segments"], file=vtt)
225
+
226
+ subtitle = audio_path + ".vtt"
227
+ output_video = audio_path + "_subtitled.mp4"
228
+
229
+ os.system(f"ffmpeg -i {input_video} -vf subtitles={subtitle} {output_video}")
230
+
231
+ return output_video
232
+
233
+
234
+ st.title("MultiLingual AI: Add Caption to Videos")
235
+
236
+ uploaded_file = st.file_uploader("Upload your video", type=["mp4"])
237
+
238
+ if uploaded_file is not None:
239
+ st.video(uploaded_file)
240
+ if st.button("Generate Subtitle Video"):
241
+ # Save uploaded file to a temporary location
242
+ with open("temp_video.mp4", "wb") as f:
243
+ f.write(uploaded_file.read())
244
+
245
+ output_video = translate("temp_video.mp4")
246
+
247
+ # Display the output video
248
+ st.video(output_video)
249
+
250
+ # Remove temporary files
251
+ os.remove("temp_video.mp4")
252
+
253
 
254
  st.markdown(
255
  '''