Module: Id3Tags

Defined in:
lib/id3_tags.rb,
lib/id3_tags/m4a.rb,
lib/id3_tags/mp3.rb,
lib/id3_tags/version.rb,
lib/id3_tags/m4a/fields.rb,
lib/id3_tags/mp3/fields.rb,
lib/id3_tags/fields_accessors.rb,
lib/id3_tags/m4a/fields_getter.rb,
lib/id3_tags/m4a/fields_setter.rb,
lib/id3_tags/mp3/fields_getter.rb,
lib/id3_tags/mp3/fields_setter.rb

Overview

Provides two methods to read and write ID3 metadata from an MP3 or M4A file

Defined Under Namespace

Modules: FieldsAccessors, M4AFields, M4AFieldsGetter, M4AFieldsSetter, MP3Fields, MP3FieldsReader, MP3FieldsWriter Classes: M4A, MPEG

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.mime_type_of(file_path) ⇒ String

Returns the MIME type of the file located at file_path.

Examples:

Return the MIME type of an M4A file with a wrong ‘.mp3’ extension

Id3Tags.mime_type_of("this_is_an_m4a.mp3")

# => "audio/mpeg"

Parameters:

  • file_path (String)

    Local path to a file

Returns:

  • (String)

    the MIME type of the file



66
67
68
69
70
# File 'lib/id3_tags.rb', line 66

def self.mime_type_of(file_path)
  mime_type   = MimeMagic.by_magic File.open(file_path)
  mime_type ||= MimeMagic.by_path file_path
  mime_type &&= mime_type.type
end

.read_tags_from(file_path) ⇒ Hash

Returns a Hash of ID3 attributes stored in the file located at file_path.

Examples:

Read tags from an MP3 full of metadata

Id3Tags.read_tags_from("all_id3.mp3")

# => {title: "Sample track", album: "Sample album", artist: Sample
      artist", :comment=>"Sample comments", genre: "Sample Genre",
      year: 1979, bitrate: 128, channels: 2, length: 38, samplerate:
      44100, bpm: 110, lyrics: "Sample lyrics line 1\rand line 2",
      composer: "Sample composer", grouping: "Sample group",
      album_artist: "Sample album artist", compilation: true, track:
      {number: 3, count: 12}, disk: {number: 1, count: 2}, cover_art:
      {mime_type: "image/png", data: "\x89PNG\r\n\x1A[...]"}}

Parameters:

  • file_path (String)

    Local path to an MP3 or M4A file

Returns:

  • (Hash)

    the ID3 attributes stored in the file



24
25
26
27
28
29
30
# File 'lib/id3_tags.rb', line 24

def self.read_tags_from(file_path)
  case mime_type_of(file_path)
    when 'audio/mpeg' then MPEG.new.read_tags_from(file_path)
    when 'audio/mp4' then M4A.new.read_tags_from(file_path)
    else {}
  end
end

.write_tags_to(file_path, attrs = {}) ⇒ Boolean

Stores the attrs Hash of ID3 attributes into the file at file_path.

Examples:

Write ID3 tags to an MP3 file

Id3Tags.write_tags_to("no_id3.mp3", {title: "Sample track", album:
     "Sample album", artist: Sample artist", :comment=>"Sample comments",
     genre: "Sample Genre", year: 1979, bitrate: 128, channels: 2,
     length: 38, samplerate: 44100, bpm: 110, lyrics: "Sample lyrics
     line 1\rand line 2", composer: "Sample composer", grouping: "Sample
     group", album_artist: "Sample album artist", compilation: true,
     track: {number: 3, count: 12}, disk: {number: 1, count: 2},
     cover_art: {mime_type: "image/png", data: "\x89PNG\r\n\x1A[...]"}}

# => true

Parameters:

  • file_path (String)

    Local path to an MP3 or M4A file

  • attrs (Hash) (defaults to: {})

    the ID3 attributes stored in the file

Returns:

  • (Boolean)

    true (the file gets changed)



49
50
51
52
53
54
55
# File 'lib/id3_tags.rb', line 49

def self.write_tags_to(file_path, attrs = {})
  case mime_type_of(file_path)
    when 'audio/mpeg' then MPEG.new.write_tags_to(file_path, attrs)
    when 'audio/mp4' then M4A.new.write_tags_to(file_path, attrs)
    else false
  end
end