Skip to content

Commit

Permalink
Take into account rotation metadata to define video size (#577)
Browse files Browse the repository at this point in the history
* On autorotate flip the video_size

- ffmpeg autorotates videos but the video_size is unhanged,
  this results e.g. in stretched output images or even unreadable images or videos.
  This commit exchanges width and height if 90 or 270 degree rotation was detected.

* Use fix directly in video writer, add test cases

* Remove uneeded files

* Add CHANGELOG entry

Co-authored-by: Florian Zierer <[email protected]>
Co-authored-by: Álvaro Mondéjar <[email protected]>
  • Loading branch information
3 people authored May 19, 2021
1 parent c8887f0 commit a472c86
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed `clip.preview()` crashing at exit when running inside Jupyter Notebook in Windows [\#1537](https://github.com/Zulko/moviepy/pull/1537)
- Fixed rotate FX not being applied to mask images [\#1399](https://github.com/Zulko/moviepy/pull/1399)
- Fixed opacity error blitting VideoClips [\#1552](https://github.com/Zulko/moviepy/pull/1552)
- Fixed rotation metadata of input not being taken into account rendering VideoClips [\#577](https://github.com/Zulko/moviepy/pull/577)


## [v2.0.0.dev2](https://github.com/zulko/moviepy/tree/v2.0.0.dev2) (2020-10-05)
Expand Down
Binary file added media/rotated-90-degrees.mp4
Binary file not shown.
7 changes: 6 additions & 1 deletion moviepy/video/io/ffmpeg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ def __init__(
)
self.fps = infos["video_fps"]
self.size = infos["video_size"]
self.rotation = infos.get("video_rotation", 0)

# ffmpeg automatically rotates videos if rotation information is
# available, so exchange width and height
self.rotation = abs(infos.get("video_rotation", 0))
if self.rotation in [90, 270]:
self.size = [self.size[1], self.size[0]]

if target_resolution:
if None in target_resolution:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_ffmpeg_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
FFmpegInfosParser,
ffmpeg_parse_infos,
)
from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.video.VideoClip import BitmapClip

from tests.test_helper import TMP_DIR
Expand Down Expand Up @@ -299,6 +300,27 @@ def test_ffmpeg_parse_infos_metadata_with_attached_pic():
assert len(d["metadata"].keys()) == 7


def test_ffmpeg_parse_video_rotation():
d = ffmpeg_parse_infos("media/rotated-90-degrees.mp4")
assert d["video_rotation"] == 90
assert d["video_size"] == [1920, 1080]


def test_correct_video_rotation():
"""See https://github.com/Zulko/moviepy/pull/577"""
clip = VideoFileClip("media/rotated-90-degrees.mp4").subclip(0.2, 0.4)

corrected_rotation_filename = os.path.join(
TMP_DIR,
"correct_video_rotation.mp4",
)
clip.write_videofile(corrected_rotation_filename)

d = ffmpeg_parse_infos(corrected_rotation_filename)
assert "video_rotation" not in d
assert d["video_size"] == [1080, 1920]


def test_ffmpeg_parse_infos_multiline_metadata():
"""Check that the parser can parse multiline metadata values."""
infos = """Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/110_PREV_FINAL.mov':
Expand Down

0 comments on commit a472c86

Please sign in to comment.