Eason Lu commited on
Commit
0091da5
2 Parent(s): 5a7c441 09cabee

solve conflict

Browse files

Former-commit-id: c9d4906675aac0476496e62adf2e6482e75c0bca

.gitignore CHANGED
@@ -3,4 +3,4 @@
3
  .DS_Store
4
  test.py
5
  test.srt
6
- test.txt
 
3
  .DS_Store
4
  test.py
5
  test.srt
6
+ test.txt
README.md CHANGED
@@ -19,10 +19,11 @@ example offline:
19
  python3 pipeline.py --audio_file test_translation.m4a --result ./results --video_name test_translation
20
  ```
21
 
22
- example srt input:
23
- ```
24
- python pipeline.py --text_file "/home/jiaenliu/project-t/results/huanghe_translation_en.txt" --result "/home/jiaenliu/project-t/results" --video_name "huanghe_test"
25
- ```
 
26
 
27
  ## Usage
28
  ```
 
19
  python3 pipeline.py --audio_file test_translation.m4a --result ./results --video_name test_translation
20
  ```
21
 
22
+ python3 pipeline.py --link https://www.youtube.com/watch?v=VrigMmXt9A0 --video_name Ukraine_and_its_Global_Impact
23
+
24
+ python3 pipeline.py --video_file '/home/jiaenliu/project-t/downloads/audio/Ukraine_and_its_Global_Impact.mp4' -v --video_name Ukraine_and_its_Global_Impact
25
+
26
+ example offline: python3 pipeline.py --local_path test_translation.m4a --result ./results --video_name test_translation
27
 
28
  ## Usage
29
  ```
SRT.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datetime import timedelta
2
+ import os
3
+ import whisper
4
+
5
+ class SRT_segment(object):
6
+ def __init__(self, segment) -> None:
7
+ self.start_time_str = str(0)+str(timedelta(seconds=int(segment['start'])))+',000'
8
+ self.end_time_str = str(0)+str(timedelta(seconds=int(segment['end'])))+',000'
9
+ self.segment_id = segment['id']+1
10
+ self.source_text = segment['text']
11
+ self.duration = f"{self.start_time_str} --> {self.end_time_str}"
12
+ self.translation = ""
13
+
14
+
15
+ class SRT_script():
16
+ def __init__(self, segments) -> None:
17
+ self.segments = []
18
+ for seg in segments:
19
+ srt_seg = SRT_segment(seg)
20
+ self.segments.append(srt_seg)
21
+
22
+ def get_source_only():
23
+ # return a string
24
+ pass
25
+
26
+ def write_srt_file(path:str):
27
+ # write srt file to path
28
+ pass
29
+
30
+
__pycache__/{srt2ass.cpython-310.pyc → srt2ass.cpython-38.pyc} RENAMED
File without changes
pipeline.py CHANGED
@@ -2,9 +2,7 @@ import openai
2
  from pytube import YouTube
3
  import argparse
4
  import os
5
- import io
6
  import whisper
7
- import ffmpeg
8
  from tqdm import tqdm
9
 
10
  parser = argparse.ArgumentParser()
@@ -66,14 +64,12 @@ if args.link is not None and args.video_file is None:
66
  exit()
67
 
68
  video_path = f'{DOWNLOAD_PATH}/video/{video.default_filename}'
69
- # video_file = open(video_path, "rb")
70
  audio_path = '{}/audio/{}'.format(DOWNLOAD_PATH, audio.default_filename)
71
  audio_file = open(audio_path, "rb")
72
  if VIDEO_NAME == 'placeholder':
73
  VIDEO_NAME = audio.default_filename.split('.')[0]
74
  elif args.video_file is not None:
75
  # Read from local
76
- # video_file = open(args.video_file, "rb")
77
  video_path = args.video_file
78
  if args.audio_file is not None:
79
  audio_file= open(args.audio_file, "rb")
@@ -162,17 +158,17 @@ script_input_withForceTerm = re.sub('\n ', '\n', "".join(ready_words))
162
 
