forked from Zulko/moviepy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_VideoClip.py
179 lines (140 loc) · 6.33 KB
/
test_VideoClip.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
import os
import pytest
from numpy import pi, sin
from moviepy.audio.AudioClip import AudioClip
from moviepy.audio.io.AudioFileClip import AudioFileClip
from moviepy.utils import close_all_clips
from moviepy.video.fx.speedx import speedx
from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.video.VideoClip import ColorClip, VideoClip
from tests.test_helper import TMP_DIR
def test_check_codec():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
location = os.path.join(TMP_DIR, "not_a_video.mas")
try:
clip.write_videofile(location)
except ValueError as e:
assert (
"MoviePy couldn't find the codec associated with the filename."
" Provide the 'codec' parameter in write_videofile." in str(e)
)
close_all_clips(locals())
def test_errors_with_redirected_logs():
"""Checks error cases return helpful messages even when logs redirected
See https://github.com/Zulko/moviepy/issues/877"""
clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
location = os.path.join(TMP_DIR, "logged-write.mp4")
with pytest.raises(IOError) as e:
clip.write_videofile(location, codec="nonexistent-codec", write_logfile=True)
assert "Unknown encoder 'nonexistent-codec'" in str(e.value)
close_all_clips(locals())
def test_write_videofiles_with_temp_audiofile_path():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.5)
location = os.path.join(TMP_DIR, "temp_audiofile_path.webm")
temp_location = "temp_audiofile"
if not os.path.exists(temp_location):
os.mkdir(temp_location)
clip.write_videofile(location, temp_audiofile_path=temp_location, remove_temp=False)
assert os.path.isfile(location)
contents_of_temp_dir = os.listdir(temp_location)
assert any(file.startswith("temp_audiofile_path") for file in contents_of_temp_dir)
def test_save_frame():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
location = os.path.join(TMP_DIR, "save_frame.png")
clip.save_frame(location, t=0.5)
assert os.path.isfile(location)
close_all_clips(locals())
def test_write_image_sequence():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.5)
locations = clip.write_images_sequence(os.path.join(TMP_DIR, "frame%02d.png"))
for location in locations:
assert os.path.isfile(location)
close_all_clips(locals())
def test_write_gif_imageio():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.8)
location = os.path.join(TMP_DIR, "imageio_gif.gif")
clip.write_gif(location, program="imageio")
assert os.path.isfile(location)
close_all_clips(locals())
def test_write_gif_ffmpeg():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.4)
location = os.path.join(TMP_DIR, "ffmpeg_gif.gif")
clip.write_gif(location, program="ffmpeg")
assert os.path.isfile(location)
close_all_clips(locals())
def test_write_gif_ffmpeg_tmpfiles():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.5)
location = os.path.join(TMP_DIR, "ffmpeg_tmpfiles_gif.gif")
clip.write_gif(location, program="ffmpeg", tempfiles=True)
assert os.path.isfile(location)
close_all_clips(locals())
def test_write_gif_ImageMagick():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.5)
location = os.path.join(TMP_DIR, "imagemagick_gif.gif")
clip.write_gif(location, program="ImageMagick")
close_all_clips(locals())
# Fails for some reason
# assert os.path.isfile(location)
def test_write_gif_ImageMagick_tmpfiles():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.5)
location = os.path.join(TMP_DIR, "imagemagick_tmpfiles_gif.gif")
clip.write_gif(location, program="ImageMagick", tempfiles=True)
assert os.path.isfile(location)
close_all_clips(locals())
def test_subfx():
clip = VideoFileClip("media/big_buck_bunny_0_30.webm").subclip(0, 1)
transform = lambda c: speedx(c, 0.5)
new_clip = clip.subfx(transform, 0.5, 0.8)
location = os.path.join(TMP_DIR, "subfx.mp4")
new_clip.write_videofile(location)
assert os.path.isfile(location)
close_all_clips(locals())
def test_oncolor():
# It doesn't need to be a ColorClip
clip = ColorClip(size=(100, 60), color=(255, 0, 0), duration=0.5)
on_color_clip = clip.on_color(size=(200, 160), color=(0, 0, 255))
location = os.path.join(TMP_DIR, "oncolor.mp4")
on_color_clip.write_videofile(location, fps=24)
assert os.path.isfile(location)
close_all_clips(locals())
def test_setaudio():
clip = ColorClip(size=(100, 60), color=(255, 0, 0), duration=0.5)
make_frame_440 = lambda t: [sin(440 * 2 * pi * t)]
audio = AudioClip(make_frame_440, duration=0.5)
audio.fps = 44100
clip = clip.set_audio(audio)
location = os.path.join(TMP_DIR, "setaudio.mp4")
clip.write_videofile(location, fps=24)
assert os.path.isfile(location)
close_all_clips(locals())
def test_setaudio_with_audiofile():
clip = ColorClip(size=(100, 60), color=(255, 0, 0), duration=0.5)
audio = AudioFileClip("media/crunching.mp3").subclip(0, 0.5)
clip = clip.set_audio(audio)
location = os.path.join(TMP_DIR, "setaudiofile.mp4")
clip.write_videofile(location, fps=24)
assert os.path.isfile(location)
close_all_clips(locals())
def test_setopacity():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.6)
clip = clip.set_opacity(0.5)
clip = clip.on_color(size=(1000, 1000), color=(0, 0, 255), col_opacity=0.8)
location = os.path.join(TMP_DIR, "setopacity.mp4")
clip.write_videofile(location)
assert os.path.isfile(location)
close_all_clips(locals())
def test_toimageclip():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.6)
clip = clip.to_ImageClip(t=0.1, duration=0.4)
location = os.path.join(TMP_DIR, "toimageclip.mp4")
clip.write_videofile(location, fps=24)
assert os.path.isfile(location)
close_all_clips(locals())
def test_withoutaudio():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.6)
new_clip = clip.without_audio()
assert new_clip.audio is None
close_all_clips(locals())
if __name__ == "__main__":
# pytest.main()
test_write_videofiles_with_temp_audiofile_path()