Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add change_duration flag to Clip.set_fps() #1329

Merged
merged 5 commits into from
Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added <!-- for new features -->
- New `pix_fmt` parameter in `VideoFileClip`, `VideoClip.write_videofile()`, `VideoClip.write_gif()` that allows passing a custom `pix_fmt` parameter such as `"bgr24"` to FFmpeg [#1237]
- New `change_duration` parameter in `Clip.set_fps()` that allows changing the video speed to match the new fps [#1329]

### Changed <!-- for changes in existing functionality -->
- `FFMPEG_AudioReader.close_proc()` -> `FFMPEG_AudioReader.close()` for consistency with `FFMPEG_VideoReader` [#1220]
Expand Down
19 changes: 15 additions & 4 deletions moviepy/Clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,22 @@ def set_make_frame(self, make_frame):
"""
self.make_frame = make_frame

@outplace
def set_fps(self, fps):
def set_fps(self, fps, change_duration=False):
"""Returns a copy of the clip with a new default fps for functions like
write_videofile, iterframe, etc."""
self.fps = fps
write_videofile, iterframe, etc.
If ``change_duration=True``, then the video speed will change to match the
new fps (conserving all frames 1:1). For example, if the fps is
halved in this mode, the duration will be doubled."""

if change_duration:
from moviepy.video.fx.speedx import speedx

newclip = speedx(self, fps / self.fps)
else:
newclip = self.copy()

newclip.fps = fps
return newclip

@outplace
def set_ismask(self, ismask):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_VideoClip.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,5 +261,31 @@ def test_withoutaudio():
close_all_clips(locals())


def test_setfps_withoutchangeduration():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0, 1)
# The sum is unique for each frame, so we can use it as a frame-ID
# to check which frames are being preserved
clip_sums = [f.sum() for f in clip.iter_frames()]

clip2 = clip.set_fps(48)
clip2_sums = [f.sum() for f in clip2.iter_frames()]
assert clip2_sums[::2] == clip_sums
assert clip2.duration == clip.duration
close_all_clips(locals())


def test_setfps_withchangeduration():
clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0, 1)
# The sum is unique for each frame, so we can use it as a frame-ID
# to check which frames are being preserved
clip_sums = [f.sum() for f in clip.iter_frames()]

clip2 = clip.set_fps(48, change_duration=True)
clip2_sums = [f.sum() for f in clip2.iter_frames()]
assert clip2_sums == clip_sums
assert clip2.duration == clip.duration / 2
close_all_clips(locals())


if __name__ == "__main__":
pytest.main()