-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvmaker.py
More file actions
260 lines (210 loc) · 9.3 KB
/
svmaker.py
File metadata and controls
260 lines (210 loc) · 9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import moviepy.editor as mpy
import pydub as pdb
import numpy as np
import matplotlib.pyplot as plt
#import cv2
import moviepy.video.fx.all as vfx
from collections import defaultdict
import moviepy.video.fx.all as vfx
import os
# CONSTANTS
PREVIEW_SIZE = (1280, 720)
#PRODUCTION_SIZE = (1024, 768)
def resize_and_fit(origin_clip, video_size):
new_size = (0, 0) # init the value
if origin_clip.h / origin_clip.w > PREVIEW_SIZE[1] / PREVIEW_SIZE[0]: # EH: when the image is square, change it according to video size
new_size = ((origin_clip.w * video_size[1] / origin_clip.h),video_size[1])
else:
new_size = ((video_size[0], origin_clip.h * video_size[0] / origin_clip.w))
clip = origin_clip.resize(new_size).on_color(video_size)
return clip
def set_scene_dur(origin_clip, dur, fps=25):
clip = origin_clip.set_duration(dur).set_fps(fps)
return clip
def set_img_dur(origin_clip, dur, fps=25):
clip = origin_clip.set_duration(dur).set_fps(fps)
return clip
def set_video_dur(origin_clip, dur, fps=25):
clip = vfx.speedx(origin_clip, final_duration=dur).set_fps(fps)
return clip
def generate_subtitles_clip(subtitles, fontsize=60, color="white", stroke_width=2, stroke_color="black"):
'''
params:
* subtitles: parse script
'''
text_clips = []
for content in subtitles:
if isinstance(content, SContent):
text_content = mpy.TextClip(content.text,
color=color,
stroke_color= stroke_color,
stroke_width= stroke_width,
font="ArialUnicode", # TODO: change the font
fontsize=fontsize
)
text_on_color = text_content.on_color(PREVIEW_SIZE, pos=('center', 'bottom') ,col_opacity=0)
text_clip = text_on_color.set_duration(content.dur)
text_clips.append(text_clip)
# EH: add some spaces between subtitles
return mpy.concatenate_videoclips(text_clips)
class SCommand():
def __init__(self, command_str):
self.command_str = command_str
self.command_parsed = defaultdict(list)
self.parse()
def parse(self):
command_factors = self.command_str.split()
command_factors = [factor for factor in command_factors if factor] # Clean blank lines
param_name = "position_args"
for i in range(len(command_factors)):
factor = command_factors[i]
if i == 0:
self.command_parsed['func'] = factor
elif factor[0:2] == "--":
param_name = factor[2:]
else:
# Append the param
if factor.replace(".", "", 1).isdigit(): # check if it is float
factor = float(factor)
elif ":" in factor:
if factor.count(":") == 1: # if hour is forgot to be written
if len(factor.split(":")[0]) == 1: # if 0 is forgot to be written
factor = "00:0" + factor
else:
factor = "00:" + factor
self.command_parsed[param_name].append(factor)
class SContent():
def __init__(self, text, audio=0, dur=1.5):
self.text = text
self.audio = audio
self.dur = dur # EH: adjust the dur according to audio
# EH: change a position to set the text
if len(self.text) > 18:
self.text = text[:12] + "\n" + text[12:]
def parse_script(script):
script_lines = [line for line in script.split("\n") if line]
parsed = []
for line in script_lines:
if line[0] == "$":
parsed.append(SCommand(line[1:]))
else:
parsed.append(SContent(line))
return parsed
def get_scene_transition_schedule(parsed_script):
# EH: change this into index based method. This method is too ugly.
'''
return: a list of tuples like (scene_filename, dur, params)
'''
schedule = []
current_scene = ""
dur = 0
params = {}
for line in parsed_script:
if isinstance(line, SCommand):
if line.command_parsed["func"] == "ST":
if current_scene: # If 'line' is not the first scene, append the previous scene
schedule.append((current_scene, dur, params))
current_scene = line.command_parsed["position_args"][0]
dur = 0
# EH: change the way params are added
params = {"part": line.command_parsed['part'], "crop": line.command_parsed['crop']}
else:
if isinstance(line, SContent):
dur += line.dur
# Add the final scene after all lines are schedule
schedule.append((current_scene, dur, params))
return schedule
def scheduled_time_scene_transition(schedule, resource_folder_name="res"):
'''
params:
- schedule: a list of tuples of (file name, dur)
'''
clips = []
print(schedule)#DEBUG
for res, dur, params in schedule:
# EH: use a better way to detect the type of a file
file_name = os.path.join(resource_folder_name, res)
if not os.path.exists(file_name):
print("File not found! {}".format(file_name))
raise FileNotFoundError()
file_type = res.split(".")[-1]
if file_type in ["mov", "mp4", "avi", "flv"]:
origin_video_clip = mpy.VideoFileClip(os.path.join(resource_folder_name, res), audio=False)
if params["part"]:
#print(params["part"])
parts = params["part"]
origin_video_clip = origin_video_clip.subclip(parts[0], parts[1])
if params["crop"]:
w = origin_video_clip.w
h = origin_video_clip.h
rect = params["crop"]
origin_video_clip = vfx.crop(origin_video_clip, w*rect[0], h*rect[1], w*rect[2], h*rect[3])
clips.append(set_video_dur(resize_and_fit(origin_video_clip, PREVIEW_SIZE), dur))
elif file_type in ["jpg", "png", "jpeg"]:
origin_img_clip = mpy.ImageClip(os.path.join(resource_folder_name, res))
if params["crop"]:
w = origin_img_clip.w
h = origin_img_clip.h
rect = params["crop"]
#print("Crop", w, h, rect, rect[0]*w)
origin_img_clip = vfx.crop(origin_img_clip, w*rect[0], h*rect[1], w*rect[2], h*rect[3])
clips.append(set_img_dur(resize_and_fit(origin_img_clip, PREVIEW_SIZE), dur))
elif file_type in ["txt"]:
print(res)
print(os.path.join(resource_folder_name, res))
origin_txt_clip = mpy.TextClip(
open(os.path.join(resource_folder_name, res)).read(),
color="white",
font="ArialUnicode",
fontsize=100
).on_color(PREVIEW_SIZE).set_position("center")
clips.append(set_scene_dur(resize_and_fit(origin_txt_clip, PREVIEW_SIZE), dur))
return mpy.concatenate_videoclips(clips)
def get_chunks(audio):
chunks = pdb.silence.split_on_silence(audio.normalize(), min_silence_len=1000, silence_thresh=-40, keep_silence=250)
new_chunks = []
for chunk in chunks:
dur = round(chunk.duration_seconds, 1) + 0.1
new_chunk = (chunk + pdb.AudioSegment.silent())[:dur*1000]
new_chunks.append(new_chunk)
return new_chunks
def match_audio(parsed_script, chunks):
# EH: normalize the volume of audio at first and then divide it into chunks
for line in parsed_script:
if isinstance(line, SContent) and chunks:
line.audio = chunks.pop(0) # Get the main audio before chunks are removed
line.dur = line.audio.duration_seconds
def generate_video():
# Parse script
script = ""
with open("script.txt", "r") as f:
script = f.read()
parsed_script = parse_script(script)
print("Script parsed.")
# Generate audio
if "audio.wav" in os.listdir("."):
print("Audio detected.")
chunks = get_chunks(pdb.AudioSegment.from_wav("audio.wav"))
print("Audio chunks generated.")
sum(chunks).export("audio_track.wav", "wav")
# Export to file first, then match
match_audio(parsed_script, chunks)
# Generate subtitles
subtitle_clip = generate_subtitles_clip(parsed_script)
print("Subtitles generated.")
# Generate scenes
schedule = get_scene_transition_schedule(parsed_script)
video_clip = scheduled_time_scene_transition(schedule)
print("Scene generated.")
# Generate the video
main_clip = mpy.CompositeVideoClip([video_clip, subtitle_clip])
# Add the audio track
if "audio_track.wav" in os.listdir("."):
audio_clip = mpy.AudioFileClip('audio_track.wav')
if "BGM.mp3" in os.listdir(".") or "BGM.flac" in os.listdir("."):
audio_clip = mpy.CompositeAudioClip([mpy.AudioFileClip('audio_track.wav'), mpy.AudioFileClip("BGM.mp3").volumex(0.15)])
main_clip = main_clip.set_audio(audio_clip.set_duration(main_clip.duration))
# Write the video
main_clip.write_videofile("output.mp4")
if __name__ == "__main__":
generate_video()