-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClipEditor.py
198 lines (166 loc) · 7.22 KB
/
ClipEditor.py
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
import ast
import glob
import logging
import random
import moviepy.video.fx.all as vfx
from moviepy.audio.AudioClip import CompositeAudioClip
from moviepy.audio.io.AudioFileClip import AudioFileClip
from moviepy.editor import VideoFileClip, concatenate_videoclips
from moviepy.video.VideoClip import ImageClip, ColorClip
from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
import config
import utils
logger = config.set_logger('ClipEditor.py')
def string_to_list(lst_str):
logger.info('Converting string to list...')
lst_str = lst_str.replace("*", ",")
return ast.literal_eval(lst_str)
def create(vid_num, gif_num, pic_num, sfx_num, result_path, interval_lst_str):
logger.info('Creating clip at create...')
interval_lst = string_to_list(interval_lst_str)
clip_editor = ClipEditor(vid_num, gif_num, pic_num, sfx_num, result_path, interval_lst)
clip_editor.create_clip()
clip_editor.download()
class ClipEditor(object):
def __init__(self, vid_num, gif_num, pic_num, sfx_num, result_path, interval_lst):
self.vid_num = int(vid_num)
self.gif_num = int(gif_num)
self.pic_num = int(pic_num)
self.sfx_num = int(sfx_num)
self.result_path = result_path
self.interval_lst = interval_lst
self.final_clip = None
self.clips = []
def create_clip(self):
logger.info('Creating clip at create_clip...')
def name(vid):
return vid[vid.rfind('/') + 1:-4]
vid_lst = glob.glob(config.VID_PATH + '*.mp4')
for vid in vid_lst:
vid_name = name(vid)
for interval in self.interval_lst:
if interval[0] == vid_name:
clip = VideoFileClip(vid).subclip(interval[1], interval[2]).resize((1280, 720))
clip = self.modify_clip(clip)
clip = self.decorate_clip(clip)
self.add_clip(clip)
@staticmethod
def modify_clip(clip):
logger.info('Modifying clip...')
m_type = random.randint(0, 3)
if m_type == 0:
m_clip = (clip.fx(vfx.mirror_y)
.fx(vfx.colorx, factor=random.randint(0, 50)))
#.fx(vfx.painting, saturation=random.uniform(0, 5), black=random.uniform(0, 0.01)))
elif m_type == 1:
m_clip = (clip.fx(vfx.invert_colors)
.fx(vfx.colorx, factor=random.randint(0, 50))
.fx(vfx.gamma_corr, gamma=random.uniform(0, 5)))
elif m_type == 2:
m_clip = (clip.fx(vfx.blackwhite)
.fx(vfx.gamma_corr, gamma=random.uniform(0, 5)))
else:
m_clip = (clip.fx(vfx.invert_colors)
.fx(vfx.mirror_y)
.fx(vfx.gamma_corr, gamma=random.uniform(0, 5)))
#.fx(vfx.painting, saturation=random.uniform(0, 5), black=random.uniform(0, 0.01)))
return m_clip
def decorate_clip(self, clip):
logging.info('Decorating clip...')
clip = self.add_screens(clip)
clip = self.add_pics(clip)
clip = self.add_gifs(clip)
clip = self.add_sfx(clip)
return clip
def add_screens(self, clip):
logger.info('Adding screens...')
screen_num = random.randint(3, 7)
screen_clip = [clip]
for i in range(screen_num):
x_pos, y_pos, x_size, y_size = self.generate_coordinates(clip)
screen = clip.volumex(0).resize((x_size, y_size)) \
.set_pos((x_pos, y_pos)).add_mask().rotate(random.randint(-180, 180))
screen = self.modify_clip(screen)
screen_clip.append(screen)
return CompositeVideoClip(screen_clip)
def add_gifs(self, clip):
logger.info('Adding gifs...')
gif_clip = [clip]
for i in range(self.gif_num):
x_pos, y_pos, x_size, y_size = self.generate_coordinates(clip)
gif_path = config.GIF_PATH + str(i) + '.gif'
gif = VideoFileClip(gif_path).loop(duration=clip.duration).set_duration(clip.duration) \
.fx(vfx.mask_color, color=[255, 255, 255]) \
.resize((x_size, y_size)).set_pos((x_pos, y_pos))
gif_clip.append(gif)
return CompositeVideoClip(gif_clip)
def add_pics(self, clip):
logger.info('Adding pics...')
pic_clip = [clip]
for i in range(self.pic_num):
x_pos, y_pos, x_size, y_size = self.generate_coordinates(clip)
pic_path = config.PIC_PATH + str(i) + '.jpg'
pic = ImageClip(pic_path).set_duration(clip.duration).resize((x_size, y_size)) \
.set_pos((x_pos, y_pos)).add_mask().rotate(random.randint(-180, 180))
pic_clip.append(pic)
return CompositeVideoClip(pic_clip)
def add_sfx(self, clip):
logger.info('Adding sfx...')
new_audio = [clip.audio]
for i in range(self.sfx_num):
sfx_path = config.SFX_PATH + str(i) + '.mp3'
sfx_clip = AudioFileClip(sfx_path)
if sfx_clip.duration > clip.duration:
sfx_clip = sfx_clip.set_duration(clip.duration)
new_audio.append(sfx_clip)
else:
new_audio.append(sfx_clip)
return clip.set_audio(CompositeAudioClip(new_audio))
@staticmethod
def generate_coordinates(clip):
logger.info('Generating coordinates...')
x_pos = random.randint(0, clip.w - 100)
y_pos = random.randint(0, clip.h - 100)
x_size = random.randint(100, clip.w)
y_size = random.randint(100, clip.h)
return x_pos, y_pos, x_size, y_size
def download(self):
logger.info('Downloading result...')
task = {'status': 'Downloading newly generated content...'}
utils.update_script(task)
self.set_final_clip()
self.add_intro()
self.write_final_clip()
def add_intro(self):
logger.info('Adding intro...')
intro = self.generate_intro()
new_final_clip = concatenate_videoclips([intro, self.get_final_clip()], method='compose')
self.update_final_clip(new_final_clip)
@staticmethod
def generate_intro():
logger.info('Generating intro...')
color = (255, 255, 255)
size = (1280, 720)
clip = ColorClip(size, color, duration=3)
logo = ImageClip(config.LOGO_PATH).set_duration(clip.duration) \
.resize(width=400, height=200) \
.set_pos(('center', 'center'))
return CompositeVideoClip([clip, logo])
def add_clip(self, clip):
logger.info('Adding clip...')
self.clips.append(clip)
def set_interval_lst(self, lst):
logger.info('Setting interval list...')
self.interval_lst = lst
def get_final_clip(self):
logger.info('Getting final clip...')
return self.final_clip
def set_final_clip(self):
logger.info('Setting final clip...')
self.final_clip = concatenate_videoclips(self.clips, method='compose').resize((1280, 720))
def update_final_clip(self, clip):
logger.info('Updating final clip...')
self.final_clip = clip
def write_final_clip(self):
logger.info('Writing final clip...')
self.final_clip.write_videofile(self.result_path, verbose=False)