163
 
164
  # Split the video script by sentences and create chunks within the token limit
165
- n_threshold = 1500 # Token limit for the GPT-3 model
166
- script_split = script_input_withForceTerm.split('.')
167
 
168
  script_arr = []
169
  script = ""
170
  for sentence in script_split:
171
  if len(script) + len(sentence) + 1 <= n_threshold:
172
- script += sentence + '.'
173
  else:
174
  script_arr.append(script.strip())
175
- script = sentence + '.'
176
  if script.strip():
177
  script_arr.append(script.strip())
178
 
@@ -185,12 +181,15 @@ for s in tqdm(script_arr):
185
  model=model_name,
186
  messages = [
187
  {"role": "system", "content": "You are a helpful assistant that translates English to Chinese and have decent background in starcraft2."},
 
 
188
  {"role": "user", "content": 'Translate the following English text to Chinese: "{}"'.format(s)}
189
  ],
190
  temperature=0.15
191
  )
192
  with open(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt", 'a+') as f:
193
  f.write(response['choices'][0]['message']['content'].strip())
 
194
 
195
  if model_name == "text-davinci-003":
196
  prompt = f"Please help me translate this into Chinese:\n\n{s}\n\n"
@@ -207,6 +206,7 @@ for s in tqdm(script_arr):
207
 
208
  with open(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt", 'a+') as f:
209
  f.write(response['choices'][0]['text'].strip())
 
210
 
211
  if not args.only_srt:
212
  assSub_zh = srt2ass(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt", "default", "No", "Modest")
 
2
  from pytube import YouTube
3
  import argparse
4
  import os
 
5
  import whisper
 
6
  from tqdm import tqdm
7
 
8
  parser = argparse.ArgumentParser()
 
64
  exit()
65
 
66
  video_path = f'{DOWNLOAD_PATH}/video/{video.default_filename}'
 
67
  audio_path = '{}/audio/{}'.format(DOWNLOAD_PATH, audio.default_filename)
68
  audio_file = open(audio_path, "rb")
69
  if VIDEO_NAME == 'placeholder':
70
  VIDEO_NAME = audio.default_filename.split('.')[0]
71
  elif args.video_file is not None:
72
  # Read from local
 
73
  video_path = args.video_file
74
  if args.audio_file is not None:
75
  audio_file= open(args.audio_file, "rb")
 
158
 
159
 
160
  # Split the video script by sentences and create chunks within the token limit
161
+ n_threshold = 1000 # Token limit for the GPT-3 model
162
+ script_split = script_input.split('\n')
163
 
164
  script_arr = []
165
  script = ""
166
  for sentence in script_split:
167
  if len(script) + len(sentence) + 1 <= n_threshold:
168
+ script += sentence + '\n'
169
  else:
170
  script_arr.append(script.strip())
171
+ script = sentence + '\n'
172
  if script.strip():
173
  script_arr.append(script.strip())
174
 
 
181
  model=model_name,
182
  messages = [
183
  {"role": "system", "content": "You are a helpful assistant that translates English to Chinese and have decent background in starcraft2."},
184
+ {"role": "system", "content": "Your translation has to keep the orginal format and be as accurate as possible."},
185
+ {"role": "system", "content": "There is no need for you to add any comments or notes."},
186
  {"role": "user", "content": 'Translate the following English text to Chinese: "{}"'.format(s)}
187
  ],
188
  temperature=0.15
189
  )
190
  with open(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt", 'a+') as f:
191
  f.write(response['choices'][0]['message']['content'].strip())
192
+ f.write("\n")
193
 
194
  if model_name == "text-davinci-003":
195
  prompt = f"Please help me translate this into Chinese:\n\n{s}\n\n"
 
206
 
207
  with open(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt", 'a+') as f:
208
  f.write(response['choices'][0]['text'].strip())
209
+ f.write("\n")
210
 
211
  if not args.only_srt:
212
  assSub_zh = srt2ass(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt", "default", "No", "Modest")