JiaenLiu commited on
Commit
ff51822
·
1 Parent(s): 08c7492

output fixed

Browse files

Former-commit-id: 25a186c46fc6829e3f0d708612f710324154c258

Files changed (2) hide show
  1. SRT.py +6 -6
  2. pipeline.py +22 -7
SRT.py CHANGED
@@ -421,7 +421,7 @@ class SRT_script():
421
  n = 0
422
  return real_word, len(word)+n
423
 
424
- def realtime_write_srt(self,path,range,length,idx):
425
  start_seg_id = range[0]
426
  end_seg_id = range[1]
427
  with open(path, "a", encoding='utf-8') as f:
@@ -429,19 +429,19 @@ class SRT_script():
429
  # f.write(f'{i+idx}\n')
430
  # f.write(seg.get_trans_str())
431
  for i, seg in enumerate(self.segments):
432
- if i<range[0]-1: continue
433
- if i>=range[1] + length:break
434
  f.write(f'{i+idx}\n')
435
  f.write(seg.get_trans_str())
436
  pass
437
 
438
- def realtime_bilingual_write_srt(self,path,range,length,idx):
439
  start_seg_id = range[0]
440
  end_seg_id = range[1]
441
  with open(path, "a", encoding='utf-8') as f:
442
  for i, seg in enumerate(self.segments):
443
- if i<range[0]-1: continue
444
- if i>=range[1] + length:break
445
  f.write(f'{i+idx}\n')
446
  f.write(seg.get_bilingual_str())
447
  pass
 
421
  n = 0
422
  return real_word, len(word)+n
423
 
424
+ def realtime_write_srt(self,path,range,length, idx):
425
  start_seg_id = range[0]
426
  end_seg_id = range[1]
427
  with open(path, "a", encoding='utf-8') as f:
 
429
  # f.write(f'{i+idx}\n')
430
  # f.write(seg.get_trans_str())
431
  for i, seg in enumerate(self.segments):
432
+ if i<range[0]-1 : continue
433
+ if i>=range[1] + length :break
434
  f.write(f'{i+idx}\n')
435
  f.write(seg.get_trans_str())
436
  pass
437
 
438
+ def realtime_bilingual_write_srt(self,path,range, length,idx):
439
  start_seg_id = range[0]
440
  end_seg_id = range[1]
441
  with open(path, "a", encoding='utf-8') as f:
442
  for i, seg in enumerate(self.segments):
443
+ if i<range[0]-1 : continue
444
+ if i>=range[1] + length :break
445
  f.write(f'{i+idx}\n')
446
  f.write(seg.get_bilingual_str())
447
  pass
pipeline.py CHANGED
@@ -5,6 +5,7 @@ import os
5
  from tqdm import tqdm
6
  from SRT import SRT_script
7
  import stable_whisper
 
8
 
9
  import time
10
 
@@ -94,9 +95,17 @@ elif args.video_file is not None:
94
  audio_file= open(args.audio_file, "rb")
95
  audio_path = args.audio_file
96
  else:
97
- os.system(f'ffmpeg -i {args.video_file} -f mp3 -ab 192000 -vn {DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3')
98
- audio_file= open(f'{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3', "rb")
99
- audio_path = f'{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3'
 
 
 
 
 
 
 
 
100
 
101
  if not os.path.exists(f'{RESULT_PATH}/{VIDEO_NAME}'):
102
  os.mkdir(f'{RESULT_PATH}/{VIDEO_NAME}')
@@ -250,7 +259,11 @@ def get_response(model_name, sentence):
250
 
251
 
252
  # Translate and save
 
253
  for sentence, range in tqdm(zip(script_arr, range_arr)):
 
 
 
254
  # using chatgpt model
255
  print(f"now translating sentences {range}")
256
  flag = True
@@ -265,12 +278,14 @@ for sentence, range in tqdm(zip(script_arr, range_arr)):
265
  flag = True
266
  # add read-time output back and modify the post-processing by using one batch as an unit.
267
  srt.set_translation(translate, range, model_name)
268
- add_length = srt.check_len_and_split_range(range, threshold)
269
- srt.realtime_write_srt(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt",range, add_length,segidx)
 
 
270
  # srt.realtime_bilingual_write_srt(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_bi.srt",range, add_length,segidx)
271
 
272
- # srt.check_len_and_split()
273
- # srt.write_srt_file_translate(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt")
274
  # srt.write_srt_file_bilingual(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_bi.srt")
275
 
276
  if not args.only_srt:
 
5
  from tqdm import tqdm
6
  from SRT import SRT_script
7
  import stable_whisper
8
+ import subprocess
9
 
10
  import time
11
 
 
95
  audio_file= open(args.audio_file, "rb")
96
  audio_path = args.audio_file
97
  else:
98
+ # escaped_video_path = args.video_file.replace('(', '\(').replace(')', '\)').replace(' ', '\ ')
99
+ # print(escaped_video_path)
100
+ # os.system(f'ffmpeg -i {escaped_video_path} -f mp3 -ab 192000 -vn {DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3')
101
+ # audio_file= open(f'{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3', "rb")
102
+ # audio_path = f'{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3'
103
+ output_audio_path = f'{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3'
104
+ # print(video_path)
105
+ # print(output_audio_path)
106
+ subprocess.run(['ffmpeg', '-i', video_path, '-f', 'mp3', '-ab', '192000', '-vn', output_audio_path])
107
+ audio_file = open(output_audio_path, "rb")
108
+ audio_path = output_audio_path
109
 
110
  if not os.path.exists(f'{RESULT_PATH}/{VIDEO_NAME}'):
111
  os.mkdir(f'{RESULT_PATH}/{VIDEO_NAME}')
 
259
 
260
 
261
  # Translate and save
262
+ previous_length = 0
263
  for sentence, range in tqdm(zip(script_arr, range_arr)):
264
+ # update the range based on previous length
265
+ range = (range[0]+previous_length, range[1]+previous_length)
266
+
267
  # using chatgpt model
268
  print(f"now translating sentences {range}")
269
  flag = True
 
278
  flag = True
279
  # add read-time output back and modify the post-processing by using one batch as an unit.
280
  srt.set_translation(translate, range, model_name)
281
+ # add_length = srt.check_len_and_split_range(range, threshold)
282
+ # srt.realtime_write_srt(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt",range, add_length ,segidx)
283
+ # # save current length as previous length
284
+ # previous_length = add_length
285
  # srt.realtime_bilingual_write_srt(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_bi.srt",range, add_length,segidx)
286
 
287
+ srt.check_len_and_split()
288
+ srt.write_srt_file_translate(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt")
289
  # srt.write_srt_file_bilingual(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_bi.srt")
290
 
291
  if not args.only_srt: