Skip to content

Commit

Permalink
Issue Zulko#596: Update tests to use close().
Browse files Browse the repository at this point in the history
* Without tests changes, many of these existing tests do not pass on
Windows.
  • Loading branch information
Julian-O committed Jul 1, 2017
1 parent 2c5307b commit c1a1563
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 122 deletions.
8 changes: 4 additions & 4 deletions tests/test_ImageSequenceClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def test_1():
durations.append(i)
images.append("media/python_logo_upside_down.png")

clip = ImageSequenceClip(images, durations=durations)
assert clip.duration == sum(durations)
clip.write_videofile("/tmp/ImageSequenceClip1.mp4", fps=30)
with ImageSequenceClip(images, durations=durations) as clip:
assert clip.duration == sum(durations)
clip.write_videofile("/tmp/ImageSequenceClip1.mp4", fps=30)

def test_2():
images=[]
Expand All @@ -37,7 +37,7 @@ def test_2():

#images are not the same size..
with pytest.raises(Exception, message='Expecting Exception'):
ImageSequenceClip(images, durations=durations)
ImageSequenceClip(images, durations=durations).close()


if __name__ == '__main__':
Expand Down
33 changes: 17 additions & 16 deletions tests/test_PR.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def test_PR_339():

# In caption mode.
TextClip(txt='foo', color='white', font=FONT, size=(640, 480),
method='caption', align='center', fontsize=25)
method='caption', align='center', fontsize=25).close()

# In label mode.
TextClip(txt='foo', font=FONT, method='label')
TextClip(txt='foo', font=FONT, method='label').close()

def test_PR_373():
result = Trajectory.load_list("media/traj.txt")
Expand All @@ -68,16 +68,16 @@ def test_PR_424():
warnings.simplefilter('always') # Alert us of deprecation warnings.

# Recommended use
ColorClip([1000, 600], color=(60, 60, 60), duration=10)
ColorClip([1000, 600], color=(60, 60, 60), duration=10).close()

with pytest.warns(DeprecationWarning):
# Uses `col` so should work the same as above, but give warning.
ColorClip([1000, 600], col=(60, 60, 60), duration=10)
ColorClip([1000, 600], col=(60, 60, 60), duration=10).close()

# Catch all warnings as record.
with pytest.warns(None) as record:
# Should give 2 warnings and use `color`, not `col`
ColorClip([1000, 600], color=(60, 60, 60), duration=10, col=(2,2,2))
ColorClip([1000, 600], color=(60, 60, 60), duration=10, col=(2,2,2)).close()

message1 = 'The `ColorClip` parameter `col` has been deprecated. ' + \
'Please use `color` instead.'
Expand All @@ -93,26 +93,27 @@ def test_PR_458():
clip = ColorClip([1000, 600], color=(60, 60, 60), duration=10)
clip.write_videofile(os.path.join(TMP_DIR, "test.mp4"),
progress_bar=False, fps=30)
clip.close()

def test_PR_515():
# Won't actually work until video is in download_media
clip = VideoFileClip("media/fire2.mp4", fps_source='tbr')
assert clip.fps == 90000
clip = VideoFileClip("media/fire2.mp4", fps_source='fps')
assert clip.fps == 10.51
with VideoFileClip("media/fire2.mp4", fps_source='tbr') as clip:
assert clip.fps == 90000
with VideoFileClip("media/fire2.mp4", fps_source='fps') as clip:
assert clip.fps == 10.51


def test_PR_528():
clip = ImageClip("media/vacation_2017.jpg")
new_clip = scroll(clip, w=1000, x_speed=50)
new_clip = new_clip.set_duration(20)
new_clip.fps = 24
new_clip.write_videofile(os.path.join(TMP_DIR, "pano.mp4"))
with ImageClip("media/vacation_2017.jpg") as clip:
new_clip = scroll(clip, w=1000, x_speed=50)
new_clip = new_clip.set_duration(20)
new_clip.fps = 24
new_clip.write_videofile(os.path.join(TMP_DIR, "pano.mp4"))


def test_PR_529():
video_clip = VideoFileClip("media/fire2.mp4")
assert video_clip.rotation == 180
with VideoFileClip("media/fire2.mp4") as video_clip:
assert video_clip.rotation == 180


if __name__ == '__main__':
Expand Down
40 changes: 22 additions & 18 deletions tests/test_VideoFileClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,43 @@ def test_setup():
blue = ColorClip((1024,800), color=(0,0,255))

red.fps = green.fps = blue.fps = 30
video = clips_array([[red, green, blue]]).set_duration(5)
video.write_videofile("/tmp/test.mp4")
with clips_array([[red, green, blue]]).set_duration(5) as video:
video.write_videofile("/tmp/test.mp4")

assert os.path.exists("/tmp/test.mp4")

