JiaenLiu commited on
Commit
e10f7f9
·
2 Parent(s): 03b08f3 dd5c13c

Merge pull request #23 from project-kxkg/eason/refactor

Browse files

Eason/refactor to test the log function

Former-commit-id: c3a243de07855712925578c1aa4749153ef05bdd

Files changed (2) hide show
  1. SRT.py +6 -6
  2. pipeline.py +23 -9
SRT.py CHANGED
@@ -420,7 +420,7 @@ class SRT_script():
420
  n = 0
421
  return real_word, len(word)+n
422
 
423
- def realtime_write_srt(self,path,range,length,idx):
424
  start_seg_id = range[0]
425
  end_seg_id = range[1]
426
  with open(path, "a", encoding='utf-8') as f:
@@ -428,19 +428,19 @@ class SRT_script():
428
  # f.write(f'{i+idx}\n')
429
  # f.write(seg.get_trans_str())
430
  for i, seg in enumerate(self.segments):
431
- if i<range[0]-1: continue
432
- if i>=range[1] + length:break
433
  f.write(f'{i+idx}\n')
434
  f.write(seg.get_trans_str())
435
  pass
436
 
437
- def realtime_bilingual_write_srt(self,path,range,length,idx):
438
  start_seg_id = range[0]
439
  end_seg_id = range[1]
440
  with open(path, "a", encoding='utf-8') as f:
441
  for i, seg in enumerate(self.segments):
442
- if i<range[0]-1: continue
443
- if i>=range[1] + length:break
444
  f.write(f'{i+idx}\n')
445
  f.write(seg.get_bilingual_str())
446
  pass
 
420
  n = 0
421
  return real_word, len(word)+n
422
 
423
+ def realtime_write_srt(self,path,range,length, idx):
424
  start_seg_id = range[0]
425
  end_seg_id = range[1]
426
  with open(path, "a", encoding='utf-8') as f:
 
428
  # f.write(f'{i+idx}\n')
429
  # f.write(seg.get_trans_str())
430
  for i, seg in enumerate(self.segments):
431
+ if i<range[0]-1 : continue
432
+ if i>=range[1] + length :break
433
  f.write(f'{i+idx}\n')
434
  f.write(seg.get_trans_str())
435
  pass
436
 
437
+ def realtime_bilingual_write_srt(self,path,range, length,idx):
438
  start_seg_id = range[0]
439
  end_seg_id = range[1]
440
  with open(path, "a", encoding='utf-8') as f:
441
  for i, seg in enumerate(self.segments):
442
+ if i<range[0]-1 : continue
443
+ if i>=range[1] + length :break
444
  f.write(f'{i+idx}\n')
445
  f.write(seg.get_bilingual_str())
446
  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
 
@@ -22,7 +23,7 @@ parser.add_argument("-v", help="auto encode script with video", action='store_tr
22
  args = parser.parse_args()
23
 
24
  # input should be either video file or youtube video link.
25
- if args.link is None and args.video_file is None and args.srt_file is None:
26
  print("need video source or srt file")
27
  exit()
28
 
@@ -49,6 +50,8 @@ if args.video_name == 'placeholder' :
49
  VIDEO_NAME = args.srt_file.split('/')[-1].split('.')[0]
50
  else:
51
  VIDEO_NAME = args.video_name
 
 
52
 
53
  model_name = args.model_name
54
 
@@ -87,13 +90,22 @@ if args.link is not None and args.video_file is None:
87
  elif args.video_file is not None:
88
  # Read from local
89
  video_path = args.video_file
 
90
  if args.audio_file is not None:
91
  audio_file= open(args.audio_file, "rb")
92
  audio_path = args.audio_file
93
  else:
94
- os.system(f'ffmpeg -i {args.video_file} -f mp3 -ab 192000 -vn {DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3')
95
- audio_file= open(f'{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3', "rb")
96
- audio_path = f'{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3'
 
 
 
 
 
 
 
 
97
 
98
  if not os.path.exists(f'{RESULT_PATH}/{VIDEO_NAME}'):
99
  os.mkdir(f'{RESULT_PATH}/{VIDEO_NAME}')
@@ -247,7 +259,11 @@ def get_response(model_name, sentence):
247
 
248
 
249
  # Translate and save
 
250
  for sentence, range in tqdm(zip(script_arr, range_arr)):
 
 
 
251
  # using chatgpt model
252
  print(f"now translating sentences {range}")
253
  flag = True
@@ -261,14 +277,12 @@ for sentence, range in tqdm(zip(script_arr, range_arr)):
261
  time.sleep(30)
262
  flag = True
263
  # add read-time output back and modify the post-processing by using one batch as an unit.
264
- srt.set_translation(translate, range, model_name,args.link)
265
 
266
- add_length = srt.check_len_and_split_range(range, threshold)
267
- srt.realtime_write_srt(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt",range, add_length,segidx)
268
  # srt.realtime_bilingual_write_srt(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_bi.srt",range, add_length,segidx)
269
 
270
- # srt.check_len_and_split()
271
- # srt.write_srt_file_translate(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt")
272
  # srt.write_srt_file_bilingual(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_bi.srt")
273
 
274
  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
 
 
23
  args = parser.parse_args()
24
 
25
  # input should be either video file or youtube video link.
26
+ if args.link is None and args.video_file is None and args.srt_file is None and args.audio_file is None:
27
  print("need video source or srt file")
28
  exit()
29
 
 
50
  VIDEO_NAME = args.srt_file.split('/')[-1].split('.')[0]
51
  else:
52
  VIDEO_NAME = args.video_name
53
+ else:
54
+ VIDEO_NAME = args.video_name
55
 
56
  model_name = args.model_name
57
 
 
90
  elif args.video_file is not None:
91
  # Read from local
92
  video_path = args.video_file
93
+ # audio_path = "{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3".format(DOWNLOAD_PATH, VIDEO_NAME)
94
  if args.audio_file is not None:
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
 
277
  time.sleep(30)
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
 
 
 
282
  # srt.realtime_bilingual_write_srt(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_bi.srt",range, add_length,segidx)
283
 
284
+ srt.check_len_and_split()
285
+ srt.write_srt_file_translate(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt")
286
  # srt.write_srt_file_bilingual(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_bi.srt")
287
 
288
  if not args.only_srt: