123456789_123456789_123456789_123456789_123456789_

Class: ActiveStorage::Analyzer::AudioAnalyzer

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActiveStorage::Analyzer
Defined in: activestorage/lib/active_storage/analyzer/audio_analyzer.rb

Overview

Active Storage Audio Analyzer

Extracts duration (seconds), bit_rate (bits/s), sample_rate (hertz) and tags (internal metadata) from an audio blob.

Example:

ActiveStorage::Analyzer::AudioAnalyzer.new(blob).
# => { duration: 5.0, bit_rate: 320340, sample_rate: 44100, tags: { encoder: "Lavc57.64", ... } }

This analyzer requires the FFmpeg system library, which is not provided by Rails.

Class Attribute Summary

::ActiveStorage::Analyzer - Inherited

.analyze_later?

Implement this method in concrete subclasses.

Class Method Summary

::ActiveStorage::Analyzer - Inherited

.accept?

Implement this method in a concrete subclass.

.new

Instance Attribute Summary

Instance Method Summary

::ActiveStorage::Analyzer - Inherited

#metadata

Override this method in a concrete subclass.

#download_blob_to_tempfile

Downloads the blob to a tempfile on disk.

#instrument, #logger, #tmpdir

Constructor Details

This class inherits a constructor from ActiveStorage::Analyzer

Class Method Details

.accept?(blob) ⇒ Boolean

[ GitHub ]

  
# File 'activestorage/lib/active_storage/analyzer/audio_analyzer.rb', line 15

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

Instance Method Details

#audio_stream (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/analyzer/audio_analyzer.rb', line 44

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

#bit_rate (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/analyzer/audio_analyzer.rb', line 29

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

#duration (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/analyzer/audio_analyzer.rb', line 24

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

#ffprobe_path (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/analyzer/audio_analyzer.rb', line 73

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

#metadata

[ GitHub ]

  
# File 'activestorage/lib/active_storage/analyzer/audio_analyzer.rb', line 19

def 
  { duration: duration, bit_rate: bit_rate, sample_rate: sample_rate, tags: tags }.compact
end

#probe (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/analyzer/audio_analyzer.rb', line 52

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

#probe_from(file) (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/analyzer/audio_analyzer.rb', line 56

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

#sample_rate (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/analyzer/audio_analyzer.rb', line 34

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

#streams (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/analyzer/audio_analyzer.rb', line 48

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

#tags (private)

[ GitHub ]

  
# File 'activestorage/lib/active_storage/analyzer/audio_analyzer.rb', line 39

def tags
  tags = audio_stream["tags"]
  Hash(tags) if tags
end