Skip to content

Commit

Permalink
Remove BitmapClip.set_fps and .set_duration (#1288)
Browse files Browse the repository at this point in the history
Remove BitmapClip.set_fps and .set_duration in favour of setting fps and duration directly in the __init__
  • Loading branch information
tburrows13 committed Aug 6, 2020
1 parent 953357d commit 79b2c1d
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 123 deletions.
56 changes: 25 additions & 31 deletions moviepy/video/VideoClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,10 @@ def search(string, arg):


class BitmapClip(VideoClip):
def __init__(self, bitmap_frames, *, color_dict=None, ismask=False):
@convert_to_seconds(["duration"])
def __init__(
self, bitmap_frames, *, fps=None, duration=None, color_dict=None, ismask=False
):
"""
Creates a VideoClip object from a bitmap representation. Primarily used in the test suite.
Expand All @@ -1341,6 +1344,14 @@ def __init__(self, bitmap_frames, *, color_dict=None, ismask=False):
"RGGGR",
"RGGGR"]]
fps
The number of frames per second to display the clip at. `duration` will calculated from the total number of frames.
If both `fps` and `duration` are set, `duration` will be ignored.
duration
The total duration of the clip. `fps` will be calculated from the total number of frames.
If both `fps` and `duration` are set, `duration` will be ignored.
color_dict
A dictionary that can be used to set specific (r, g, b) values that correspond
to the letters used in ``bitmap_frames``.
Expand All @@ -1364,6 +1375,8 @@ def __init__(self, bitmap_frames, *, color_dict=None, ismask=False):
Set to ``True`` if the clip is going to be used as a mask.
"""
assert fps is not None or duration is not None

if color_dict:
self.color_dict = color_dict
else:
Expand All @@ -1388,39 +1401,20 @@ def __init__(self, bitmap_frames, *, color_dict=None, ismask=False):
frame_list.append(np.array(output_frame))

frame_array = np.array(frame_list)
VideoClip.__init__(
self, make_frame=lambda t: frame_array[int(t)], ismask=ismask
)

self.total_frames = len(frame_array)
self.fps = None

@convert_to_seconds(["duration"])
def set_duration(self, duration, change_end=True):
"""
Sets the ``duration`` attribute of the clip.
Additionally, if the clip's ``fps`` attribute has not already been set, it will
be set based on the new duration and the total number of frames.
"""
if self.fps is None:
return (
super()
.set_duration(duration=duration, change_end=change_end)
.set_fps(int(self.total_frames / duration))
)

return super().set_duration(duration=duration, change_end=change_end)
if fps is None:
fps = self.total_frames / duration
else:
duration = self.total_frames / fps

def set_fps(self, fps):
"""
Sets the ``fps`` attribute of the clip.
Additionally, if the clip's ``duration`` attribute has not already been set, it will
be set based on the new fps and the total number of frames.
"""
total_duration = self.total_frames / fps
if self.duration is None or self.duration > total_duration:
return super().set_fps(fps).set_duration(total_duration)
return super().set_fps(fps)
VideoClip.__init__(
self,
make_frame=lambda t: frame_array[int(t)],
ismask=ismask,
duration=duration,
)
self.fps = fps

def to_bitmap(self, color_dict=None):
"""
Expand Down
26 changes: 10 additions & 16 deletions tests/test_BitmapClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ def test_clip_generation():
]
)

clip = BitmapClip(bitmap)
clip.duration = len(bitmap)
clip.fps = 1
clip = BitmapClip(bitmap, fps=1)
frame_array = np.array(list(clip.iter_frames()))

# Check that frame_list == expected_frame_list
Expand All @@ -31,25 +29,21 @@ def test_clip_generation():
assert not np.array_equal(frame_array, unexpected_frame_array)


def test_set_fps():
def test_setting_fps():
bitmap = [["R"], ["R"], ["B"], ["B"], ["G"], ["G"]]
clip = BitmapClip(bitmap)
clip = BitmapClip(bitmap, fps=1)

fps_clip = clip.set_fps(1)
assert fps_clip.fps == 1
assert fps_clip.duration == 6
assert clip.fps == 1
assert clip.duration == 6


def test_set_duration():
def test_setting_duration():
bitmap = [["R"], ["R"], ["B"], ["B"], ["G"], ["G"]]
clip = BitmapClip(bitmap)
clip = BitmapClip(bitmap, duration=6)

duration_clip = clip.set_duration(len(bitmap))
assert duration_clip.fps == 1
assert duration_clip.duration == 6
assert clip.fps == 1
assert clip.duration == 6


if __name__ == "__main__":
test_clip_generation()
test_set_fps()
test_set_duration()
pytest.main()
6 changes: 3 additions & 3 deletions tests/test_Clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ def test_clip_equality():
bitmap = [["RR", "RR"], ["RB", "RB"]]
different_bitmap = [["RR", "RB"], ["RB", "RB"]]

clip = BitmapClip(bitmap).set_fps(1)
same_clip = BitmapClip(bitmap).set_fps(1)
clip = BitmapClip(bitmap, fps=1)
same_clip = BitmapClip(bitmap, fps=1)

different_clip = BitmapClip(different_bitmap).set_fps(1)
different_clip = BitmapClip(different_bitmap, fps=1)

assert clip == same_clip
assert clip != different_clip
Expand Down
12 changes: 6 additions & 6 deletions tests/test_VideoClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@


def test_aspect_ratio():
clip = BitmapClip([["AAA", "BBB"]])
clip = BitmapClip([["AAA", "BBB"]], fps=1)
assert clip.aspect_ratio == 1.5


Expand Down Expand Up @@ -217,8 +217,8 @@ def test_setopacity():


def test_set_layer():
bottom_clip = BitmapClip([["ABC"], ["BCA"], ["CAB"]]).set_fps(1).set_layer(1)
top_clip = BitmapClip([["DEF"], ["EFD"]]).set_fps(1).set_layer(2)
bottom_clip = BitmapClip([["ABC"], ["BCA"], ["CAB"]], fps=1).set_layer(1)
top_clip = BitmapClip([["DEF"], ["EFD"]], fps=1).set_layer(2)

composite_clip = CompositeVideoClip([bottom_clip, top_clip])
reversed_composite_clip = CompositeVideoClip([top_clip, bottom_clip])
Expand All @@ -230,13 +230,13 @@ def test_set_layer():
assert top_clip.subclip(0, 2) == composite_clip.subclip(0, 2)

# Make sure that it works even when there is only one clip playing at that time
target_clip = BitmapClip([["DEF"], ["EFD"], ["CAB"]]).set_fps(1)
target_clip = BitmapClip([["DEF"], ["EFD"], ["CAB"]], fps=1)
assert composite_clip == target_clip


def test_compositing_with_same_layers():
bottom_clip = BitmapClip([["ABC"], ["BCA"]]).set_fps(1)
top_clip = BitmapClip([["DEF"], ["EFD"]]).set_fps(1)
bottom_clip = BitmapClip([["ABC"], ["BCA"]], fps=1)
top_clip = BitmapClip([["DEF"], ["EFD"]], fps=1)

composite_clip = CompositeVideoClip([bottom_clip, top_clip])
reversed_composite_clip = CompositeVideoClip([top_clip, bottom_clip])
Expand Down
4 changes: 2 additions & 2 deletions tests/test_compositing.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ def test_clips_array_duration():


def test_concatenate_self():
clip = BitmapClip([["AAA", "BBB"], ["CCC", "DDD"]]).set_fps(1)
target = BitmapClip([["AAA", "BBB"], ["CCC", "DDD"]]).set_fps(1)
clip = BitmapClip([["AAA", "BBB"], ["CCC", "DDD"]], fps=1)
target = BitmapClip([["AAA", "BBB"], ["CCC", "DDD"]], fps=1)

concatenated = concatenate_videoclips([clip])

Expand Down
Loading

0 comments on commit 79b2c1d

Please sign in to comment.