Extract sample rate in audio analyzer
This commit is contained in:
parent
2cef3ac45b
commit
c0465cf6aa
@ -1,3 +1,7 @@
|
||||
* Add `sample_rate` to `ActiveStorage::Analyzer::AudioAnalyzer` output
|
||||
|
||||
*Matija Čupić*
|
||||
|
||||
* Remove deprecated `purge` and `purge_later` methods from the attachments association.
|
||||
|
||||
*Rafael Mendonça França*
|
||||
|
@ -1,12 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module ActiveStorage
|
||||
# Extracts duration (seconds) and bit_rate (bits/s) from an audio blob.
|
||||
# Extracts duration (seconds), bit_rate (bits/s) and sample_rate (hertz) from an audio blob.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# ActiveStorage::Analyzer::AudioAnalyzer.new(blob).metadata
|
||||
# # => { duration: 5.0, bit_rate: 320340 }
|
||||
# # => { duration: 5.0, bit_rate: 320340, sample_rate: 44100 }
|
||||
#
|
||||
# This analyzer requires the {FFmpeg}[https://www.ffmpeg.org] system library, which is not provided by Rails.
|
||||
class Analyzer::AudioAnalyzer < Analyzer
|
||||
@ -15,7 +15,7 @@ def self.accept?(blob)
|
||||
end
|
||||
|
||||
def metadata
|
||||
{ duration: duration, bit_rate: bit_rate }.compact
|
||||
{ duration: duration, bit_rate: bit_rate, sample_rate: sample_rate }.compact
|
||||
end
|
||||
|
||||
private
|
||||
@ -29,6 +29,11 @@ def bit_rate
|
||||
Integer(bit_rate) if bit_rate
|
||||
end
|
||||
|
||||
def sample_rate
|
||||
sample_rate = audio_stream["sample_rate"]
|
||||
Integer(sample_rate) if sample_rate
|
||||
end
|
||||
|
||||
def audio_stream
|
||||
@audio_stream ||= streams.detect { |stream| stream["codec_type"] == "audio" } || {}
|
||||
end
|
||||
|
@ -12,6 +12,7 @@ class ActiveStorage::Analyzer::AudioAnalyzerTest < ActiveSupport::TestCase
|
||||
|
||||
assert_equal 0.914286, metadata[:duration]
|
||||
assert_equal 128000, metadata[:bit_rate]
|
||||
assert_equal 44100, metadata[:sample_rate]
|
||||
end
|
||||
|
||||
test "instrumenting analysis" do
|
||||
|
Loading…
Reference in New Issue
Block a user