Skip to content

Commit

Permalink
Fix target resolution specification
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dre Westcook committed Jan 12, 2024
1 parent ef65d82 commit 868112c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
15 changes: 9 additions & 6 deletions moviepy/video/io/ffmpeg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/test_ffmpeg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 868112c

Please sign in to comment.