clip = VideoFileClip("/tmp/test.mp4")
assert clip.duration == 5
assert clip.fps == 30
assert clip.size == [1024*3, 800]
with VideoFileClip("/tmp/test.mp4") as clip:
assert clip.duration == 5
assert clip.fps == 30
assert clip.size == [1024*3, 800]

red.close()
green.close()
blue.close()

def test_ffmpeg_resizing():
"""Test FFmpeg resizing, to include downscaling."""
video_file = 'media/big_buck_bunny_432_433.webm'
target_resolution = (128, 128)
video = VideoFileClip(video_file, target_resolution=target_resolution)
frame = video.get_frame(0)
assert frame.shape[0:2] == target_resolution
with VideoFileClip(video_file, target_resolution=target_resolution) as video:
frame = video.get_frame(0)
assert frame.shape[0:2] == target_resolution

target_resolution = (128, None)
video = VideoFileClip(video_file, target_resolution=target_resolution)
frame = video.get_frame(0)
assert frame.shape[0] == target_resolution[0]
with VideoFileClip(video_file, target_resolution=target_resolution) as video:
frame = video.get_frame(0)
assert frame.shape[0] == target_resolution[0]

target_resolution = (None, 128)
video = VideoFileClip(video_file, target_resolution=target_resolution)
frame = video.get_frame(0)
assert frame.shape[1] == target_resolution[1]
with VideoFileClip(video_file, target_resolution=target_resolution) as video:
frame = video.get_frame(0)
assert frame.shape[1] == target_resolution[1]

# Test upscaling
target_resolution = (None, 2048)
video = VideoFileClip(video_file, target_resolution=target_resolution)
frame = video.get_frame(0)
assert frame.shape[1] == target_resolution[1]
with VideoFileClip(video_file, target_resolution=target_resolution) as video:
frame = video.get_frame(0)
assert frame.shape[1] == target_resolution[1]


if __name__ == '__main__':
Expand Down
14 changes: 7 additions & 7 deletions tests/test_Videos.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ def test_download_media(capsys):
download_media.download()

def test_afterimage():
ai = ImageClip("media/afterimage.png")
masked_clip = mask_color(ai, color=[0,255,1]) # for green
with ImageClip("media/afterimage.png") as ai:
masked_clip = mask_color(ai, color=[0,255,1]) # for green

some_background_clip = ColorClip((800,600), color=(255,255,255))
with ColorClip((800,600), color=(255,255,255)) as some_background_clip:

final_clip = CompositeVideoClip([some_background_clip, masked_clip],
use_bgclip=True)
final_clip.duration = 5
final_clip.write_videofile("/tmp/afterimage.mp4", fps=30)
with CompositeVideoClip([some_background_clip, masked_clip],
use_bgclip=True) as final_clip:
final_clip.duration = 5
final_clip.write_videofile("/tmp/afterimage.mp4", fps=30)

if __name__ == '__main__':
pytest.main()
39 changes: 24 additions & 15 deletions tests/test_compositing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# -*- coding: utf-8 -*-
"""Compositing tests for use with pytest."""
from os.path import join
import sys
import pytest
from moviepy.editor import *
sys.path.append("tests")
from test_helper import TMP_DIR

def test_clips_array():
red = ColorClip((1024,800), color=(255,0,0))
Expand All @@ -12,25 +16,30 @@ def test_clips_array():

with pytest.raises(ValueError,
message="Expecting ValueError (duration not set)"):
video.resize(width=480).write_videofile("/tmp/test_clips_array.mp4")
video.resize(width=480).write_videofile(join(TMP_DIR, "test_clips_array.mp4"))
video.close()
red.close()
green.close()
blue.close()

def test_clips_array_duration():
red = ColorClip((1024,800), color=(255,0,0))
green = ColorClip((1024,800), color=(0,255,0))
blue = ColorClip((1024,800), color=(0,0,255))

video = clips_array([[red, green, blue]]).set_duration(5)
for i in range(20):
red = ColorClip((1024,800), color=(255,0,0))
green = ColorClip((1024,800), color=(0,255,0))
blue = ColorClip((1024,800), color=(0,0,255))

with pytest.raises(AttributeError,
message="Expecting ValueError (fps not set)"):
video.write_videofile("/tmp/test_clips_array.mp4")
with clips_array([[red, green, blue]]).set_duration(5) as video:
with pytest.raises(AttributeError,
message="Expecting ValueError (fps not set)"):
video.write_videofile(join(TMP_DIR, "test_clips_array.mp4"))


#this one should work correctly
red.fps=green.fps=blue.fps=30
video = clips_array([[red, green, blue]]).set_duration(5)
video.write_videofile("/tmp/test_clips_array.mp4")
#this one should work correctly
red.fps = green.fps = blue.fps = 30

with clips_array([[red, green, blue]]).set_duration(5) as video:
video.write_videofile(join(TMP_DIR, "test_clips_array.mp4"))

