class ActiveStorage::Analyzer::AudioAnalyzer

This analyzer requires the FFmpeg system library, which is not provided by Rails.
# => { duration: 5.0, bit_rate: 320340 }
ActiveStorage::Analyzer::AudioAnalyzer.new(blob).metadata
Example:
Extracts duration (seconds) and bit_rate (bits/s) from an audio blob.

def self.accept?(blob)

def self.accept?(blob)
  blob.audio?
end

def audio_stream

def audio_stream
  @audio_stream ||= streams.detect { |stream| stream["codec_type"] == "audio" } || {}
end

def bit_rate

def bit_rate
  bit_rate = audio_stream["bit_rate"]
  Integer(bit_rate) if bit_rate
end

def duration

def duration
  duration = audio_stream["duration"]
  Float(duration) if duration
end

def ffprobe_path

def ffprobe_path
  ActiveStorage.paths[:ffprobe] || "ffprobe"
end

def metadata

def metadata
  { duration: duration, bit_rate: bit_rate }.compact
end

def probe

def probe
  @probe ||= download_blob_to_tempfile { |file| probe_from(file) }
end

def probe_from(file)

def probe_from(file)
  instrument(File.basename(ffprobe_path)) do
    IO.popen([ ffprobe_path,
      "-print_format", "json",
      "-show_streams",
      "-show_format",
      "-v", "error",
      file.path
    ]) do |output|
      JSON.parse(output.read)
    end
  end
rescue Errno::ENOENT
  logger.info "Skipping audio analysis because ffprobe isn't installed"
  {}
end

def streams

def streams
  probe["streams"] || []
end