Skip to content

Commit bedf55b

Browse files
committed
Add counterrotating preview images of videos rotated based on metadata
Based on a proposal by HerMajestyDrMona reported as #16
1 parent 025abc0 commit bedf55b

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "pyvideothumbnailer"
7-
version = "2.0.4"
7+
version = "2.1.0"
88
authors = [
99
{ name="Harald Hetzner", email="57875126+hhtznr@users.noreply.github.com" },
1010
]

src/pyvideothumbnailer/videothumbnailer.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
__author__ = 'Harald Hetzner'
4242
__license__ = 'BSD 3-Clause License'
43-
__version__ = '2.0.4'
43+
__version__ = '2.1.0'
4444

4545
class Parameters:
4646
"""
@@ -752,6 +752,17 @@ def create_preview_thumbnails_for(self, file_path: Path) -> Image:
752752
video_width = int(video_metadata['width'])
753753
# Height in px
754754
video_height = int(video_metadata['height'])
755+
# Rotation in °
756+
video_rotation = 0
757+
if 'rotation' in video_metadata.keys():
758+
video_rotation = int(float(video_metadata['rotation']))
759+
if self.parameters.verbose:
760+
print('Video images are rotated by {}° according to metadata.'.format(video_rotation))
761+
# Swap width and height if the video is rotated by 90° or 270°
762+
if video_rotation in (90, 270):
763+
temp = video_width
764+
video_width = video_height
765+
video_height = temp
755766
# Aspect ratio
756767
video_aspect = float(video_width) / float(video_height)
757768
# Frames per second
@@ -959,7 +970,11 @@ def create_preview_thumbnails_for(self, file_path: Path) -> Image:
959970
# pts = presentation timestamp in time_base units
960971
if frame.pts >= target_timestamp:
961972
# VideoFrame.to_image() returns a PIL image
962-
image = frame.to_image().resize((thumbnail_width, thumbnail_height))
973+
image = frame.to_image()
974+
# Counterrotate the image if the video is rotated by multiples of 90° according to metadata
975+
if video_rotation in (90, 180, 270):
976+
image = image.rotate(-video_rotation, expand=True)
977+
image = image.resize((thumbnail_width, thumbnail_height))
963978
thumbnails_image.paste(image, box=(x, y))
964979
frame_captured = True
965980
break

0 commit comments

Comments
 (0)