Skip to content

Video editing with Python

Test tools, refactor increasing tests performance (#1584)

Álvaro Mondéjarbbf0d01c644a
diff --git a/moviepy/tools.py b/moviepy/tools.py
index 802606eb..699f7b03 100644
--- a/moviepy/tools.py
+++ b/moviepy/tools.py
@@ -6,6 +6,9 @@ import warnings
import proglog
+OS_NAME = os.name
+
+
def cross_platform_popen_params(popen_params):
"""Wrap with this function a dictionary of ``subprocess.Popen`` kwargs and
will be ready to work without unexpected behaviours in any platform.
@@ -14,7 +17,7 @@ def cross_platform_popen_params(popen_params):
- ``creationflags=0x08000000``: no extra unwanted window opens on Windows
when the child process is created. Only added on Windows.
"""
- if os.name == "nt":
+ if OS_NAME == "nt":
popen_params["creationflags"] = 0x08000000
return popen_params
diff --git a/tests/test_PR.py b/tests/test_PR.py
index ca733674..4f46cd90 100644
--- a/tests/test_PR.py
+++ b/tests/test_PR.py
@@ -14,7 +14,7 @@ from moviepy.video.tools.interpolators import Trajectory
from moviepy.video.tools.subtitles import SubtitlesClip
from moviepy.video.VideoClip import ColorClip, ImageClip, TextClip
-from tests.test_helper import FONT, TMP_DIR
+from tests.test_helper import FONT, TMP_DIR, get_test_video
def test_PR_306():
@@ -101,9 +101,7 @@ def test_PR_610():
def test_PR_1137_video():
"""Test support for path-like objects as arguments for VideoFileClip."""
- with VideoFileClip(Path("media/big_buck_bunny_432_433.webm")).subclip(
- 0.2, 0.4
- ) as video:
+ with get_test_video().subclip(0.2, 0.4) as video:
video.write_videofile(Path(TMP_DIR) / "pathlike.mp4")
assert isinstance(video.filename, str)
diff --git a/tests/test_misc.py b/tests/test_SubtitlesClip.py
similarity index 98%
rename from tests/test_misc.py
rename to tests/test_SubtitlesClip.py
index bd8efe83..44e7dd35 100644
--- a/tests/test_misc.py
+++ b/tests/test_SubtitlesClip.py
@@ -1,3 +1,5 @@
+"""SubtitlesClip tests."""
+
import os
import pytest
diff --git a/tests/test_VideoClip.py b/tests/test_VideoClip.py
index 00da5c17..d8c61696 100644
--- a/tests/test_VideoClip.py
+++ b/tests/test_VideoClip.py
@@ -12,7 +12,7 @@ from moviepy.video.fx.multiply_speed import multiply_speed
from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.video.VideoClip import BitmapClip, ColorClip, VideoClip
-from tests.test_helper import TMP_DIR
+from tests.test_helper import TMP_DIR, get_test_video
def test_aspect_ratio():
@@ -21,7 +21,7 @@ def test_aspect_ratio():
def test_check_codec():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
+ clip = get_test_video()
location = os.path.join(TMP_DIR, "not_a_video.mas")
try:
clip.write_videofile(location)
@@ -35,7 +35,7 @@ def test_check_codec():
def test_write_frame_errors():
"""Checks error cases return helpful messages."""
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
+ clip = get_test_video()
location = os.path.join(TMP_DIR, "unlogged-write.mp4")
with pytest.raises(IOError) as e:
clip.write_videofile(location, codec="nonexistent-codec")
@@ -54,7 +54,7 @@ def test_write_frame_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")
+ clip = get_test_video()
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)
@@ -85,7 +85,7 @@ def test_write_videofiles_with_temp_audiofile_path():
def test_save_frame():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm")
+ 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)
@@ -93,7 +93,7 @@ def test_save_frame():
def test_write_image_sequence():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.24)
+ clip = get_test_video().subclip(0.2, 0.24)
locations = clip.write_images_sequence(os.path.join(TMP_DIR, "frame%02d.png"))
for location in locations:
assert os.path.isfile(location)
@@ -101,7 +101,7 @@ def test_write_image_sequence():
def test_write_gif_imageio():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.8)
+ clip = get_test_video().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)
@@ -109,7 +109,7 @@ def test_write_gif_imageio():
def test_write_gif_ffmpeg():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.4)
+ clip = get_test_video().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)
@@ -117,7 +117,7 @@ def test_write_gif_ffmpeg():
def test_write_gif_ffmpeg_pixel_format():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.4)
+ clip = get_test_video().subclip(0.2, 0.4)
location = os.path.join(TMP_DIR, "ffmpeg_gif.gif")
clip.write_gif(location, program="ffmpeg", pixel_format="bgr24")
assert os.path.isfile(location)
@@ -125,7 +125,7 @@ def test_write_gif_ffmpeg_pixel_format():
def test_write_gif_ffmpeg_tmpfiles():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.24)
+ clip = get_test_video().subclip(0.2, 0.24)
location = os.path.join(TMP_DIR, "ffmpeg_tmpfiles_gif.gif")
clip.write_gif(location, program="ffmpeg", tempfiles=True)
assert os.path.isfile(location)
@@ -133,7 +133,7 @@ def test_write_gif_ffmpeg_tmpfiles():
def test_write_gif_ffmpeg_tmpfiles_pixel_format():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.24)
+ clip = get_test_video().subclip(0.2, 0.24)
location = os.path.join(TMP_DIR, "ffmpeg_tmpfiles_gif.gif")
clip.write_gif(location, program="ffmpeg", tempfiles=True, pixel_format="bgr24")
assert os.path.isfile(location)
@@ -141,7 +141,7 @@ def test_write_gif_ffmpeg_tmpfiles_pixel_format():
def test_write_gif_ImageMagick():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.5)
+ clip = get_test_video().subclip(0.2, 0.5)
location = os.path.join(TMP_DIR, "imagemagick_gif.gif")
clip.write_gif(location, program="ImageMagick")
close_all_clips(locals())
@@ -150,7 +150,7 @@ def test_write_gif_ImageMagick():
def test_write_gif_ImageMagick_tmpfiles():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.24)
+ clip = get_test_video().subclip(0.2, 0.24)
location = os.path.join(TMP_DIR, "imagemagick_tmpfiles_gif.gif")
clip.write_gif(location, program="ImageMagick", tempfiles=True)
assert os.path.isfile(location)
@@ -158,7 +158,7 @@ def test_write_gif_ImageMagick_tmpfiles():
def test_write_gif_ImageMagick_tmpfiles_pixel_format():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.24)
+ clip = get_test_video().subclip(0.2, 0.24)
location = os.path.join(TMP_DIR, "imagemagick_tmpfiles_gif.gif")
clip.write_gif(location, program="ImageMagick", tempfiles=True, pixel_format="SGI")
assert os.path.isfile(location)
@@ -220,7 +220,7 @@ def test_setaudio_with_audiofile():
def test_setopacity():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.6)
+ clip = get_test_video().subclip(0.2, 0.6)
clip = clip.with_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")
@@ -259,7 +259,7 @@ def test_compositing_with_same_layers():
def test_toimageclip():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.6)
+ clip = get_test_video().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)
@@ -268,14 +268,14 @@ def test_toimageclip():
def test_withoutaudio():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.6)
+ clip = get_test_video().subclip(0.2, 0.6)
new_clip = clip.without_audio()
assert new_clip.audio is None
close_all_clips(locals())
def test_setfps_withoutchangeduration():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0, 1)
+ clip = get_test_video()
# The sum is unique for each frame, so we can use it as a frame-ID
# to check which frames are being preserved
clip_sums = [f.sum() for f in clip.iter_frames()]
@@ -288,7 +288,7 @@ def test_setfps_withoutchangeduration():
def test_setfps_withchangeduration():
- clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0, 1)
+ clip = get_test_video()
# The sum is unique for each frame, so we can use it as a frame-ID
# to check which frames are being preserved
clip_sums = [f.sum() for f in clip.iter_frames()]
diff --git a/tests/test_fx.py b/tests/test_fx.py
index 6db9012f..66c642c1 100644
--- a/tests/test_fx.py
+++ b/tests/test_fx.py
@@ -52,11 +52,7 @@ from moviepy.video.fx import (
time_symmetrize,
)
-from tests.test_helper import TMP_DIR
-
-
-def get_test_video():
- return VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0, 1)
+from tests.test_helper import TMP_DIR, get_test_video
def test_accel_decel():
diff --git a/tests/test_helper.py b/tests/test_helper.py
index ccb9fff6..6fa76a82 100644
--- a/tests/test_helper.py
+++ b/tests/test_helper.py
@@ -1,8 +1,11 @@
"""Define general test helper attributes and utilities."""
+import functools
import sys
import tempfile
+from moviepy.video.io.VideoFileClip import VideoFileClip
+
PYTHON_VERSION = "%s.%s" % (sys.version_info.major, sys.version_info.minor)
TMP_DIR = tempfile.gettempdir() # because tempfile.tempdir is sometimes None
@@ -16,3 +19,8 @@ else:
FONT = (
"Liberation-Mono" # This is available in the fonts-liberation package on Linux
)
+
+
[email protected]_cache(maxsize=None)
+def get_test_video():
+ return VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0, 1)
diff --git a/tests/test_tools.py b/tests/test_tools.py
index b0bef70a..47282981 100644
--- a/tests/test_tools.py
+++ b/tests/test_tools.py
@@ -1,13 +1,23 @@
"""Tool tests meant to be run with pytest. Taken from PR #121 (grimley517)."""
+import importlib
+import os
+import shutil
+
import pytest
import moviepy.tools as tools
@pytest.mark.parametrize(
- "given, expected",
- [("libx264", "mp4"), ("libmpeg4", "mp4"), ("libtheora", "ogv"), ("libvpx", "webm")],
+ ("given", "expected"),
+ [
+ ("libx264", "mp4"),
+ ("libmpeg4", "mp4"),
+ ("libtheora", "ogv"),
+ ("libvpx", "webm"),
+ ("jpeg", "jpeg"),
+ ],
)
def test_find_extensions(given, expected):
"""Test for find_extension function."""
@@ -42,5 +52,24 @@ def test_cvsecs(given, expected):
assert tools.convert_to_seconds(given) == expected
[email protected](not shutil.which("echo"), reason="not in Unix")
[email protected]("command", ("echo", "jbdshfuygvhbsdvfghew"))
+def test_subprocess_call(command):
+ if command == "echo":
+ tools.subprocess_call(command, logger=None)
+ else:
+ with pytest.raises(IOError):
+ tools.subprocess_call(command, logger=None)
+
+
[email protected]("os_name", (os.name, "nt"))
+def test_cross_platform_popen_params(os_name, monkeypatch):
+ tools_module = importlib.import_module("moviepy.tools")
+ monkeypatch.setattr(tools_module, "OS_NAME", os_name)
+
+ params = tools_module.cross_platform_popen_params({})
+ assert len(params) == (1 if os_name == "nt" else 0)
+
+
if __name__ == "__main__":
pytest.main()