Skip to content

Commit 776626f

Browse files
author
Anup Narkhede
authored
Fix rotation detection for HDR videos (rails#50854)
Fixes rails#50853 The video analyzer was relying on the positional reference of the Display Matrix side_data to fetch the rotation value. However, the side_data is not guaranteed to be in the same position. For instance, HDR videos shot on iOS have "DOVI configuration record" side_data in the first position, followed by the "Display Matrix" side data containing the rotation value. This fix removes the positional reference and explicitely searches for the "Display Matrix" side_data to retrieve the rotation angle.
1 parent ee88f41 commit 776626f

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

activestorage/lib/active_storage/analyzer/video_analyzer.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,15 @@ def duration
5555
def angle
5656
if tags["rotate"]
5757
Integer(tags["rotate"])
58-
elsif side_data && side_data[0] && side_data[0]["rotation"]
59-
Integer(side_data[0]["rotation"])
58+
elsif display_matrix && display_matrix["rotation"]
59+
Integer(display_matrix["rotation"])
6060
end
6161
end
6262

63+
def display_matrix
64+
side_data.detect { |data| data["side_data_type"] == "Display Matrix" }
65+
end
66+
6367
def display_aspect_ratio
6468
if descriptor = video_stream["display_aspect_ratio"]
6569
if terms = descriptor.split(":", 2)

activestorage/test/analyzer/video_analyzer_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ class ActiveStorage::Analyzer::VideoAnalyzerTest < ActiveSupport::TestCase
2929
assert_includes [90, -90], metadata[:angle]
3030
end
3131

32+
test "analyzing a rotated HDR video" do
33+
blob = create_file_blob(filename: "rotated_hdr_video.mov", content_type: "video/quicktime")
34+
metadata = extract_metadata_from(blob)
35+
36+
assert_equal 1080.0, metadata[:width]
37+
assert_equal 1920.0, metadata[:height]
38+
assert_equal(-90, metadata[:angle])
39+
end
40+
3241
test "analyzing a video with rectangular samples" do
3342
blob = create_file_blob(filename: "video_with_rectangular_samples.mp4", content_type: "video/mp4")
3443
metadata = extract_metadata_from(blob)
Binary file not shown.

0 commit comments

Comments
 (0)