Skip to content

Commit

Permalink
Make 'start' and 'end' arguments more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed May 24, 2021
1 parent 58ccc2a commit ecd43d6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `video.io.ffmpeg_reader.ffmpeg_parse_infos` returns metadata of the container in attribute `metadata` [\#1466](https://github.com/Zulko/moviepy/pull/1466)
- `center`, `translate` and `bg_color` arguments to `video.fx.rotate` [\#1474](https://github.com/Zulko/moviepy/pull/1474)
- `audio.fx.audio_delay` FX [\#1481](https://github.com/Zulko/moviepy/pull/1481)
- `start` and `end` optional arguments to `multiply_volume` FX which allow to specify a range applying the transformation [\#1572](https://github.com/Zulko/moviepy/pull/1572)
- `start_time` and `end_time` optional arguments to `multiply_volume` FX which allow to specify a range applying the transformation [\#1572](https://github.com/Zulko/moviepy/pull/1572)

### Changed <!-- for changes in existing functionality -->
- Lots of method and parameter names have been changed. This will be explained better in the documentation soon. See https://github.com/Zulko/moviepy/pull/1170 for more information. [\#1170](https://github.com/Zulko/moviepy/pull/1170)
Expand Down
20 changes: 10 additions & 10 deletions moviepy/audio/fx/multiply_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from moviepy.decorators import audio_video_fx, convert_parameter_to_seconds


def _multiply_volume_in_range(factor, start, end, nchannels):
def _multiply_volume_in_range(factor, start_time, end_time, nchannels):
def factors_filter(factor, t):
return np.array([factor if start <= t_ <= end else 1 for t_ in t])
return np.array([factor if start_time <= t_ <= end_time else 1 for t_ in t])

def multiply_stereo_volume(get_frame, t):
return np.multiply(
Expand All @@ -20,8 +20,8 @@ def multiply_mono_volume(get_frame, t):


@audio_video_fx
@convert_parameter_to_seconds(["start", "end"])
def multiply_volume(clip, factor, start=None, end=None):
@convert_parameter_to_seconds(["start_time", "end_time"])
def multiply_volume(clip, factor, start_time=None, end_time=None):
"""Returns a clip with audio volume multiplied by the
value `factor`. Can be applied to both audio and video clips.
Expand All @@ -31,11 +31,11 @@ def multiply_volume(clip, factor, start=None, end=None):
factor : float
Volume multiplication factor.
start : float, optional
start_time : float, optional
Time from the beginning of the clip until the volume transformation
begins to take effect, in seconds. By default at the beginning.
end : float, optional
end_time : float, optional
Time from the beginning of the clip until the volume transformation
ends to take effect, in seconds. By default at the end.
Expand All @@ -49,9 +49,9 @@ def multiply_volume(clip, factor, start=None, end=None):
>>> half_audio_clip = clip.multiply_volume(0.5) # half audio
>>>
>>> # silenced clip during one second at third
>>> silenced_range_audio_clip = clip.multiply_volume(0, start=2, end=3)
>>> silenced_clip = clip.multiply_volume(0, start_time=2, end_time=3)
"""
if start is None and end is None:
if start_time is None and end_time is None:
return clip.transform(
lambda get_frame, t: factor * get_frame(t),
keep_duration=True,
Expand All @@ -60,8 +60,8 @@ def multiply_volume(clip, factor, start=None, end=None):
return clip.transform(
_multiply_volume_in_range(
factor,
clip.start if start is None else start,
clip.end if end is None else end,
clip.start if start_time is None else start_time,
clip.end if end_time is None else end_time,
clip.nchannels,
),
keep_duration=True,
Expand Down
45 changes: 30 additions & 15 deletions tests/test_fx.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ def test_audio_normalize_muted():


@pytest.mark.parametrize(
("sound_type", "factor", "duration", "start", "end"),
("sound_type", "factor", "duration", "start_time", "end_time"),
(
pytest.param(
"stereo",
Expand Down Expand Up @@ -1188,7 +1188,13 @@ def test_audio_normalize_muted():
),
),
)
def test_multiply_volume_audioclip(sound_type, factor, duration, start, end):
def test_multiply_volume_audioclip(
sound_type,
factor,
duration,
start_time,
end_time,
):
if sound_type == "stereo":
make_frame = lambda t: np.array(
[
Expand All @@ -1206,7 +1212,12 @@ def test_multiply_volume_audioclip(sound_type, factor, duration, start, end):
)
clip_array = clip.to_soundarray()

clip_transformed = multiply_volume(clip, factor, start=start, end=end)
clip_transformed = multiply_volume(
clip,
factor,
start_time=start_time,
end_time=end_time,
)
clip_transformed_array = clip_transformed.to_soundarray()

assert len(clip_transformed_array)
Expand All @@ -1216,18 +1227,20 @@ def test_multiply_volume_audioclip(sound_type, factor, duration, start, end):
left_channel_transformed = clip_transformed_array[:, 0]
right_channel_transformed = clip_transformed_array[:, 1]

if start is None and end is None:
if start_time is None and end_time is None:
expected_left_channel_transformed = clip_array[:, 0] * factor
expected_right_channel_transformed = clip_array[:, 1] * factor
else:
start = convert_to_seconds(start) if start else clip.start
end = convert_to_seconds(end) if end else clip.duration
start_time = convert_to_seconds(start_time) if start_time else clip.start
end_time = convert_to_seconds(end_time) if end_time else clip.end

expected_left_channel_transformed = np.array([])
expected_right_channel_transformed = np.array([])
for i, frame in enumerate(clip_array):
t = i / clip.fps
transformed_frame = frame * (factor if start <= t <= end else 1)
transformed_frame = frame * (
factor if start_time <= t <= end_time else 1
)
expected_left_channel_transformed = np.append(
expected_left_channel_transformed,
transformed_frame[0],
Expand All @@ -1254,16 +1267,18 @@ def test_multiply_volume_audioclip(sound_type, factor, duration, start, end):
else:
# mono clip

if start is None and end is None:
if start_time is None and end_time is None:
expected_clip_transformed_array = clip_array * factor
else:
start = convert_to_seconds(start) if start else clip.start
end = convert_to_seconds(end) if end else clip.duration
start_time = convert_to_seconds(start_time) if start_time else clip.start
end_time = convert_to_seconds(end_time) if end_time else clip.end

expected_clip_transformed_array = np.array([])
for i, frame in enumerate(clip_array[0]):
t = i / clip.fps
transformed_frame = frame * (factor if start <= t <= end else 1)
transformed_frame = frame * (
factor if start_time <= t <= end_time else 1
)
expected_clip_transformed_array = np.append(
expected_clip_transformed_array,
transformed_frame,
Expand All @@ -1283,13 +1298,13 @@ def test_multiply_volume_audioclip(sound_type, factor, duration, start, end):


def test_multiply_volume_videoclip():
start, end = (0.1, 0.2)
start_time, end_time = (0.1, 0.2)

clip = multiply_volume(
VideoFileClip("media/chaplin.mp4").subclip(0, 0.3),
0,
start=start,
end=end,
start_time=start_time,
end_time=end_time,
)
clip_soundarray = clip.audio.to_soundarray()

Expand All @@ -1299,7 +1314,7 @@ def test_multiply_volume_videoclip():

for i, frame in enumerate(clip_soundarray):
t = i / clip.audio.fps
if start <= t <= end:
if start_time <= t <= end_time:
assert np.array_equal(frame, expected_silence)
else:
assert not np.array_equal(frame, expected_silence)
Expand Down

0 comments on commit ecd43d6

Please sign in to comment.