class ActiveAnalysis::Analyzer::AudioAnalyzer
Extracts duration (seconds) and bit_rate
(bits/s) from an audio blob.
Example:
ActiveStorage::Analyzer::AudioAnalyzer.new(blob).metadata # => { duration: 5.0, bit_rate: 320340 }
This analyzer requires the FFmpeg system library, which is not provided by Rails.
Public Class Methods
accept?(blob)
click to toggle source
# File lib/active_analysis/analyzer/audio_analyzer.rb, line 15 def self.accept?(blob) blob.audio? end
Public Instance Methods
metadata()
click to toggle source
# File lib/active_analysis/analyzer/audio_analyzer.rb, line 19 def metadata { duration: duration, bit_rate: bit_rate }.compact end
Private Instance Methods
audio_stream()
click to toggle source
# File lib/active_analysis/analyzer/audio_analyzer.rb, line 34 def audio_stream @audio_stream ||= streams.detect { |stream| stream["codec_type"] == "audio" } || {} end
bit_rate()
click to toggle source
# File lib/active_analysis/analyzer/audio_analyzer.rb, line 29 def bit_rate bit_rate = audio_stream["bit_rate"] Integer(bit_rate) if bit_rate end
duration()
click to toggle source
# File lib/active_analysis/analyzer/audio_analyzer.rb, line 24 def duration duration = audio_stream["duration"] Float(duration) if duration end
ffprobe_path()
click to toggle source
# File lib/active_analysis/analyzer/audio_analyzer.rb, line 61 def ffprobe_path ActiveStorage.paths[:ffprobe] || "ffprobe" end
probe()
click to toggle source
# File lib/active_analysis/analyzer/audio_analyzer.rb, line 42 def probe @probe ||= download_blob_to_tempfile { |file| probe_from(file) } end
probe_from(file)
click to toggle source
# File lib/active_analysis/analyzer/audio_analyzer.rb, line 46 def probe_from(file) IO.popen([ffprobe_path, "-print_format", "json", "-show_streams", "-show_format", "-v", "error", file.path ]) do |output| JSON.parse(output.read) end rescue Errno::ENOENT logger.info "Skipping audio analysis because FFmpeg isn't installed" {} end
streams()
click to toggle source
# File lib/active_analysis/analyzer/audio_analyzer.rb, line 38 def streams probe["streams"] || [] end