Class: ActiveStorage::Audio::Analyzer

Inherits:
ActiveStorage::Analyzer
  • Object
show all
Defined in:
lib/active_storage/audio/analyzer.rb

Overview

Uses ‘ffprobe` to quickly find information about the track file, such as duration, bitrate, sample rate, and its codec.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.accept?(blob) ⇒ Boolean

Tests whether the given ‘blob` is an audio file. If so, this file will be analyzed by the `Audio::Analyzer`.

Parameters:

  • blob (ActiveStorage::Blob)
    • Blob to test

Returns:

  • (Boolean)

    whether ‘#audio?` returns true



13
14
15
# File 'lib/active_storage/audio/analyzer.rb', line 13

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

Instance Method Details

#bit_rateFloat

Bit rate of this audio file, e.g. ‘16.0`

Returns:

  • (Float)


41
42
43
# File 'lib/active_storage/audio/analyzer.rb', line 41

def bit_rate
  stream['bit_rate'].to_f
end

#channel_layoutString

Layout of channels.

Returns:

  • (String)


69
70
71
# File 'lib/active_storage/audio/analyzer.rb', line 69

def channel_layout
  stream['channel_layout']
end

#channelsInteger

Number of audio channels (1 for mono, 2 for stereo)

Returns:

  • (Integer)


62
63
64
# File 'lib/active_storage/audio/analyzer.rb', line 62

def channels
  stream['channels'].to_i
end

#codecString

Codec used to encode this audio file.

Returns:

  • (String)


55
56
57
# File 'lib/active_storage/audio/analyzer.rb', line 55

def codec
  stream['codec']
end

#durationFloat

Duration of this audio file in seconds.

Returns:

  • (Float)


34
35
36
# File 'lib/active_storage/audio/analyzer.rb', line 34

def duration
  stream['duration'].to_f
end

#metadataHash

Metadata to add to the blob. Any empty values are omitted.

Returns:

  • (Hash)


20
21
22
23
24
25
26
27
28
29
# File 'lib/active_storage/audio/analyzer.rb', line 20

def 
  {
    duration: duration,
    bit_rate: bit_rate,
    sample_rate: sample_rate,
    codec: codec,
    channels: channels,
    channel_layout: channel_layout
  }.compact
end

#sample_rateFloat

Sample rate of this audio file, e.g. ‘44.1`

Returns:

  • (Float)


48
49
50
# File 'lib/active_storage/audio/analyzer.rb', line 48

def sample_rate
  stream['sample_rate'].to_i
end