RASMUS commited on
Commit
0461bb6
1 Parent(s): 1279c7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -36
app.py CHANGED
@@ -269,9 +269,14 @@ def translate_transcriptions(df, selected_translation_lang_2):
269
 
270
  print("translations done")
271
 
272
- print("Starting SRT-file creation")
273
 
274
- with open('subtitles.srt','w', encoding="utf-8") as file:
 
 
 
 
 
275
  for i in range(len(df)):
276
  file.write(str(i+1))
277
  file.write('\n')
@@ -279,32 +284,32 @@ def translate_transcriptions(df, selected_translation_lang_2):
279
 
280
 
281
  milliseconds = round(start * 1000.0)
282
-
283
  hours = milliseconds // 3_600_000
284
  milliseconds -= hours * 3_600_000
285
-
286
  minutes = milliseconds // 60_000
287
  milliseconds -= minutes * 60_000
288
-
289
  seconds = milliseconds // 1_000
290
  milliseconds -= seconds * 1_000
291
-
292
  file.write(f"{hours}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}")
293
 
294
  stop = df.iloc[i]['end']
295
 
296
 
297
  milliseconds = round(stop * 1000.0)
298
-
299
  hours = milliseconds // 3_600_000
300
  milliseconds -= hours * 3_600_000
301
-
302
  minutes = milliseconds // 60_000
303
  milliseconds -= minutes * 60_000
304
-
305
  seconds = milliseconds // 1_000
306
  milliseconds -= seconds * 1_000
307
-
308
 
309
  file.write(' --> ')
310
  file.write(f"{hours}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}")
@@ -313,16 +318,9 @@ def translate_transcriptions(df, selected_translation_lang_2):
313
  if int(i) != len(df)-1:
314
  file.write('\n\n')
315
 
316
- print("SRT DONE")
317
-
318
- return df, 'subtitles.srt'
319
-
320
- def create_srt_and_burn(srt_file, video_in):
321
-
322
- print("Starting creation of video wit srt")
323
-
324
  try:
325
- file1 = open('./subtitles.srt', 'r', encoding="utf-8")
326
  Lines = file1.readlines()
327
 
328
  count = 0
@@ -336,10 +334,10 @@ def create_srt_and_burn(srt_file, video_in):
336
 
337
  video_out = video_in.replace('.mp4', '_out.mp4')
338
  print(video_out)
339
- command = 'ffmpeg -i "{}" -y -vf subtitles=./subtitles.srt "{}"'.format(video_in, video_out)
340
  print(command)
341
  os.system(command)
342
- return video_out, './subtitles.srt'
343
  except Exception as e:
344
  print(e)
345
  return video_out
