Module: AudioRating::M4A

Defined in:
lib/audio_rating/m4a.rb

Overview

Base class for M4A files.

Class Method Summary collapse

Class Method Details

.get(path) ⇒ Integer, ...

Get an M4A file’s embedded star-rating metadata if present.

Uses taglib-ruby class: rubydoc.info/gems/taglib-ruby/TagLib/MP4/File

Parameters:

  • path (String)

    the M4A file path

Returns:

  • (Integer)

    the star-rating of 1, 2, 3, 4 or 5

  • (Float)

    the star-rating of 0.5, 1.5, 2.5, 3.5 or 4.5

  • (nil)

    if file not found, or file has no star-rating metadata



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/audio_rating/m4a.rb', line 16

def self.get(path)
  TagLib::MP4::File.open(path) do |file|
    next unless file&.valid? && file&.mp4_tag?

    items = file.tag.item_map

    if items.contains 'rate' # MusicBee & MediaMonkey sets this
      rate_value = get_item_value(items.fetch('rate'))
      return RATE_VALUE_TO_STAR_RATING[rate_value]
    end

    itunes_item = get_itunes_rating_item(items)
    get_item_value(itunes_item) if itunes_item
  end
end

.get_item_value(item) ⇒ Integer (private)

Gets the value from an MP4 Item.

Uses taglib-ruby class: rubydoc.info/gems/taglib-ruby/TagLib/MP4/Item

Parameters:

  • item (TagLib::MP4::Item)

    the MP4 item

Returns:

  • (Integer)

    the item value



39
40
41
# File 'lib/audio_rating/m4a.rb', line 39

def self.get_item_value(item)
  item.to_string_list.first.to_i
end

.get_itunes_rating_item(item_map) ⇒ TagLib::MP4::Item? (private)

Gets the iTunes rating MP4 Item from an MP4 ItemMap, if present.

Uses taglib-ruby class: rubydoc.info/gems/taglib-ruby/TagLib/MP4/ItemMap

Parameters:

  • item_map (TagLib::MP4::ItemMap)

    the MP4 ItemMap

Returns:

  • (TagLib::MP4::Item)

    the MP4 Item

  • (nil)

    if a rating item is not present



53
54
55
56
57
58
59
60
61
62
# File 'lib/audio_rating/m4a.rb', line 53

def self.get_itunes_rating_item(item_map)
  key_a = '----:com.apple.iTunes:Rating' # foobar2000 sets this
  return item_map.fetch(key_a) if item_map.contains key_a

  key_b = '----:com.apple.iTunes:rating' # Unsure of source
  return item_map.fetch(key_b) if item_map.contains key_b

  key_c = '----:com.apple.iTunes:RATING' # Unsure of source
  item_map.fetch(key_c) if item_map.contains key_c
end