From 48a7ec49bf0155c6f3c67eb273f17ec6b0a27e18 Mon Sep 17 00:00:00 2001 From: Breno Gazzola Date: Wed, 9 Jun 2021 10:56:52 -0300 Subject: [PATCH] Add metadata value for presence of video channel in video blobs --- activestorage/CHANGELOG.md | 7 +++++++ .../lib/active_storage/analyzer/video_analyzer.rb | 9 +++++++-- activestorage/test/analyzer/video_analyzer_test.rb | 5 +++++ guides/source/configuring.md | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/activestorage/CHANGELOG.md b/activestorage/CHANGELOG.md index b9558e1c0c..b3771fdbe5 100644 --- a/activestorage/CHANGELOG.md +++ b/activestorage/CHANGELOG.md @@ -1,3 +1,10 @@ +* Add metadata value for presence of video channel in video blobs + + The `metadata` attribute of video blobs has a new boolean key named `video` that is set to + `true` if the file has an video channel and `false` if it doesn't. + + *Breno Gazzola* + * Deprecate usage of `purge` and `purge_later` from the association extension. *Jacopo Beschi* diff --git a/activestorage/lib/active_storage/analyzer/video_analyzer.rb b/activestorage/lib/active_storage/analyzer/video_analyzer.rb index c1bb3a7dae..516b0f83fe 100644 --- a/activestorage/lib/active_storage/analyzer/video_analyzer.rb +++ b/activestorage/lib/active_storage/analyzer/video_analyzer.rb @@ -9,11 +9,12 @@ module ActiveStorage # * Angle (degrees) # * Display aspect ratio # * Audio (true if file has an audio channel, false if not) + # * Video (true if file has an video channel, false if not) # # Example: # # ActiveStorage::Analyzer::VideoAnalyzer.new(blob).metadata - # # => { width: 640.0, height: 480.0, duration: 5.0, angle: 0, display_aspect_ratio: [4, 3], audio: true } + # # => { width: 640.0, height: 480.0, duration: 5.0, angle: 0, display_aspect_ratio: [4, 3], audio: true, video: true } # # When a video's angle is 90 or 270 degrees, its width and height are automatically swapped for convenience. # @@ -24,7 +25,7 @@ module ActiveStorage end def metadata - { width: width, height: height, duration: duration, angle: angle, display_aspect_ratio: display_aspect_ratio, audio: audio? }.compact + { width: width, height: height, duration: duration, angle: angle, display_aspect_ratio: display_aspect_ratio, audio: audio?, video: video? }.compact end private @@ -72,6 +73,10 @@ module ActiveStorage audio_stream.present? end + def video? + video_stream.present? + end + def computed_height if encoded_width && display_height_scale encoded_width * display_height_scale diff --git a/activestorage/test/analyzer/video_analyzer_test.rb b/activestorage/test/analyzer/video_analyzer_test.rb index 4708d8abfd..80e89463be 100644 --- a/activestorage/test/analyzer/video_analyzer_test.rb +++ b/activestorage/test/analyzer/video_analyzer_test.rb @@ -15,6 +15,7 @@ class ActiveStorage::Analyzer::VideoAnalyzerTest < ActiveSupport::TestCase assert_equal [4, 3], metadata[:display_aspect_ratio] assert_equal 5.166648, metadata[:duration] assert metadata[:audio] + assert metadata[:video] assert_not_includes metadata, :angle end @@ -54,6 +55,7 @@ class ActiveStorage::Analyzer::VideoAnalyzerTest < ActiveSupport::TestCase assert_equal 480, metadata[:height] assert_equal 5.229000, metadata[:duration] assert metadata[:audio] + assert metadata[:video] end test "analyzing a video without a video stream" do @@ -63,12 +65,15 @@ class ActiveStorage::Analyzer::VideoAnalyzerTest < ActiveSupport::TestCase assert_not_includes metadata, :width assert_not_includes metadata, :height assert_equal 1.022000, metadata[:duration] + assert_not metadata[:video] + assert metadata[:audio] end test "analyzing a video without an audio stream" do blob = create_file_blob(filename: "video_without_audio_stream.mp4", content_type: "video/mp4") metadata = extract_metadata_from(blob) + assert metadata[:video] assert_not metadata[:audio] end end diff --git a/guides/source/configuring.md b/guides/source/configuring.md index 93d717af3f..7d6edcc010 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -987,7 +987,7 @@ You can find more detailed configuration options in the * `config.active_storage.variant_processor` accepts a symbol `:mini_magick` or `:vips`, specifying whether variant transformations will be performed with MiniMagick or ruby-vips. The default is `:mini_magick`. -* `config.active_storage.analyzers` accepts an array of classes indicating the analyzers available for Active Storage blobs. The default is `[ActiveStorage::Analyzer::ImageAnalyzer, ActiveStorage::Analyzer::VideoAnalyzer, ActiveStorage::Analyzer::AudioAnalyzer]`. The image analyzer can extract width and height of an image blob; the video analyzer can extract width, height, duration, angle, aspect ratio and presence/absence of audio channel of a video blob; the audio analyzer can extract duration and bit rate of an audio blob. +* `config.active_storage.analyzers` accepts an array of classes indicating the analyzers available for Active Storage blobs. The default is `[ActiveStorage::Analyzer::ImageAnalyzer, ActiveStorage::Analyzer::VideoAnalyzer, ActiveStorage::Analyzer::AudioAnalyzer]`. The image analyzer can extract width and height of an image blob; the video analyzer can extract width, height, duration, angle, aspect ratio and presence/absence of video/audio channels of a video blob; the audio analyzer can extract duration and bit rate of an audio blob. * `config.active_storage.previewers` accepts an array of classes indicating the image previewers available in Active Storage blobs. The default is `[ActiveStorage::Previewer::PopplerPDFPreviewer, ActiveStorage::Previewer::MuPDFPreviewer, ActiveStorage::Previewer::VideoPreviewer]`. `PopplerPDFPreviewer` and `MuPDFPreviewer` can generate a thumbnail from the first page of a PDF blob; `VideoPreviewer` from the relevant frame of a video blob.