Video editing with Python
Add tests for VideoClip and root __init__ (#1590)
Álvaro Mondéjar68818f6bc36b
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.pyindex 229d459d..3ad7a8c8 100644--- a/moviepy/video/VideoClip.py+++ b/moviepy/video/VideoClip.py@@ -164,12 +164,23 @@ class VideoClip(Clip):def save_frame(self, filename, t=0, with_mask=True):"""Save a clip's frame to an image file.- Saves the frame of clip corresponding to time ``t`` in- 'filename'. ``t`` can be expressed in seconds (15.35), in- (min, sec), in (hour, min, sec), or as a string: '01:03:05.35'.+ Saves the frame of clip corresponding to time ``t`` in ``filename``.+ ``t`` can be expressed in seconds (15.35), in (min, sec),+ in (hour, min, sec), or as a string: '01:03:05.35'.++ Parameters+ ----------++ filename : str+ Name of the file in which the frame will be stored.++ t : float or tuple or str, optional+ Moment of the frame to be saved. As default, the first frame will be+ saved.- If ``with_mask`` is ``True`` the mask is saved in- the alpha layer of the picture (only works with PNGs).+ with_mask : bool, optional+ If is ``True`` the mask is saved in the alpha layer of the picture+ (only works with PNGs)."""im = self.get_frame(t)if with_mask and self.mask is not None:diff --git a/tests/test_VideoClip.py b/tests/test_VideoClip.pyindex d8c61696..e4112595 100644--- a/tests/test_VideoClip.py+++ b/tests/test_VideoClip.py@@ -3,16 +3,18 @@ import osimport numpy as npimport pytest+from PIL import Imagefrom moviepy.audio.AudioClip import AudioClipfrom moviepy.audio.io.AudioFileClip import AudioFileClip+from moviepy.tools import convert_to_secondsfrom moviepy.utils import close_all_clipsfrom moviepy.video.compositing.CompositeVideoClip import CompositeVideoClipfrom moviepy.video.fx.multiply_speed import multiply_speedfrom moviepy.video.io.VideoFileClip import VideoFileClipfrom moviepy.video.VideoClip import BitmapClip, ColorClip, VideoClip-from tests.test_helper import TMP_DIR, get_test_video+from tests.test_helper import TMP_DIR, get_stereo_wave, get_test_videodef test_aspect_ratio():@@ -20,6 +22,35 @@ def test_aspect_ratio():assert clip.aspect_ratio == 1.5+ ("duration", "fps", "expected_n_frames"),+ (+ (1, 60, 60),+ (0.1, 100, 10),+ (2.4, 60, 144),+ ),+)+def test_n_frames(duration, fps, expected_n_frames):+ clip = VideoClip(duration=duration).with_fps(fps)+ assert clip.n_frames == expected_n_frames+++def test_with_audio():+ clip = VideoClip(duration=1).with_fps(1)+ assert clip.audio is None++ audio_clip = AudioClip(get_stereo_wave(), duration=1, fps=22050)+ assert clip.with_audio(audio_clip).audio is audio_clip+++def test_without_audio():+ audio_clip = AudioClip(get_stereo_wave(), duration=1, fps=22050)+ clip = VideoClip(duration=1).with_fps(1).with_audio(audio_clip)++ assert clip.audio is audio_clip+ assert clip.without_audio().audio is None++def test_check_codec():clip = get_test_video()location = os.path.join(TMP_DIR, "not_a_video.mas")@@ -84,12 +115,63 @@ def test_write_videofiles_with_temp_audiofile_path():close_all_clips(locals())-def test_save_frame():- clip = get_test_video()- 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())[email protected]("mask_color", (0, 0.5, 0.8, 1))+ "with_mask",+ (False, True),+ ids=("mask", ""),+)[email protected]("t", (0, "00:00:01", (0, 0, 2)), ids=("t=0", "t=1", "t=2"))+ "frames",+ (+ pytest.param(+ [["RR", "RR"], ["GG", "GG"], ["BB", "BB"]],+ id="RGB 2x2",+ ),+ pytest.param(+ [["O", "O"], ["W", "W"], ["B", "B"]],+ id="OWB 2x1",+ ),+ ),+)+def test_save_frame(with_mask, t, mask_color, frames):+ filename = os.path.join(TMP_DIR, "moviepy_VideoClip_save_frame.png")+ if os.path.isfile(filename):+ os.remove(filename)++ width, height = (len(frames[0][0]), len(frames[0]))++ clip = BitmapClip(frames, fps=1)+ if with_mask:+ mask = ColorClip(color=mask_color, is_mask=True, size=(width, height))+ clip = clip.with_mask(mask)++ clip.save_frame(filename, t)++ t = int(convert_to_seconds(t))++ # expected RGB+ e_r, e_g, e_b = BitmapClip.DEFAULT_COLOR_DICT[frames[t][0][0]]++ im = Image.open(filename, mode="r")+ assert im.width == width+ assert im.height == height++ for i in range(im.width):+ for j in range(im.height):+ rgba = im.getpixel((i, j))+ if len(rgba) == 4:+ r, g, b, a = rgba+ else:+ r, g, b = rgba++ assert r == e_r+ assert g == e_g+ assert b == e_b++ if with_mask:+ assert round(a / 254, 2) == mask_colordef test_write_image_sequence():diff --git a/tests/test_editor.py b/tests/test_editor.pyindex d5899a47..c1ae48dd 100644--- a/tests/test_editor.py+++ b/tests/test_editor.py@@ -2,6 +2,7 @@import importlibimport io+import sysfrom contextlib import redirect_stdoutimport pytest@@ -35,9 +36,12 @@ def test_preview_methods():editor_module.VideoClip.preview.__hash__()== preview_module.preview.__hash__())+ finally:+ if "moviepy.editor" in sys.modules:+ del sys.modules["moviepy.editor"]try:- importlib.import_module("matplotlib")+ importlib.import_module("matplotlib.pyplot")except ImportError:editor_module = importlib.import_module("moviepy.editor")with pytest.raises(ImportError) as exc:@@ -45,6 +49,26 @@ def test_preview_methods():assert str(exc.value) == "sliders requires matplotlib installed"+ del sys.modules["moviepy.editor"]+ else:+ del sys.modules["matplotlib.pyplot"]++ del sys.modules["moviepy"]+++def test__init__preview_methods():+ moviepy_module = importlib.import_module("moviepy")++ with pytest.raises(ImportError) as exc:+ moviepy_module.VideoClip.preview(True)+ assert str(exc.value) == "clip.preview requires importing from moviepy.editor"++ with pytest.raises(ImportError) as exc:+ moviepy_module.VideoClip.show(True)+ assert str(exc.value) == "clip.show requires importing from moviepy.editor"++ del sys.modules["moviepy"]+if __name__ == "__main__":pytest.main()diff --git a/tests/test_examples.py b/tests/test_examples.pyindex 736bc558..2e1c0a09 100644--- a/tests/test_examples.py+++ b/tests/test_examples.py@@ -10,16 +10,18 @@ from tests.test_helper import TMP_DIRtry:- import matplotlib+ import matplotlib.pyplotexcept ImportError:matplotlib = None+else:+ matplotlib = True@pytest.mark.skipif(not matplotlib, reason="no matplotlib")def test_matplotlib_simple_example():import matplotlib.pyplot as plt- plt.switch_backend("agg")+ plt.switch_backend("Agg")x = np.linspace(-2, 2, 200)duration = 0.5diff --git a/tests/test_issues.py b/tests/test_issues.pyindex 1a8ca7f8..aba6daf7 100644--- a/tests/test_issues.py+++ b/tests/test_issues.py@@ -17,9 +17,11 @@ from tests.test_helper import TMP_DIRtry:- import matplotlib+ import matplotlib.pyplotexcept ImportError:matplotlib = None+else:+ matplotlib = Truedef test_issue_145():@@ -277,7 +279,7 @@ def test_issue_368():from moviepy.video.io.bindings import mplfig_to_npimage- plt.switch_backend("agg")+ plt.switch_backend("Agg")X, Y = make_moons(50, noise=0.1, random_state=2) # semi-random data