Eason Lu commited on
Commit
144c78b
1 Parent(s): 4f95b2f

add video option in pipeline

Browse files

Former-commit-id: c7f057ced06adf0ba3d6f5d4040c09693f267eed

Files changed (3) hide show
  1. .mp3 +0 -0
  2. pipeline.py +38 -12
  3. placeholder.mp3 +0 -0
.mp3 ADDED
Binary file (657 kB). View file
 
pipeline.py CHANGED
@@ -8,17 +8,19 @@ import ffmpeg
8
 
9
  parser = argparse.ArgumentParser()
10
  parser.add_argument("--link", help="youtube video link here", default=None, type=str, required=False)
11
- parser.add_argument("--local_path", help="local video path here", default=None, type=str, required=False)
 
12
  parser.add_argument("--srt_file", help="srt file input path here", default=None, type=str, required=False) # New argument
13
  parser.add_argument("--download", help="download path", default='./downloads', type=str, required=False)
14
  parser.add_argument("--output_dir", help="translate result path", default='./results', type=str, required=False)
15
  parser.add_argument("--video_name", help="video name, if use video link as input, the name will auto-filled by youtube video name", default='placeholder', type=str, required=False)
16
  parser.add_argument("--model_name", help="model name only support text-davinci-003 and gpt-3.5-turbo", type=str, required=False, default="gpt-3.5-turbo")
17
- parser.add_argument("-ass", help="set script output to .ass file", action='store_true')
 
18
  args = parser.parse_args()
19
 
20
  # input should be either video file or youtube video link.
21
- if args.link is None and args.local_path is None and args.srt_file is None:
22
  print("need video source or srt file")
23
  exit()
24
 
@@ -38,30 +40,47 @@ VIDEO_NAME = args.video_name
38
  model_name = args.model_name
39
 
40
  # get source audio
41
- if args.link is not None and args.local_path is None:
42
  # Download audio from YouTube
43
  video_link = args.link
44
  video = None
45
  audio = None
46
  try:
47
- video = YouTube(video_link)
48
- audio = video.streams.filter(only_audio=True, file_extension='mp4').first()
 
 
 
 
 
 
49
  if audio:
50
  audio.download(f'{DOWNLOAD_PATH}/audio')
51
- print('Download Completed!')
52
  else:
53
  print("Error: Audio stream not found")
54
  except Exception as e:
55
  print("Connection Error")
56
  print(e)
 
 
 
 
57
  audio_path = '{}/audio/{}'.format(DOWNLOAD_PATH, audio.default_filename)
58
  audio_file = open(audio_path, "rb")
59
  if VIDEO_NAME == 'placeholder':
60
  VIDEO_NAME = audio.default_filename.split('.')[0]
61
- elif args.local_path is not None:
62
  # Read from local
63
- audio_file= open(args.local_path, "rb")
64
- audio_path = args.local_path
 
 
 
 
 
 
 
65
 
66
  if not os.path.exists(f'{RESULT_PATH}/{VIDEO_NAME}'):
67
  os.mkdir(f'{RESULT_PATH}/{VIDEO_NAME}')
@@ -94,7 +113,7 @@ else:
94
  script_en = f.read()
95
  script_input = script_en
96
 
97
- if args.ass:
98
  from srt2ass import srt2ass
99
  assSub_en = srt2ass(srt_file_en, "default", "No", "Modest")
100
  print('ASS subtitle saved as: ' + assSub_en)
@@ -145,6 +164,13 @@ for s in script_arr:
145
 
