From 679b1b4c4732f1b7ed065816101bb5e18a5bd0fd Mon Sep 17 00:00:00 2001 From: Malte Rosenbjerg Date: Tue, 11 Aug 2020 00:46:44 +0200 Subject: [PATCH] Handle video frame rotation #84 Former-commit-id: 3b5f677a2c8c9593a174ba6204fbf3f190bc4e06 --- FFMpegCore/FFMpeg/FFMpeg.cs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/FFMpegCore/FFMpeg/FFMpeg.cs b/FFMpegCore/FFMpeg/FFMpeg.cs index cc042e3..ca94825 100644 --- a/FFMpegCore/FFMpeg/FFMpeg.cs +++ b/FFMpegCore/FFMpeg/FFMpeg.cs @@ -104,31 +104,31 @@ private static FFMpegArguments BuildSnapshotArguments(MediaAnalysis source, Size .Resize(size); } - private static Size? PrepareSnapshotSize(MediaAnalysis source, Size? size) + private static Size? PrepareSnapshotSize(MediaAnalysis source, Size? wantedSize) { - if (size == null || (size.Value.Height == 0 && size.Value.Width == 0)) - size = new Size(source.PrimaryVideoStream.Width, source.PrimaryVideoStream.Height); - - if (size.Value.Width != size.Value.Height) + if (wantedSize == null || (wantedSize.Value.Height <= 0 && wantedSize.Value.Width <= 0)) + return null; + + var currentSize = new Size(source.PrimaryVideoStream.Width, source.PrimaryVideoStream.Height); + if (source.PrimaryVideoStream.Rotation == 90 || source.PrimaryVideoStream.Rotation == 180) + currentSize = new Size(source.PrimaryVideoStream.Height, source.PrimaryVideoStream.Width); + + if (wantedSize.Value.Width != currentSize.Width || wantedSize.Value.Height != currentSize.Height) { - if (size.Value.Width == 0) + if (wantedSize.Value.Width <= 0 && wantedSize.Value.Height > 0) { - var ratio = (double)size.Value.Height / source.PrimaryVideoStream.Height; - - size = new Size((int)(source.PrimaryVideoStream.Width * ratio), - (int)(source.PrimaryVideoStream.Height * ratio)); + var ratio = (double)wantedSize.Value.Height / currentSize.Height; + return new Size((int)(currentSize.Width * ratio), (int)(currentSize.Height * ratio)); } - - if (size.Value.Height == 0) + if (wantedSize.Value.Height <= 0 && wantedSize.Value.Width > 0) { - var ratio = (double)size.Value.Width / source.PrimaryVideoStream.Width; - - size = new Size((int)(source.PrimaryVideoStream.Width * ratio), - (int)(source.PrimaryVideoStream.Height * ratio)); + var ratio = (double)wantedSize.Value.Width / currentSize.Width; + return new Size((int)(currentSize.Width * ratio), (int)(currentSize.Height * ratio)); } + return wantedSize; } - return size; + return null; } ///