Skip to content

Commit 48a7ec4

Browse files
committed
Add metadata value for presence of video channel in video blobs
1 parent cde84b8 commit 48a7ec4

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

activestorage/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
* Add metadata value for presence of video channel in video blobs
2+
3+
The `metadata` attribute of video blobs has a new boolean key named `video` that is set to
4+
`true` if the file has an video channel and `false` if it doesn't.
5+
6+
*Breno Gazzola*
7+
18
* Deprecate usage of `purge` and `purge_later` from the association extension.
29

310
*Jacopo Beschi*

activestorage/lib/active_storage/analyzer/video_analyzer.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ module ActiveStorage
99
# * Angle (degrees)
1010
# * Display aspect ratio
1111
# * Audio (true if file has an audio channel, false if not)
12+
# * Video (true if file has an video channel, false if not)
1213
#
1314
# Example:
1415
#
1516
# ActiveStorage::Analyzer::VideoAnalyzer.new(blob).metadata
16-
# # => { width: 640.0, height: 480.0, duration: 5.0, angle: 0, display_aspect_ratio: [4, 3], audio: true }
17+
# # => { width: 640.0, height: 480.0, duration: 5.0, angle: 0, display_aspect_ratio: [4, 3], audio: true, video: true }
1718
#
1819
# When a video's angle is 90 or 270 degrees, its width and height are automatically swapped for convenience.
1920
#
@@ -24,7 +25,7 @@ def self.accept?(blob)
2425
end
2526

2627
def metadata
27-
{ width: width, height: height, duration: duration, angle: angle, display_aspect_ratio: display_aspect_ratio, audio: audio? }.compact
28+
{ width: width, height: height, duration: duration, angle: angle, display_aspect_ratio: display_aspect_ratio, audio: audio?, video: video? }.compact
2829
end
2930

3031
private
@@ -72,6 +73,10 @@ def audio?
7273
audio_stream.present?
7374
end
7475

76+
def video?
77+
video_stream.present?
78+
end
79+
7580
def computed_height
7681
if encoded_width && display_height_scale
7782
encoded_width * display_height_scale

activestorage/test/analyzer/video_analyzer_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ActiveStorage::Analyzer::VideoAnalyzerTest < ActiveSupport::TestCase
1515
assert_equal [4, 3], metadata[:display_aspect_ratio]
1616
assert_equal 5.166648, metadata[:duration]
1717
assert metadata[:audio]
18+
assert metadata[:video]
1819
assert_not_includes metadata, :angle
1920
end
2021

@@ -54,6 +55,7 @@ class ActiveStorage::Analyzer::VideoAnalyzerTest < ActiveSupport::TestCase
5455
assert_equal 480, metadata[:height]
5556
assert_equal 5.229000, metadata[:duration]
5657
assert metadata[:audio]
58+
assert metadata[:video]
5759
end
5860

5961
test "analyzing a video without a video stream" do
@@ -63,12 +65,15 @@ class ActiveStorage::Analyzer::VideoAnalyzerTest < ActiveSupport::TestCase
6365
assert_not_includes metadata, :width
6466
assert_not_includes metadata, :height
6567
assert_equal 1.022000, metadata[:duration]
68+
assert_not metadata[:video]
69+
assert metadata[:audio]
6670
end
6771

6872
test "analyzing a video without an audio stream" do
6973
blob = create_file_blob(filename: "video_without_audio_stream.mp4", content_type: "video/mp4")
7074
metadata = extract_metadata_from(blob)
7175

76+
assert metadata[:video]
7277
assert_not metadata[:audio]
7378
end
7479
end

guides/source/configuring.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,7 @@ You can find more detailed configuration options in the
987987

988988
* `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`.
989989

990-
* `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.
990+
* `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.
991991

992992
* `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.
993993

0 commit comments

Comments
 (0)