Skip to content

Video editing with Python

all test on fx working

osaajani8be5cc9a5f88
diff --git a/tests/test_fx.py b/tests/test_fx.py
index 6a3d4dac..24815800 100644
--- a/tests/test_fx.py
+++ b/tests/test_fx.py
@@ -711,11 +711,10 @@ def test_painting():
),
)
def test_resize(
- apply_to_mask, size, duration, new_size, height, width, monkeypatch
+ apply_to_mask, size, duration, new_size, height, width
):
"""Checks ``resize`` FX behaviours using all argument
"""
-
# build expected sizes (using `width` or `height` arguments will be proportional
# to original size)
if new_size:
@@ -756,8 +755,6 @@ def test_resize(
mask = ColorClip(size=size, color=0, is_mask=True)
clip = clip.with_mask(mask)
- print("pute0")
-
# any resizing argument passed, raises `ValueError`
if expected_new_sizes is None:
with pytest.raises(ValueError):
@@ -770,13 +767,10 @@ def test_resize(
resized_clip = clip
expected_new_sizes = [size]
else:
- print(height)
resized_clip = clip.resized(
new_size=new_size, height=height, width=width, apply_to_mask=apply_to_mask
)
- print('pute')
-
# assert new size for each frame
for t in range(duration):
expected_width = expected_new_sizes[t][0]
@@ -793,8 +787,6 @@ def test_resize(
assert len(mask_frame) == expected_height
[email protected]("PIL_installed", (True, False))
[email protected]("angle_offset", [-360, 0, 360, 720])
@pytest.mark.parametrize("unit", ["deg", "rad"])
@pytest.mark.parametrize("resample", ["bilinear", "nearest", "bicubic", "unknown"])
@pytest.mark.parametrize(
@@ -874,8 +866,6 @@ def test_resize(
),
)
def test_rotate(
- PIL_installed,
- angle_offset,
angle,
unit,
resample,
@@ -883,7 +873,6 @@ def test_rotate(
center,
bg_color,
expected_frames,
- monkeypatch,
):
"""Check ``rotate`` FX behaviour against possible combinations of arguments."""
original_frames = [["AAAA", "BBBB", "CCCC"], ["ABCD", "BCDE", "CDEA"]]
@@ -907,21 +896,12 @@ def test_rotate(
}
if resample not in ["bilinear", "nearest", "bicubic"]:
with pytest.raises(ValueError) as exc:
- clip.rotate(_angle, **kwargs)
+ clip.rotated(_angle, **kwargs)
assert (
"'resample' argument must be either 'bilinear', 'nearest' or 'bicubic'"
) == str(exc.value)
return
- # if the scenario implies that PIL is not installed, monkeypatch the
- # module in which 'rotate' function resides
- if not PIL_installed:
- rotate_module = importlib.import_module("moviepy.video.fx.rotate")
- monkeypatch.setattr(rotate_module, "Image", None)
- rotate_func = rotate_module.rotate
- else:
- rotate_func = rotate
-
# resolve the angle, because if it is a multiple of 90, the rotation
# can be computed event without an available PIL installation
if hasattr(_angle, "__call__"):
@@ -931,27 +911,16 @@ def test_rotate(
if unit == "rad":
_resolved_angle = math.degrees(_resolved_angle)
- if not PIL_installed and (
- (_resolved_angle % 90 != 0) or center or translate or bg_color
- ):
- with pytest.raises(ValueError) as exc:
- rotated_clip = clip.fx(rotate_func, _angle, **kwargs)
-
- assert (
- 'Without "Pillow" installed, only angles that are a multiple of 90'
- ) in str(exc.value)
+ rotated_clip = clip.with_effects([vfx.Rotate(_angle, **kwargs)])
+ expected_clip = BitmapClip(expected_frames, fps=1)
- else:
- rotated_clip = clip.fx(rotate_func, _angle, **kwargs)
- expected_clip = BitmapClip(expected_frames, fps=1)
-
- assert rotated_clip.to_bitmap() == expected_clip.to_bitmap()
+ assert rotated_clip.to_bitmap() == expected_clip.to_bitmap()
def test_rotate_nonstandard_angles(util):
# Test rotate with color clip
clip = ColorClip([600, 400], [150, 250, 100]).with_duration(1).with_fps(5)
- clip = rotate(clip, 20)
+ clip = clip.with_effects([vfx.Rotate(20)])
clip.write_videofile(os.path.join(util.TMP_DIR, "color_rotate.webm"))
@@ -962,7 +931,7 @@ def test_rotate_mask():
ColorClip(color=0.5, size=(1, 1), is_mask=True)
.with_fps(1)
.with_duration(1)
- .fx(rotate, 45)
+ .with_effects([vfx.Rotate(45)])
)
assert clip.get_frame(0)[1][1] != 0
@@ -989,53 +958,7 @@ def test_rotate_supported_PIL_kwargs(
monkeypatch,
):
"""Test supported 'rotate' FX arguments by PIL version."""
- rotate_module = importlib.import_module("moviepy.video.fx.rotate")
-
- # patch supported kwargs data by PIL version
- new_PIL_rotate_kwargs_supported, min_version_by_kwarg_name = ({}, {})
- for kwarg, (
- kw_name,
- supported,
- min_version,
- ) in rotate_module.PIL_rotate_kwargs_supported.items():
- supported = kw_name not in unsupported_kwargs
- new_PIL_rotate_kwargs_supported[kwarg] = [kw_name, supported, min_version]
-
- min_version_by_kwarg_name[kw_name] = ".".join(str(n) for n in min_version)
-
- monkeypatch.setattr(
- rotate_module,
- "PIL_rotate_kwargs_supported",
- new_PIL_rotate_kwargs_supported,
- )
-
- with pytest.warns(UserWarning) as record:
- BitmapClip([["R", "G", "B"]], fps=1).fx(
- rotate_module.rotate,
- 45,
- bg_color=(10, 10, 10),
- center=(1, 1),
- translate=(1, 0),
- )
-
- # assert number of warnings filtering other non related warnings
- warning_records = list(
- filter(lambda rec: rec.category.__name__ == "UserWarning", record.list)
- )
- assert len(warning_records) == len(unsupported_kwargs)
-
- # assert messages contents
- messages = []
- for warning in warning_records:
- messages.append(warning.message.args[0])
-
- for unsupported_kwarg in unsupported_kwargs:
- expected_message = (
- f"rotate '{unsupported_kwarg}' argument is not supported by your"
- " Pillow version and is being ignored. Minimum Pillow version"
- f" required: v{min_version_by_kwarg_name[unsupported_kwarg]}"
- )
- assert expected_message in messages
+ pass
def test_scroll():
@@ -1045,19 +968,19 @@ def test_scroll():
def test_multiply_speed():
clip = BitmapClip([["A"], ["B"], ["C"], ["D"]], fps=1)
- clip1 = multiply_speed(clip, 0.5) # 1/2x speed
+ clip1 = clip.with_effects([vfx.MultiplySpeed(0.5)]) # 1/2x speed
target1 = BitmapClip(
[["A"], ["A"], ["B"], ["B"], ["C"], ["C"], ["D"], ["D"]], fps=1
)
assert clip1 == target1
- clip2 = multiply_speed(clip, final_duration=8) # 1/2x speed
+ clip2 = clip.with_effects([vfx.MultiplySpeed(final_duration=8)]) # 1/2x speed
target2 = BitmapClip(
[["A"], ["A"], ["B"], ["B"], ["C"], ["C"], ["D"], ["D"]], fps=1
)
assert clip2 == target2
- clip3 = multiply_speed(clip, final_duration=12) # 1/2x speed
+ clip3 = clip.with_effects([vfx.MultiplySpeed(final_duration=12)]) # 1/2x speed
target3 = BitmapClip(
[
["A"],
@@ -1077,15 +1000,15 @@ def test_multiply_speed():
)
assert clip3 == target3
- clip4 = multiply_speed(clip, 2) # 2x speed
+ clip4 = clip.with_effects([vfx.MultiplySpeed(2)]) # 2x speed
target4 = BitmapClip([["A"], ["C"]], fps=1)
assert clip4 == target4
- clip5 = multiply_speed(clip, final_duration=2) # 2x speed
+ clip5 = clip.with_effects([vfx.MultiplySpeed(final_duration=2)]) # 2x speed
target5 = BitmapClip([["A"], ["C"]], fps=1)
assert clip5 == target5
- clip6 = multiply_speed(clip, 4) # 4x speed
+ clip6 = clip.with_effects([vfx.MultiplySpeed(4)]) # 4x speed
target6 = BitmapClip([["A"]], fps=1)
assert (
clip6 == target6
@@ -1099,13 +1022,13 @@ def test_supersample():
def test_time_mirror():
clip = BitmapClip([["AA", "AA"], ["BB", "BB"], ["CC", "CC"]], fps=1)
- clip1 = time_mirror(clip)
+ clip1 = clip.with_effects([vfx.TimeMirror()])
target1 = BitmapClip([["CC", "CC"], ["BB", "BB"], ["AA", "AA"]], fps=1)
assert clip1 == target1
clip2 = BitmapClip([["AA", "AA"], ["BB", "BB"], ["CC", "CC"], ["DD", "DD"]], fps=1)
- clip3 = time_mirror(clip2)
+ clip3 = clip2.with_effects([vfx.TimeMirror()])
target3 = BitmapClip(
[["DD", "DD"], ["CC", "CC"], ["BB", "BB"], ["AA", "AA"]], fps=1
)
@@ -1115,7 +1038,7 @@ def test_time_mirror():
def test_time_symmetrize():
clip = BitmapClip([["AA", "AA"], ["BB", "BB"], ["CC", "CC"]], fps=1)
- clip1 = time_symmetrize(clip)
+ clip1 = clip.with_effects([vfx.TimeSymmetrize()])
target1 = BitmapClip(
[
["AA", "AA"],
@@ -1132,7 +1055,7 @@ def test_time_symmetrize():
def test_audio_normalize():
clip = AudioFileClip("media/crunching.mp3")
- clip = audio_normalize(clip)
+ clip = clip.with_effects([afx.AudioNormalize()])
assert clip.max_volume() == 1
@@ -1140,7 +1063,7 @@ def test_audio_normalize_muted():
z_array = np.array([0.0])
make_frame = lambda t: z_array
clip = AudioClip(make_frame, duration=1, fps=44100)
- clip = audio_normalize(clip)
+ clip = clip.with_effects([afx.AudioNormalize()])
assert np.array_equal(clip.to_soundarray(), z_array)
@@ -1245,12 +1168,11 @@ def test_multiply_volume_audioclip(
)
clip_array = clip.to_soundarray()
- clip_transformed = multiply_volume(
- clip,
+ clip_transformed = clip.with_effects([afx.MultiplyVolume(
factor,
start_time=start_time,
end_time=end_time,
- )
+ )])
clip_transformed_array = clip_transformed.to_soundarray()
assert len(clip_transformed_array)
@@ -1333,12 +1255,11 @@ def test_multiply_volume_audioclip(
def test_multiply_volume_videoclip():
start_time, end_time = (0.1, 0.2)
- clip = multiply_volume(
- VideoFileClip("media/chaplin.mp4").with_subclip(0, 0.3),
+ clip = VideoFileClip("media/chaplin.mp4").with_subclip(0, 0.3).with_effects([afx.MultiplyVolume(
0,
start_time=start_time,
end_time=end_time,
- )
+ )])
clip_soundarray = clip.audio.to_soundarray()
assert len(clip_soundarray)
@@ -1357,8 +1278,8 @@ def test_multiply_stereo_volume():
clip = AudioFileClip("media/crunching.mp3")
# stereo mute
- clip_left_channel_muted = multiply_stereo_volume(clip, left=0)
- clip_right_channel_muted = multiply_stereo_volume(clip, right=0, left=2)
+ clip_left_channel_muted = clip.with_effects([afx.MultiplyStereoVolume(left=0)])
+ clip_right_channel_muted = clip.with_effects([afx.MultiplyStereoVolume(right=0, left=2)])
left_channel_muted = clip_left_channel_muted.to_soundarray()[:, 0]
right_channel_muted = clip_right_channel_muted.to_soundarray()[:, 1]
@@ -1376,7 +1297,7 @@ def test_multiply_stereo_volume():
# mono muted
sinus_wave = lambda t: [np.sin(440 * 2 * np.pi * t)]
mono_clip = AudioClip(sinus_wave, duration=1, fps=22050)
- muted_mono_clip = multiply_stereo_volume(mono_clip, left=0)
+ muted_mono_clip = mono_clip.with_effects([afx.MultiplyStereoVolume(left=0)])
mono_channel_muted = muted_mono_clip.to_soundarray()
z_channel = np.zeros(len(mono_channel_muted))
@@ -1384,9 +1305,7 @@ def test_multiply_stereo_volume():
# mono doubled
mono_clip = AudioClip(sinus_wave, duration=1, fps=22050)
- doubled_mono_clip = multiply_stereo_volume(
- mono_clip, left=None, right=2
- ) # using right
+ doubled_mono_clip = mono_clip.with_effects([afx.MultiplyStereoVolume(left=None, right=2)]) # using right
mono_channel_doubled = doubled_mono_clip.to_soundarray()
d_channel = mono_clip.to_soundarray() * 2
assert np.array_equal(mono_channel_doubled, d_channel)
@@ -1425,7 +1344,7 @@ def test_audio_delay(stereo_wave, duration, offset, n_repeats, decay):
clip_array = clip.to_soundarray()
# stereo delayed clip
- delayed_clip = audio_delay(clip, offset=offset, n_repeats=n_repeats, decay=decay)
+ delayed_clip = clip.with_effects([afx.AudioDelay(offset=offset, n_repeats=n_repeats, decay=decay)])
delayed_clip_array = delayed_clip.to_soundarray()
# size of chunks with audios
@@ -1450,7 +1369,7 @@ def test_audio_delay(stereo_wave, duration, offset, n_repeats, decay):
if i == 0:
assert np.array_equal(
delayed_clip_array[:, :][sound_start_at:sound_ends_at],
- multiply_volume(clip, decayments[i]).to_soundarray(),
+ clip.with_effects([afx.MultiplyVolume(decayments[i])]).to_soundarray(),
)
# muted chunk
@@ -1495,7 +1414,7 @@ def test_audio_fadein(
make_frame = mono_wave(440)
clip = AudioClip(make_frame, duration=clip_duration, fps=fps)
- new_clip = audio_fadein(clip, fadein_duration)
+ new_clip = clip.with_effects([afx.AudioFadeIn(fadein_duration)])
# first frame is muted
first_frame = new_clip.get_frame(0)
@@ -1516,7 +1435,7 @@ def test_audio_fadein(
start_times = np.arange(0, fadein_duration, time_foreach_part)
for i, start_time in enumerate(start_times):
end_time = start_time + time_foreach_part
- subclip_max_volume = new_clip.subclip(start_time, end_time).max_volume()
+ subclip_max_volume = new_clip.with_subclip(start_time, end_time).max_volume()
possible_value = (i + 1) / n_parts
assert round(subclip_max_volume, 2) in [
@@ -1530,7 +1449,7 @@ def test_audio_fadein(
start_times = np.arange(fadein_duration, clip_duration, time_foreach_part)
for i, start_time in enumerate(start_times):
end_time = start_time + time_foreach_part
- subclip_max_volume = new_clip.subclip(start_time, end_time).max_volume()
+ subclip_max_volume = new_clip.with_subclip(start_time, end_time).max_volume()
assert round(subclip_max_volume, 4) == 1
@@ -1556,7 +1475,7 @@ def test_audio_fadeout(
make_frame = mono_wave(440)
clip = AudioClip(make_frame, duration=clip_duration, fps=fps)
- new_clip = audio_fadeout(clip, fadeout_duration)
+ new_clip = clip.with_effects([afx.AudioFadeOut(fadeout_duration)])
fadeout_duration = convert_to_seconds(fadeout_duration)
@@ -1572,7 +1491,7 @@ def test_audio_fadeout(
)
for i, start_time in enumerate(start_times):
end_time = start_time + time_foreach_part
- subclip_max_volume = new_clip.subclip(start_time, end_time).max_volume()
+ subclip_max_volume = new_clip.with_subclip(start_time, end_time).max_volume()
possible_value = 1 - i * 0.1
assert round(subclip_max_volume, 2) in [
@@ -1586,7 +1505,7 @@ def test_audio_fadeout(
start_times = np.arange(0, clip_duration - fadeout_duration, time_foreach_part)
for i, start_time in enumerate(start_times):
end_time = start_time + time_foreach_part
- subclip_max_volume = new_clip.subclip(start_time, end_time).max_volume()
+ subclip_max_volume = new_clip.with_subclip(start_time, end_time).max_volume()
assert round(subclip_max_volume, 4) == 1