146
  with open(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt", 'a+') as f:
147
  f.write(response['choices'][0]['text'].strip())
148
- if args.ass:
 
149
  assSub_zh = srt2ass(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt", "default", "No", "Modest")
150
  print('ASS subtitle saved as: ' + assSub_zh)
 
 
 
 
 
 
 
8
 
9
  parser = argparse.ArgumentParser()
10
  parser.add_argument("--link", help="youtube video link here", default=None, type=str, required=False)
11
+ parser.add_argument("--video_file", help="local video path here", default=None, type=str, required=False)
12
+ parser.add_argument("--audio_file", help="local audio path here", default=None, type=str, required=False)
13
  parser.add_argument("--srt_file", help="srt file input path here", default=None, type=str, required=False) # New argument
14
  parser.add_argument("--download", help="download path", default='./downloads', type=str, required=False)
15
  parser.add_argument("--output_dir", help="translate result path", default='./results', type=str, required=False)
16
  parser.add_argument("--video_name", help="video name, if use video link as input, the name will auto-filled by youtube video name", default='placeholder', type=str, required=False)
17
  parser.add_argument("--model_name", help="model name only support text-davinci-003 and gpt-3.5-turbo", type=str, required=False, default="gpt-3.5-turbo")
18
+ parser.add_argument("-only_srt", help="set script output to only .srt file", action='store_true')
19
+ parser.add_argument("-v", help="auto encode script with video", action='store_true')
20
  args = parser.parse_args()
21
 
22
  # input should be either video file or youtube video link.
23
+ if args.link is None and args.video_file is None and args.srt_file is None:
24
  print("need video source or srt file")
25
  exit()
26
 
 
40
  model_name = args.model_name
41
 
42
  # get source audio
43
+ if args.link is not None and args.video_file is None:
44
  # Download audio from YouTube
45
  video_link = args.link
46
  video = None
47
  audio = None
48
  try:
49
+ yt = YouTube(video_link)
50
+ video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
51
+ if video:
52
+ video.download(f'{DOWNLOAD_PATH}/video')
53
+ print('Video download completed!')
54
+ else:
55
+ print("Error: Video stream not found")
56
+ audio = yt.streams.filter(only_audio=True, file_extension='mp4').first()
57
  if audio:
58
  audio.download(f'{DOWNLOAD_PATH}/audio')
59
+ print('Audio download completed!')
60
  else:
61
  print("Error: Audio stream not found")
62
  except Exception as e:
63
  print("Connection Error")
64
  print(e)
65
+ exit()
66
+
67
+ video_path = f'{DOWNLOAD_PATH}/video/{video.default_filename}'
68
+ # video_file = open(video_path, "rb")
69
  audio_path = '{}/audio/{}'.format(DOWNLOAD_PATH, audio.default_filename)
70
  audio_file = open(audio_path, "rb")
71
  if VIDEO_NAME == 'placeholder':
72
  VIDEO_NAME = audio.default_filename.split('.')[0]
73
+ elif args.video_file is not None:
74
  # Read from local
75
+ # video_file = open(args.video_file, "rb")
76
+ video_path = args.video_file
77
+ if args.audio_file is not None:
78
+ audio_file= open(args.audio_file, "rb")
79
+ audio_path = args.audio_file
80
+ else:
81
+ os.system(f'ffmpeg -i {args.video_file} -f mp3 -ab 192000 -vn {DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3')
82
+ audio_file= open(f'{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3', "rb")
83
+ audio_path = f'{DOWNLOAD_PATH}/audio/{VIDEO_NAME}.mp3'
84
 
85
  if not os.path.exists(f'{RESULT_PATH}/{VIDEO_NAME}'):
86
  os.mkdir(f'{RESULT_PATH}/{VIDEO_NAME}')
 
113
  script_en = f.read()
114
  script_input = script_en
115
 
116
+ if not args.only_srt:
117
  from srt2ass import srt2ass
118
  assSub_en = srt2ass(srt_file_en, "default", "No", "Modest")
119
  print('ASS subtitle saved as: ' + assSub_en)
 
164
 
165
  with open(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt", 'a+') as f:
166
  f.write(response['choices'][0]['text'].strip())
167
+
168
+ if not args.only_srt:
169
  assSub_zh = srt2ass(f"{RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt", "default", "No", "Modest")
170
  print('ASS subtitle saved as: ' + assSub_zh)
171
+
172
+ if args.v:
173
+ if args.only_srt:
174
+ os.system(f'ffmpeg -i {video_path} -vf "subtitles={RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.srt" {RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}.mp4')
175
+ else:
176
+ os.system(f'ffmpeg -i {video_path} -vf "subtitles={RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}_zh.ass" {RESULT_PATH}/{VIDEO_NAME}/{VIDEO_NAME}.mp4')
placeholder.mp3 ADDED
Binary file (657 kB). View file