module MediaOrganizer::Music

Constants

SUPPORTED_FILETYPES

Public Class Methods

availableMetadata(filepath = '', args = {}) click to toggle source
availableMetadata(file, args): returns list of fields available as metadata for the given file.

Inputs

*(1) filepath: full/absolute URI of the file to be analyzed. Required input.
*(2) args: optional arguments passed as hash. Set ":include_null" to false to only return populated metadata fields.
# File lib/scrapers/music.rb, line 66
def self.availableMetadata(filepath = '', args = {})
  attrs = get_music_data(filepath)

  unless args[:include_null] == false
    attrs.each do |field, value|
      attrs.delete(field) if value.nil? || value == ''
    end
  end
  attrs
end
get_music_data(file) click to toggle source
# File lib/scrapers/music.rb, line 11
def self.get_music_data(file)
  attributes = {}
  TagLib::FileRef.open(file) do |fileref|
    unless fileref.null?
      # sign tags to local variables
      tag = fileref.tag
      properties = fileref.audio_properties

      # load tags into attributes attribute
      attributes[:title] = tag.title
      attributes[:track] = tag.track
      attributes[:genre] = tag.genre
      attributes[:year] = tag.year
      attributes[:album] = tag.album
      attributes[:artist] = tag.artist
      attributes[:comment] = tag.comment

      attributes[:length] = properties.length
      attributes[:bitrate] = properties.bitrate
      attributes[:channels] = properties.channels
      attributes[:sample_rate] = properties.sample_rate

    end
  end
  attributes
end
music?(uri) click to toggle source
# File lib/scrapers/music.rb, line 42
def self.music?(uri)
  unless !uri.nil? && uri.is_a?(String) && File.exist?(uri)
    raise StandardError, "Directory given (#{uri}) could not be accessed."
  end

  if SUPPORTED_FILETYPES.include?(File.extname(uri).downcase)
    return true
  else
    return false
  end

rescue FileNotFoundError => e
  puts e.message
  puts e.backtrace.inspect
  return false
end
supported_filetypes() click to toggle source
# File lib/scrapers/music.rb, line 38
def self.supported_filetypes
  reutrn SUPPORTED_FILETYPES
end
writeMetadata(filepath, meta = {}) click to toggle source
writeMetadata(file = "", meta = {}): returns list of fields available as metadata for the given file.

Inputs

*(1) filepath: full/absolute URI of the file to be analyzed. Required input.
*(2) meta: metadata to be written, passed as a hash in the format :metadata_field => metadata_value

Outputs

Returns true if the file was successfully saved. Note: true status does not necessarily indicate each field was successfully written.

Examples

Music.writeMetadata("/absolute/path/to/file.mp3", {:artist => "NewArtistName", :year => "2019"})
# File lib/scrapers/music.rb, line 90
def self.writeMetadata(filepath, meta = {})
  attributes = {}
  successflag = false
  TagLib::FileRef.open(filepath) do |fileref|
    unless fileref.null?
      # sign tags to local variables
      tag = fileref.tag
      properties = fileref.audio_properties

      meta.each do |field, value|
        if tag.respond_to?(field)
          tag.send("#{field}=", value)
        elsif properties.respond_to?(field)
          properties.send("#{field}=", value)
        end
      end
      successflag = fileref.save
    end
  end
  successflag
end