Skip to content

Commit

Permalink
Fix rotation detection for HDR videos (rails#50854)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
railsbob authored and viralpraxis committed Mar 24, 2024
1 parent 795d223 commit 9b21841
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions activestorage/lib/active_storage/analyzer/video_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ def duration
def angle
if tags["rotate"]
Integer(tags["rotate"])
elsif side_data && side_data[0] && side_data[0]["rotation"]
Integer(side_data[0]["rotation"])
elsif display_matrix && display_matrix["rotation"]
Integer(display_matrix["rotation"])
end
end

def display_matrix
side_data.detect { |data| data["side_data_type"] == "Display Matrix" }
end

def display_aspect_ratio
if descriptor = video_stream["display_aspect_ratio"]
if terms = descriptor.split(":", 2)
Expand Down
9 changes: 9 additions & 0 deletions activestorage/test/analyzer/video_analyzer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ class ActiveStorage::Analyzer::VideoAnalyzerTest < ActiveSupport::TestCase
assert_includes [90, -90], metadata[:angle]
end

test "analyzing a rotated HDR video" do
blob = create_file_blob(filename: "rotated_hdr_video.mov", content_type: "video/quicktime")
metadata = extract_metadata_from(blob)

assert_equal 1080.0, metadata[:width]
assert_equal 1920.0, metadata[:height]
assert_equal(-90, metadata[:angle])
end

test "analyzing a video with rectangular samples" do
blob = create_file_blob(filename: "video_with_rectangular_samples.mp4", content_type: "video/mp4")
metadata = extract_metadata_from(blob)
Expand Down
Binary file not shown.

0 comments on commit 9b21841

Please sign in to comment.