if __name__ == '__main__':
pytest.main()
red.close()
green.close()
blue.close()
59 changes: 30 additions & 29 deletions tests/test_fx.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,63 @@
from moviepy.video.fx.fadeout import fadeout
from moviepy.video.io.VideoFileClip import VideoFileClip

sys.path.append("tests")

import download_media
from test_helper import TMP_DIR

sys.path.append("tests")


def test_download_media(capsys):
with capsys.disabled():
download_media.download()

def test_blackwhite():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
clip1 = blackwhite(clip)
clip1.write_videofile(os.path.join(TMP_DIR,"blackwhite1.webm"))
with VideoFileClip("media/big_buck_bunny_432_433.webm") as clip:
clip1 = blackwhite(clip)
clip1.write_videofile(os.path.join(TMP_DIR,"blackwhite1.webm"))

# This currently fails with a with_mask error!
# def test_blink():
# clip = VideoFileClip("media/big_buck_bunny_0_30.webm").subclip(0,10)
# clip1 = blink(clip, 1, 1)
# clip1.write_videofile(os.path.join(TMP_DIR,"blink1.webm"))
# with VideoFileClip("media/big_buck_bunny_0_30.webm").subclip(0,10) as clip:
# clip1 = blink(clip, 1, 1)
# clip1.write_videofile(os.path.join(TMP_DIR,"blink1.webm"))

def test_colorx():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
clip1 = colorx(clip, 2)
clip1.write_videofile(os.path.join(TMP_DIR,"colorx1.webm"))
with VideoFileClip("media/big_buck_bunny_432_433.webm") as clip:
clip1 = colorx(clip, 2)
clip1.write_videofile(os.path.join(TMP_DIR,"colorx1.webm"))

def test_crop():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
with VideoFileClip("media/big_buck_bunny_432_433.webm") as clip:

clip1=crop(clip) #ie, no cropping (just tests all default values)
clip1.write_videofile(os.path.join(TMP_DIR, "crop1.webm"))
clip1=crop(clip) #ie, no cropping (just tests all default values)
clip1.write_videofile(os.path.join(TMP_DIR, "crop1.webm"))

clip2=crop(clip, x1=50, y1=60, x2=460, y2=275)
clip2.write_videofile(os.path.join(TMP_DIR, "crop2.webm"))
clip2=crop(clip, x1=50, y1=60, x2=460, y2=275)
clip2.write_videofile(os.path.join(TMP_DIR, "crop2.webm"))

clip3=crop(clip, y1=30) #remove part above y=30
clip3.write_videofile(os.path.join(TMP_DIR, "crop3.webm"))
clip3=crop(clip, y1=30) #remove part above y=30
clip3.write_videofile(os.path.join(TMP_DIR, "crop3.webm"))

clip4=crop(clip, x1=10, width=200) # crop a rect that has width=200
clip4.write_videofile(os.path.join(TMP_DIR, "crop4.webm"))
clip4=crop(clip, x1=10, width=200) # crop a rect that has width=200
clip4.write_videofile(os.path.join(TMP_DIR, "crop4.webm"))

clip5=crop(clip, x_center=300, y_center=400, width=50, height=150)
clip5.write_videofile(os.path.join(TMP_DIR, "crop5.webm"))
clip5=crop(clip, x_center=300, y_center=400, width=50, height=150)
clip5.write_videofile(os.path.join(TMP_DIR, "crop5.webm"))

clip6=crop(clip, x_center=300, width=400, y1=100, y2=600)
clip6.write_videofile(os.path.join(TMP_DIR, "crop6.webm"))
clip6=crop(clip, x_center=300, width=400, y1=100, y2=600)
clip6.write_videofile(os.path.join(TMP_DIR, "crop6.webm"))

def test_fadein():
clip = VideoFileClip("media/big_buck_bunny_0_30.webm").subclip(0,5)
clip1 = fadein(clip, 1)
clip1.write_videofile(os.path.join(TMP_DIR,"fadein1.webm"))
with VideoFileClip("media/big_buck_bunny_0_30.webm").subclip(0,5) as clip:
clip1 = fadein(clip, 1)
clip1.write_videofile(os.path.join(TMP_DIR,"fadein1.webm"))

def test_fadeout():
clip = VideoFileClip("media/big_buck_bunny_0_30.webm").subclip(0,5)
clip1 = fadeout(clip, 1)
clip1.write_videofile(os.path.join(TMP_DIR,"fadeout1.webm"))
with VideoFileClip("media/big_buck_bunny_0_30.webm").subclip(0,5) as clip:
clip1 = fadeout(clip, 1)
clip1.write_videofile(os.path.join(TMP_DIR,"fadeout1.webm"))


if __name__ == '__main__':
Expand Down
Loading

0 comments on commit c1a1563

Please sign in to comment.