Class: PopCap::AudioFile

Inherits:
Object
  • Object
show all
Includes:
Fileable
Defined in:
lib/pop_cap/audio_file.rb

Overview

Public: This is a class for managing audio files on the filesystem. It is used to read & write metadata tags, convert between audio formats, and manage a file on the filesystem using standard UNIX file commands.

Examples

filepath = 'spec/fixtures/sample.flac'
af = AudioFile.new(filepath)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Fileable

#backup, #backup_path, #destroy, #directory, #filename, #move, #rename, #restore, #tmppath

Constructor Details

#initialize(filepath) ⇒ AudioFile

Public: Initialize

filepath - Requires a valid filepath to a file on the local filesystem.

Raises:



34
35
36
37
38
# File 'lib/pop_cap/audio_file.rb', line 34

def initialize(filepath)
  raise(FileNotFound, filepath) unless File.exists?(filepath)
  raise(InvalidAudioFormat, filepath) unless valid_audio_format?(filepath)
  @filepath = File.realpath(filepath)
end

Instance Attribute Details

#filepathObject

Returns the value of attribute filepath.



26
27
28
# File 'lib/pop_cap/audio_file.rb', line 26

def filepath
  @filepath
end

Instance Method Details

#convert(format, bitrate = 192, converter: Converter) ⇒ Object

Public: convert This method converts an audio file between formats. It takes an optional bitrate for mp3 formats.

format - A valid audio file format as string or symbol. bitrate - An optional bitrate for mp3s.

Examples

audio_file = AudioFile.new('spec/fixtures/sample.flac')
audio_file.convert('mp3', 128)
# => 'spec/fixtures/sample.mp3'


52
53
54
# File 'lib/pop_cap/audio_file.rb', line 52

def convert(format, bitrate=192, converter: Converter)
  converter.convert(filepath, {format: format, bitrate: bitrate})
end

#formatted(formatted_hash: FormattedHash) ⇒ Object

Public: This method returns #unformatted tags with any available Formatters automatically applied.

{
  filename: '$HOME/spec/fixtures/sample.flac',
  nb_streams: 1,
  format_name: 'flac',
  format_long_name: 'raw FLAC',
  duration: '1',
  filesize: '17.9K',
  bit_rate: '146 kb/s',
  genre: 'Sample Genre',
  track: '01',
  album: 'Sample Album',
  date: 2012,
  title: 'Sample Title',
  artist: 'Sample Artist'
}


130
131
132
# File 'lib/pop_cap/audio_file.rb', line 130

def formatted(formatted_hash: FormattedHash)
  @formatted_hash ||= formatted_hash.formatted(unformatted)
end

#raw_output(tag_reader: TagReader) ⇒ Object

Public: raw_tags This method returns the raw_tags from FFmpeg.

Examples

 audio_file = AudioFile.new('spec/fixtures/sample.flac')
 audio_file.raw_tags
 # =>

{
  "format":
    {
      "filename":"spec/fixtures/sample.flac",
      "nb_streams":1,
      "format_name":"flac",
      "format_long_name":"raw FLAC",
      "duration":"1.000000",
      "size":"18291",
      "bit_rate":"146328",
        "tags":
        {
          "GENRE":"Sample Genre",
          "track":"01",
          "ALBUM":"Sample Album",
          "DATE":"2012",
          "TITLE":"Sample Title",
          "ARTIST":"Sample Artist"
        }
    }
}


86
87
88
# File 'lib/pop_cap/audio_file.rb', line 86

def raw_output(tag_reader: TagReader)
  @raw ||= tag_reader.read(filepath)
end

#reload!Object

Public: This method reloads the current instance.

Examples

audio_file.reload!


160
161
162
# File 'lib/pop_cap/audio_file.rb', line 160

def reload!
  ::MEMOIZABLES.each { |var| self.instance_variable_set(var, nil) }
end

#tags(tag_struct: TagStruct) ⇒ Object

Public: This method builds a tag structure from #formatted.

.album             =>  'Sample Album'
.artist            =>  'Sample Artist'
.bit_rate          =>  '146 kb/s'
.date              =>  2012
.duration          =>  '1'
.filename          =>  'spec/fixtures/sample.flac'
.filesize          =>  '17.9K'
.format_long_name  =>  'raw FLAC'
.format_name       =>  'flac'
.genre             =>  'Sample Genre'
.nb_streams        =>  '1'
.start_time        =>  'N/A'
.title             =>  'Sample Title'
.track             =>  '01'


151
152
153
# File 'lib/pop_cap/audio_file.rb', line 151

def tags(tag_struct: TagStruct)
  @tag_struct ||= tag_struct.new(formatted)
end

#unformatted(unformatted_hash: UnformattedHash) ⇒ Object

Public: This method returns a sanitized version of #raw_tags.

{
  filename: '$HOME/spec/fixtures/sample.flac',
  nb_streams: 1,
  format_name: 'flac',
  format_long_name: 'raw FLAC',
  duration: '1.000000',
  filesize: '18291',
  bit_rate: '146328',
  genre: 'Sample Genre',
  track: '01',
  album: 'Sample Album',
  date: '2012',
  title: 'Sample Title',
  artist: 'Sample Artist'
}


108
109
110
# File 'lib/pop_cap/audio_file.rb', line 108

def unformatted(unformatted_hash: UnformattedHash)
  @unformatted_hash ||= unformatted_hash.hash(raw_output)
end

#update(updates = {}) ⇒ Object

Public: update(updates) Updates existing tags, adds a tag if it does not exist.

updates - This takes a hash of tags.

Examples

audio_file.update(artist: 'New Artist', album: 'New Album')


173
174
175
176
177
# File 'lib/pop_cap/audio_file.rb', line 173

def update(updates={})
  TagWriter.write(filepath, updates)
  self.reload!
  self.tags
end