forked from Zulko/moviepy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_Clip.py
258 lines (213 loc) · 6.45 KB
/
test_Clip.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
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
"""Clip tests."""
import copy
import numpy as np
import pytest
from moviepy.Clip import Clip
from moviepy.video.VideoClip import BitmapClip, ColorClip
def test_clip_equality():
bitmap = [["RR", "RR"], ["RB", "RB"]]
different_bitmap = [["RR", "RB"], ["RB", "RB"]]
different_duration_bitmap = [["RR", "RR"], ["RB", "RB"], ["RR", "RR"]]
clip = BitmapClip(bitmap, fps=1)
same_clip = BitmapClip(bitmap, fps=1)
different_clip = BitmapClip(different_bitmap, fps=1)
different_duration_clip = BitmapClip(different_duration_bitmap, fps=1)
assert clip == same_clip
assert clip != different_clip
assert clip != different_duration_clip
assert different_clip != different_duration_clip
def test_clip_with_is_mask():
clip = BitmapClip([["RR", "GG"]], fps=1)
assert not clip.is_mask
assert clip.with_is_mask(True).is_mask
assert not clip.with_is_mask(False).is_mask
@pytest.mark.parametrize(
(
"start",
"end",
"duration",
"new_start",
"change_end",
"expected_end",
"expected_duration",
),
(
(0, 3, 3, 1, True, 4, 3),
(0, 3, 3, 1, False, 3, 2), # not change_end
),
)
def test_clip_with_start(
start,
end,
duration,
new_start,
change_end,
expected_end,
expected_duration,
):
clip = (
ColorClip(color=(255, 0, 0), size=(2, 2))
.with_fps(1)
.with_duration(duration)
.with_end(end)
.with_start(start)
)
new_clip = clip.with_start(new_start, change_end=change_end)
assert new_clip.end == expected_end
assert new_clip.duration == expected_duration
@pytest.mark.parametrize(
("duration", "start", "end", "expected_start", "expected_duration"),
(
(3, 1, 2, 1, 1),
(3, 1, None, 1, 3), # end is None
(3, None, 4, 1, 3), # start is None
),
)
def test_clip_with_end(duration, start, end, expected_start, expected_duration):
clip = ColorClip(color=(255, 0, 0), size=(2, 2), duration=duration).with_fps(1)
if start is not None:
clip = clip.with_start(start)
else:
clip.start = None
clip = clip.with_end(end)
assert clip.start == expected_start
assert clip.duration == expected_duration
@pytest.mark.parametrize(
(
"duration",
"start",
"end",
"new_duration",
"change_end",
"expected_duration",
"expected_start",
"expected_end",
),
(
(5, None, None, 3, True, 3, 0, 3),
("00:00:05", 1, 6, 3, True, 3, 1, 4), # change end
((0, 0, 5), 1, 6, 3, False, 3, 3, 6), # change start
(5, None, None, None, False, ValueError, None, None),
),
)
def test_clip_with_duration(
duration,
start,
end,
new_duration,
change_end,
expected_duration,
expected_start,
expected_end,
):
clip = ColorClip(color=(255, 0, 0), size=(2, 2)).with_fps(1).with_duration(duration)
if start is not None:
clip = clip.with_start(start)
if end is not None:
clip = clip.with_end(end)
if hasattr(expected_duration, "__traceback__"):
with pytest.raises(expected_duration):
clip.with_duration(new_duration, change_end=change_end)
else:
clip = clip.with_duration(new_duration, change_end=change_end)
assert clip.duration == expected_duration
assert clip.start == expected_start
assert clip.end == expected_end
@pytest.mark.parametrize(
"copy_func",
(
lambda clip: clip.copy(),
lambda clip: copy.copy(clip),
lambda clip: copy.deepcopy(clip),
),
ids=(
"clip.copy()",
"copy.copy(clip)",
"copy.deepcopy(clip)",
),
)
def test_clip_copy(copy_func):
"""Clip must be copied with `.copy()` method, `copy.copy()` and
`copy.deepcopy()` (same behaviour).
"""
clip = Clip()
other_clip = Clip()
# shallow copy of clip
for attr in clip.__dict__:
setattr(clip, attr, "foo")
copied_clip = copy_func(clip)
# assert copied attributes
for attr in copied_clip.__dict__:
assert getattr(copied_clip, attr) == getattr(clip, attr)
# other instances are not edited
assert getattr(copied_clip, attr) != getattr(other_clip, attr)
@pytest.mark.parametrize(
("duration", "start_time", "end_time", "expected_duration"),
(
(1, 0, None, 1),
(3, 0, 2, 2),
(3, 1, 2, 1),
(3, -2, 2, 1), # negative start_time
(3, 4, None, ValueError), # start_time > duration
(3, 3, None, ValueError), # start_time == duration
(3, 1, -1, 1), # negative end_time
(None, 1, -1, ValueError), # negative end_time for clip without duration
),
)
def test_clip_subclip(duration, start_time, end_time, expected_duration):
if duration is None:
clip = ColorClip(color=(255, 0, 0), size=(2, 2)).with_fps(1)
else:
clip = BitmapClip([["RR", "GG"] for _ in range(duration)], fps=1)
if hasattr(expected_duration, "__traceback__"):
with pytest.raises(expected_duration):
clip.subclip(start_time=start_time, end_time=end_time)
else:
sub_clip = clip.subclip(start_time=start_time, end_time=end_time)
assert sub_clip.duration == expected_duration
@pytest.mark.parametrize(
("start_time", "end_time", "expected_frames"),
(
(
1,
2,
[["RR", "RR"], ["BB", "BB"]],
),
(
1,
3,
[["RR", "RR"]],
),
(
2,
3,
[["RR", "RR"], ["GG", "GG"]],
),
(
0,
1,
[["GG", "GG"], ["BB", "BB"]],
),
(
0,
2,
[["BB", "BB"]],
),
),
)
def test_clip_cutout(start_time, end_time, expected_frames):
clip = BitmapClip([["RR", "RR"], ["GG", "GG"], ["BB", "BB"]], fps=1)
new_clip = clip.cutout(start_time, end_time)
assert new_clip == BitmapClip(expected_frames, fps=1)
def test_clip_memoize():
clip = BitmapClip([["RR", "RR"], ["GG", "GG"], ["BB", "BB"]], fps=1)
assert not clip.memoize
memoize_clip = clip.with_memoize(True)
assert memoize_clip.memoize
# get_frame memoizing
memoize_clip.memoized_t = 5
memoize_clip.memoized_frame = "foo"
assert memoize_clip.get_frame(5) == "foo"
assert isinstance(memoize_clip.get_frame(1), np.ndarray)
if __name__ == "__main__":
pytest.main()