From 868112ca976877889e2c6ff97d052737e8d0e163 Mon Sep 17 00:00:00 2001 From: Dre Westcook Date: Sat, 13 Jan 2024 07:10:14 +1100 Subject: [PATCH] Fix target resolution specification When only one dimension was specified in the target resolution it was used to find the scale factor, then used to scale the video with a possible rounding issue, causing a mismatch between what was given and what was received. This change will only calculate the unspecified dimension and use the given value as is. --- moviepy/video/io/ffmpeg_reader.py | 15 +++++++++------ tests/test_ffmpeg_reader.py | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/moviepy/video/io/ffmpeg_reader.py b/moviepy/video/io/ffmpeg_reader.py index b7be97177..5e6ce5bac 100644 --- a/moviepy/video/io/ffmpeg_reader.py +++ b/moviepy/video/io/ffmpeg_reader.py @@ -43,13 +43,16 @@ def __init__( if self.rotation in [90, 270]: self.size = [self.size[1], self.size[0]] - if target_resolution: + if target_resolution and target_resolution != (None, None): if None in target_resolution: - ratio = 1 - for idx, target in enumerate(target_resolution): - if target: - ratio = target / self.size[idx] - self.size = (int(self.size[0] * ratio), int(self.size[1] * ratio)) + first, second = target_resolution + if first: + ratio = first / self.size[0] + second = int(self.size[1] * ratio) + else: + ratio = second / self.size[1] + first = int(self.size[0] * ratio) + self.size = (first, second) else: self.size = target_resolution self.resize_algo = resize_algo diff --git a/tests/test_ffmpeg_reader.py b/tests/test_ffmpeg_reader.py index f01127b73..4fc9749bb 100644 --- a/tests/test_ffmpeg_reader.py +++ b/tests/test_ffmpeg_reader.py @@ -290,6 +290,22 @@ def test_ffmpeg_parse_video_rotation(): assert d["video_size"] == [1920, 1080] +@pytest.mark.parametrize( + "target_resolution,expected_size", + [ + (None, (314, 273)), + ((100, None), (100, 86)), + ((None, 100), (115, 100)), + ((None, None), (314, 273)), + ], +) +def test_video_target_resolution(target_resolution, expected_size): + clip = VideoFileClip( + "media/pigs_in_a_polka.gif", target_resolution=target_resolution + ) + assert clip.size == expected_size + + def test_correct_video_rotation(util): """See https://github.com/Zulko/moviepy/pull/577""" clip = VideoFileClip("media/rotated-90-degrees.mp4").subclip(0.2, 0.4)