Skip to content

Video editing with Python

Refactor tests files removing 3 (#1593)

Álvaro Mondéjarebfe16cc6c21
diff --git a/tests/test_SubtitlesClip.py b/tests/test_SubtitlesClip.py
index 44e7dd35..207de53d 100644
--- a/tests/test_SubtitlesClip.py
+++ b/tests/test_SubtitlesClip.py
@@ -4,7 +4,6 @@ import os
import pytest
-from moviepy.utils import close_all_clips
from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
from moviepy.video.compositing.concatenate import concatenate_videoclips
from moviepy.video.tools.subtitles import SubtitlesClip, file_to_subtitles
@@ -45,13 +44,16 @@ def test_subtitles():
subtitles = SubtitlesClip("media/subtitles.srt", generator)
final = CompositeVideoClip([myvideo, subtitles])
- final.write_videofile(os.path.join(TMP_DIR, "subtitles.mp4"), fps=30)
+ final.subclip(0, 0.5).write_videofile(
+ os.path.join(TMP_DIR, "subtitles.mp4"),
+ fps=5,
+ logger=None,
+ )
assert subtitles.subtitles == MEDIA_SUBTITLES_DATA
subtitles = SubtitlesClip(MEDIA_SUBTITLES_DATA, generator)
assert subtitles.subtitles == MEDIA_SUBTITLES_DATA
- close_all_clips(locals())
def test_file_to_subtitles():
diff --git a/tests/test_audiotools.py b/tests/test_audiotools.py
deleted file mode 100644
index 4bbf21fc..00000000
--- a/tests/test_audiotools.py
+++ /dev/null
@@ -1,36 +0,0 @@
-"""Audio tools tests."""
-
-import pytest
-
-from moviepy.audio.AudioClip import AudioClip, CompositeAudioClip
-from moviepy.audio.fx.multiply_volume import multiply_volume
-from moviepy.audio.tools.cuts import find_audio_period
-from moviepy.video.fx.loop import loop
-
-from tests.test_helper import get_mono_wave, get_stereo_wave
-
-
[email protected]("wave", ("mono", "stereo"))
-def test_find_audio_period(wave):
- if wave == "mono":
- wave1 = get_mono_wave(freq=400)
- wave2 = get_mono_wave(freq=100)
- else:
- wave1 = get_stereo_wave(left_freq=400, right_freq=220)
- wave2 = get_stereo_wave(left_freq=100, right_freq=200)
- clip = CompositeAudioClip(
- [
- AudioClip(make_frame=wave1, duration=0.3, fps=22050),
- multiply_volume(
- AudioClip(make_frame=wave2, duration=0.3, fps=22050),
- 0,
- end_time=0.1,
- ),
- ]
- )
- loop_clip = loop(clip, 4)
- assert round(find_audio_period(loop_clip), 6) == pytest.approx(0.29932, 0.1)
-
-
-if __name__ == "__main__":
- pytest.main()
diff --git a/tests/test_ffmpeg_reader.py b/tests/test_ffmpeg_reader.py
index a5e7116a..7c398486 100644
--- a/tests/test_ffmpeg_reader.py
+++ b/tests/test_ffmpeg_reader.py
@@ -2,6 +2,7 @@
import os
import subprocess
+import time
import numpy as np
import pytest
@@ -9,13 +10,14 @@ import pytest
from moviepy.audio.AudioClip import AudioClip
from moviepy.config import FFMPEG_BINARY
from moviepy.utils import close_all_clips
+from moviepy.video.compositing.CompositeVideoClip import clips_array
from moviepy.video.io.ffmpeg_reader import (
FFMPEG_VideoReader,
FFmpegInfosParser,
ffmpeg_parse_infos,
)
from moviepy.video.io.VideoFileClip import VideoFileClip
-from moviepy.video.VideoClip import BitmapClip
+from moviepy.video.VideoClip import BitmapClip, ColorClip
from tests.test_helper import TMP_DIR, get_mono_wave
@@ -609,5 +611,98 @@ def test_seeking_beyond_file_end():
assert reader.pos == 30 * 24 + 1
+def test_release_of_file_via_close():
+ # Create a random video file.
+ red = ColorClip((256, 200), color=(255, 0, 0))
+ green = ColorClip((256, 200), color=(0, 255, 0))
+ blue = ColorClip((256, 200), color=(0, 0, 255))
+
+ red.fps = green.fps = blue.fps = 10
+
+ # Repeat this so we can see no conflicts.
+ for i in range(3):
+ # Get the name of a temporary file we can use.
+ local_video_filename = os.path.join(
+ TMP_DIR, "test_release_of_file_via_close_%s.mp4" % int(time.time())
+ )
+
+ clip = clips_array([[red, green, blue]]).with_duration(0.5)
+ clip.write_videofile(local_video_filename)
+
+ # Open it up with VideoFileClip.
+ video = VideoFileClip(local_video_filename)
+ video.close()
+ clip.close()
+
+ # Now remove the temporary file.
+ # This would fail on Windows if the file is still locked.
+
+ # This should succeed without exceptions.
+ os.remove(local_video_filename)
+
+ red.close()
+ green.close()
+ blue.close()
+
+
+def test_failure_to_release_file():
+ """Expected to fail. It demonstrates that there *is* a problem with not
+ releasing resources when running on Windows.
+
+ The real issue was that, as of movepy 0.2.3.2, there was no way around it.
+
+ See test_resourcerelease.py to see how the close() methods provide a solution.
+ """
+ # Get the name of a temporary file we can use.
+ local_video_filename = os.path.join(
+ TMP_DIR, "test_release_of_file_%s.mp4" % int(time.time())
+ )
+
+ # Repeat this so we can see that the problems escalate:
+ for i in range(5):
+
+ # Create a random video file.
+ red = ColorClip((256, 200), color=(255, 0, 0))
+ green = ColorClip((256, 200), color=(0, 255, 0))
+ blue = ColorClip((256, 200), color=(0, 0, 255))
+
+ red.fps = green.fps = blue.fps = 30
+ video = clips_array([[red, green, blue]]).with_duration(1)
+
+ try:
+ video.write_videofile(local_video_filename)
+
+ # Open it up with VideoFileClip.
+ clip = VideoFileClip(local_video_filename)
+
+ # Normally a client would do processing here.
+
+ # All finished, so delete the clipS.
+ clip.close()
+ video.close()
+ del clip
+ del video
+
+ except IOError:
+ print(
+ "On Windows, this succeeds the first few times around the loop"
+ " but eventually fails."
+ )
+ print("Need to shut down the process now. No more tests in this file.")
+ return
+
+ try:
+ # Now remove the temporary file.
+ # This will fail on Windows if the file is still locked.
+
+ # In particular, this raises an exception with PermissionError.
+ # In there was no way to avoid it.
+
+ os.remove(local_video_filename)
+ print("You are not running Windows, because that worked.")
+ except OSError: # More specifically, PermissionError in Python 3.
+ print("Yes, on Windows this fails.")
+
+
if __name__ == "__main__":
pytest.main()
diff --git a/tests/test_resourcerelease.py b/tests/test_resourcerelease.py
deleted file mode 100644
index 07c18a32..00000000
--- a/tests/test_resourcerelease.py
+++ /dev/null
@@ -1,50 +0,0 @@
-"""
-Tool tests meant to be run with pytest.
-
-Testing whether issue #596 has been repaired.
-
-Note: Platform dependent test. Will only fail on Windows > NT.
-"""
-
-import os
-import time
-
-from moviepy.video.compositing.CompositeVideoClip import clips_array
-from moviepy.video.io.VideoFileClip import VideoFileClip
-from moviepy.video.VideoClip import ColorClip
-
-from tests.test_helper import TMP_DIR
-
-
-def test_release_of_file_via_close():
- # Create a random video file.
- red = ColorClip((256, 200), color=(255, 0, 0))
- green = ColorClip((256, 200), color=(0, 255, 0))
- blue = ColorClip((256, 200), color=(0, 0, 255))
-
- red.fps = green.fps = blue.fps = 10
-
- # Repeat this so we can see no conflicts.
- for i in range(3):
- # Get the name of a temporary file we can use.
- local_video_filename = os.path.join(
- TMP_DIR, "test_release_of_file_via_close_%s.mp4" % int(time.time())
- )
-
- clip = clips_array([[red, green, blue]]).with_duration(0.5)
- clip.write_videofile(local_video_filename)
-
- # Open it up with VideoFileClip.
- video = VideoFileClip(local_video_filename)
- video.close()
- clip.close()
-
- # Now remove the temporary file.
- # This would fail on Windows if the file is still locked.
-
- # This should succeed without exceptions.
- os.remove(local_video_filename)
-
- red.close()
- green.close()
- blue.close()
diff --git a/tests/test_resourcereleasedemo.py b/tests/test_resourcereleasedemo.py
deleted file mode 100644
index 037ffcdf..00000000
--- a/tests/test_resourcereleasedemo.py
+++ /dev/null
@@ -1,75 +0,0 @@
-"""
-Tool tests meant to be run with pytest.
-
-Demonstrates issue #596 exists.
-
-Note: Platform dependent test. Will only fail on Windows > NT.
-"""
-
-import os
-import time
-
-from moviepy.video.compositing.CompositeVideoClip import clips_array
-from moviepy.video.io.VideoFileClip import VideoFileClip
-from moviepy.video.VideoClip import ColorClip
-
-from tests.test_helper import TMP_DIR
-
-
-def test_failure_to_release_file():
- """Expected to fail. It demonstrates that there *is* a problem with not
- releasing resources when running on Windows.
-
- The real issue was that, as of movepy 0.2.3.2, there was no way around it.
-
- See test_resourcerelease.py to see how the close() methods provide a solution.
- """
- # Get the name of a temporary file we can use.
- local_video_filename = os.path.join(
- TMP_DIR, "test_release_of_file_%s.mp4" % int(time.time())
- )
-
- # Repeat this so we can see that the problems escalate:
- for i in range(5):
-
- # Create a random video file.
- red = ColorClip((256, 200), color=(255, 0, 0))
- green = ColorClip((256, 200), color=(0, 255, 0))
- blue = ColorClip((256, 200), color=(0, 0, 255))
-
- red.fps = green.fps = blue.fps = 30
- video = clips_array([[red, green, blue]]).with_duration(1)
-
- try:
- video.write_videofile(local_video_filename)
-
- # Open it up with VideoFileClip.
- clip = VideoFileClip(local_video_filename)
-
- # Normally a client would do processing here.
-
- # All finished, so delete the clipS.
- clip.close()
- video.close()
- del clip
- del video
-
- except IOError:
- print(
- "On Windows, this succeeds the first few times around the loop"
- " but eventually fails."
- )
- print("Need to shut down the process now. No more tests in this file.")
- return
-
- try:
- # Now remove the temporary file.
- # This will fail on Windows if the file is still locked.
-
- # In particular, this raises an exception with PermissionError.
- # In there was no way to avoid it.
-
- os.remove(local_video_filename)
- print("You are not running Windows, because that worked.")
- except OSError: # More specifically, PermissionError in Python 3.
- print("Yes, on Windows this fails.")
diff --git a/tests/test_videotools.py b/tests/test_videotools.py
index d223fdf0..890cf5a9 100644
--- a/tests/test_videotools.py
+++ b/tests/test_videotools.py
@@ -7,8 +7,11 @@ import sys
import pytest
-from moviepy.audio.AudioClip import AudioClip
+from moviepy.audio.AudioClip import AudioClip, CompositeAudioClip
+from moviepy.audio.fx.multiply_volume import multiply_volume
+from moviepy.audio.tools.cuts import find_audio_period
from moviepy.video.compositing.concatenate import concatenate_videoclips
+from moviepy.video.fx.loop import loop
from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.video.tools.credits import CreditsClip
from moviepy.video.tools.cuts import (
@@ -19,7 +22,7 @@ from moviepy.video.tools.cuts import (
)
from moviepy.video.VideoClip import BitmapClip, ColorClip, ImageClip, VideoClip
-from tests.test_helper import FONT, TMP_DIR, get_stereo_wave
+from tests.test_helper import FONT, TMP_DIR, get_mono_wave, get_stereo_wave
try:
@@ -438,5 +441,27 @@ def test_ipython_display_not_available():
del sys.modules["moviepy.video.io.html_tools"]
[email protected]("wave", ("mono", "stereo"))
+def test_find_audio_period(wave):
+ if wave == "mono":
+ wave1 = get_mono_wave(freq=400)
+ wave2 = get_mono_wave(freq=100)
+ else:
+ wave1 = get_stereo_wave(left_freq=400, right_freq=220)
+ wave2 = get_stereo_wave(left_freq=100, right_freq=200)
+ clip = CompositeAudioClip(
+ [
+ AudioClip(make_frame=wave1, duration=0.3, fps=22050),
+ multiply_volume(
+ AudioClip(make_frame=wave2, duration=0.3, fps=22050),
+ 0,
+ end_time=0.1,
+ ),
+ ]
+ )
+ loop_clip = loop(clip, 4)
+ assert round(find_audio_period(loop_clip), 6) == pytest.approx(0.29932, 0.1)
+
+
if __name__ == "__main__":
pytest.main()