-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·175 lines (113 loc) · 4.76 KB
/
test.py
File metadata and controls
executable file
·175 lines (113 loc) · 4.76 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
"""
Description of the video:
Mimic of Star Wars' opening title. A text with a (false)
perspective effect goes towards the end of space, on a
background made of stars. Slight fading effect on the text.
"""
import numpy as np
from skimage import transform as tf
from moviepy.editor import *
from moviepy.video.tools.drawing import color_gradient
# RESOLUTION
w = 720
h = w*9/16 # 16/9 screen
moviesize = w,h
# THE RAW TEXT
txt = "\n".join([
"A long time ago, in a faraway galaxy,",
"there lived a prince and a princess",
"who had never seen the stars, for they",
"lived deep underground.",
"",
"Many years before, the prince's",
"grandfather had ventured out to the",
"surface and had been burnt to ashes by",
"solar winds.",
"",
"One day, as the princess was coding",
"and the prince was shopping online, a",
"meteor landed just a few megameters",
"from the couple's flat."
])
# Add blanks
txt = 10*"\n" +txt + 10*"\n"
# CREATE THE TEXT IMAGE
clip_txt = TextClip(txt,color='white', align='West',fontsize=25,
font='Xolonium-Bold', method='label')
# SCROLL THE TEXT IMAGE BY CROPPING A MOVING AREA
txt_speed = 27
fl = lambda gf,t : gf(t)[int(txt_speed*t):int(txt_speed*t)+h,:]
moving_txt= clip_txt.fl(fl, apply_to=['mask'])
# ADD A VANISHING EFFECT ON THE TEXT WITH A GRADIENT MASK
grad = color_gradient(moving_txt.size,p1=(0,2*h/3),
p2=(0,h/4),col1=0.0,col2=1.0)
gradmask = ImageClip(grad,ismask=True)
fl = lambda pic : np.minimum(pic,gradmask.img)
moving_txt.mask = moving_txt.mask.fl_image(fl)
# WARP THE TEXT INTO A TRAPEZOID (PERSPECTIVE EFFECT)
def trapzWarp(pic,cx,cy,ismask=False):
""" Complicated function (will be latex packaged as a fx) """
Y,X = pic.shape[:2]
src = np.array([[0,0],[X,0],[X,Y],[0,Y]])
dst = np.array([[cx*X,cy*Y],[(1-cx)*X,cy*Y],[X,Y],[0,Y]])
tform = tf.ProjectiveTransform()
tform.estimate(src,dst)
im = tf.warp(pic, tform.inverse, output_shape=(Y,X))
return im if ismask else (im*255).astype('uint8')
fl_im = lambda pic : trapzWarp(pic,0.2,0.3)
fl_mask = lambda pic : trapzWarp(pic,0.2,0.3, ismask=True)
warped_txt= moving_txt.fl_image(fl_im)
warped_txt.mask = warped_txt.mask.fl_image(fl_mask)
# BACKGROUND IMAGE, DARKENED AT 60%
stars = ImageClip('../../videos/stars.jpg')
stars_darkened = stars.fl_image(lambda pic: (0.6*pic).astype('int16'))
# COMPOSE THE MOVIE
final = CompositeVideoClip([
stars_darkened,
warped_txt.set_pos(('center','bottom'))],
size = moviesize)
# WRITE TO A FILE
final.set_duration(8).write_videofile("starworms.avi", fps=5)
# This script is heavy (30s of computations to render 8s of video)
"""=====================================================================
CODE FOR THE VIDEO TUTORIAL
We will now code the video tutorial for this video.
When you think about it, it is a code for a video explaining how to
make another video using some code (this is so meta !).
This code uses the variables of the previous code (it should be placed
after that previous code to work).
====================================================================="""
def annotate(clip,txt,txt_color='white',bg_color=(0,0,255)):
""" Writes a text at the bottom of the clip. """
txtclip = TextClip(txt, fontsize=20, font='Ubuntu-bold',
color=txt_color)
txtclip = txtclip.on_color((clip.w,txtclip.h+6), color=(0,0,255),
pos=(6,'center'))
cvc = CompositeVideoClip([clip , txtclip.set_pos((0,'bottom'))])
return cvc.set_duration(clip.duration)
def resizeCenter(clip):
return clip.resize( height=h).set_pos('center')
def composeCenter(clip):
return CompositeVideoClip([clip.set_pos('center')],size=moviesize)
annotated_clips = [ annotate(clip,text) for clip,text in [
(composeCenter(resizeCenter(stars)).subclip(0,3),
"This is a public domain picture of stars"),
(CompositeVideoClip([stars],moviesize).subclip(0,3),
"We only keep one part."),
(CompositeVideoClip([stars_darkened],moviesize).subclip(0,3),
"We darken it a little."),
(composeCenter(resizeCenter(clip_txt)).subclip(0,3),
"We generate a text image."),
(composeCenter(moving_txt.set_mask(None)).subclip(6,9),
"We scroll the text by cropping a moving region of it."),
(composeCenter(gradmask.to_RGB()).subclip(0,2),
"We add this mask to the clip."),
(composeCenter(moving_txt).subclip(6,9),
"Here is the result"),
(composeCenter(warped_txt).subclip(6,9),
"We now warp this clip in a trapezoid."),
(final.subclip(6,9),
"We finally superimpose with the stars.")
]]
# Concatenate and write to a file
concatenate_videoclips(annotated_clips).write_videofile('tutorial.avi', fps=5)