Skip to content

Commit

Permalink
Refactor accel_decel function to accept a new_duration parameter and …
Browse files Browse the repository at this point in the history
…a func parameter
  • Loading branch information
SohamTilekar committed Mar 23, 2024
1 parent 7f1d027 commit b51385a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
7 changes: 5 additions & 2 deletions test/test_fx/test_accel_decel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest
import numpy
from vidiopy import Data2ImageClip
from vidiopy.video.VideoClip import VideoClip
from vidiopy.video.fx.accel_decel import accel_decel


Expand All @@ -23,7 +22,7 @@ def test_accel_decel():
assert clip.fps == 5

# Test when new_duration is a callable
clip = accel_decel(clip.copy(), new_duration=lambda x: x * 2)
clip = accel_decel(clip.copy(), func=lambda x: x * 2)
assert clip.duration == 20
assert clip.end == None
assert clip.start == 0
Expand All @@ -40,3 +39,7 @@ def test_accel_decel():
clip = Data2ImageClip(image, fps=5)
with pytest.raises(ValueError):
accel_decel(clip.copy(), new_duration=20)


if __name__ == "__main__":
pytest.main([__file__])
26 changes: 12 additions & 14 deletions vidiopy/video/fx/accel_decel.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

def accel_decel(
clip,
new_duration: Callable[[int | float], int | float] | float | int | None = None,
new_duration: float | int | None = None,
ratio: int | float = 1,
func: Callable[[float | int], float | int] | None = None,
):
if new_duration is None and ratio == 1:
...
Expand All @@ -19,18 +20,15 @@ def accel_decel(
else:
raise ValueError("Clip Duration is Not Set")
elif new_duration is not None:
if callable(new_duration):
clip.fl_time_transform(new_duration)
elif isinstance(new_duration, (int, float)):
if clip._dur is not None:
clip.start = new_duration / clip._dur * clip.start
clip.end = (
(new_duration / clip._dur * clip.end)
if clip.end is not None
else None
)
clip._dur = new_duration
else:
raise ValueError("Clip Duration is Not Set")
if clip._dur is not None:
clip.start = new_duration / clip._dur * clip.start
clip.end = (
(new_duration / clip._dur * clip.end) if clip.end is not None else None
)
clip._dur = new_duration
else:
raise ValueError("Clip Duration is Not Set")
if func is not None:
clip.fl_time_transform(func)
clip._sync_audio_video_s_e_d()
return clip

0 comments on commit b51385a

Please sign in to comment.