@@ -359,13 +357,6 @@ selected_whisper_model = gr.Dropdown(choices=whisper_models, type="value", value
359
  transcription_df = gr.DataFrame(value=df_init,label="Transcription dataframe", row_count=(0, "dynamic"), max_rows = 10, wrap=True, overflow_row_behaviour='paginate')
360
  transcription_and_translation_df = gr.DataFrame(value=df_init,label="Transcription and translation dataframe", max_rows = 10, wrap=True, overflow_row_behaviour='paginate')
361
 
362
- srt_file = gr.File(
363
- label="Download srt-file",
364
- file_count="single",
365
- type="file",
366
- interactive=False,
367
- )
368
-
369
  demo = gr.Blocks(css='''
370
  #cut_btn, #reset_btn { align-self:stretch; }
371
  #\\31 3 { max-width: 540px; }
@@ -434,13 +425,8 @@ with demo:
434
  ##### ''')
435
  selected_translation_lang_2.render()
436
  translate_transcriptions_button = gr.Button("Step 3. Translate transcription")
437
- translate_transcriptions_button.click(translate_transcriptions, [transcription_df, selected_translation_lang_2], transcription_and_translation_df, srt_file)
438
  transcription_and_translation_df.render()
439
-
440
- with gr.Row():
441
- with gr.Column():
442
- gr.Markdown('''##### From here you can download the srt-file ''')
443
- srt_file.render()
444
 
445
  with gr.Row():
446
  with gr.Column():
@@ -449,10 +435,9 @@ with demo:
449
  ##### ''')
450
  translate_and_make_srt_btn = gr.Button("Step 4. Create and burn srt to video")
451
  print(video_in)
452
- translate_and_make_srt_btn.click(create_srt_and_burn, [srt_file,video_in], [
453
  video_out])
454
  video_out.render()
455
 
456
-
457
 
458
  demo.launch()
 
269
 
270
  print("translations done")
271
 
272
+ return df
273
 
274
+ def create_srt_and_burn(df, video_in):
275
+
276
+ print("Starting creation of video wit srt")
277
+
278
+
279
+ with open('testi.srt','w', encoding="utf-8") as file:
280
  for i in range(len(df)):
281
  file.write(str(i+1))
282
  file.write('\n')
 
284
 
285
 
286
  milliseconds = round(start * 1000.0)
287
+
288
  hours = milliseconds // 3_600_000
289
  milliseconds -= hours * 3_600_000
290
+
291
  minutes = milliseconds // 60_000
292
  milliseconds -= minutes * 60_000
293
+
294
  seconds = milliseconds // 1_000
295
  milliseconds -= seconds * 1_000
296
+
297
  file.write(f"{hours}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}")
298
 
299
  stop = df.iloc[i]['end']
300
 
301
 
302
  milliseconds = round(stop * 1000.0)
303
+
304
  hours = milliseconds // 3_600_000
305
  milliseconds -= hours * 3_600_000
306
+
307
  minutes = milliseconds // 60_000
308
  milliseconds -= minutes * 60_000
309
+
310
  seconds = milliseconds // 1_000
311
  milliseconds -= seconds * 1_000
312
+
313
 
314
  file.write(' --> ')
315
  file.write(f"{hours}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}")
 
318
  if int(i) != len(df)-1:
319
  file.write('\n\n')
320
 
321
+ print("SRT DONE")
 
 
 
 
 
 
 
322
  try:
323
+ file1 = open('./testi.srt', 'r', encoding="utf-8")
324
  Lines = file1.readlines()
325
 
326
  count = 0
 
334
 
335
  video_out = video_in.replace('.mp4', '_out.mp4')
336
  print(video_out)
337
+ command = 'ffmpeg -i "{}" -y -vf subtitles=./testi.srt "{}"'.format(video_in, video_out)
338
  print(command)
339
  os.system(command)
340
+ return video_out
341
  except Exception as e:
342
  print(e)
343
  return video_out
 
357
  transcription_df = gr.DataFrame(value=df_init,label="Transcription dataframe", row_count=(0, "dynamic"), max_rows = 10, wrap=True, overflow_row_behaviour='paginate')
358
  transcription_and_translation_df = gr.DataFrame(value=df_init,label="Transcription and translation dataframe", max_rows = 10, wrap=True, overflow_row_behaviour='paginate')
359
 
 
 
 
 
 
 
 
360
  demo = gr.Blocks(css='''
361
  #cut_btn, #reset_btn { align-self:stretch; }
362
  #\\31 3 { max-width: 540px; }
 
425
  ##### ''')
426
  selected_translation_lang_2.render()
427
  translate_transcriptions_button = gr.Button("Step 3. Translate transcription")
428
+ translate_transcriptions_button.click(translate_transcriptions, [transcription_df, selected_translation_lang_2], transcription_and_translation_df)
429
  transcription_and_translation_df.render()
 
 
 
 
 
430
 
431
  with gr.Row():
432
  with gr.Column():
 
435
  ##### ''')
436
  translate_and_make_srt_btn = gr.Button("Step 4. Create and burn srt to video")
437
  print(video_in)
438
+ translate_and_make_srt_btn.click(create_srt_and_burn, [transcription_and_translation_df,video_in], [
439
  video_out])
440
  video_out.render()
441
 
 
442
 
443
  demo.launch()