Class: IO

Inherits:
Object
  • Object
show all
Defined in:
lib/id3/io_extensions.rb

Overview

if you have a (partial) MP3-file stored in a File or IO object, you can check if it contains ID3 tags NOTE: file needs to be opened in binary mode! 'rb:binary'

Instance Method Summary collapse

Instance Method Details

#hasID3tag?Boolean

Returns:

  • (Boolean)

13
14
15
# File 'lib/id3/io_extensions.rb', line 13

def hasID3tag?
  hasID3v2tag? || hasID3v1tag? ? true : false         # returns true or false
end

#hasID3v1tag?Boolean

Returns:

  • (Boolean)

17
18
19
20
21
22
23
24
25
# File 'lib/id3/io_extensions.rb', line 17

def hasID3v1tag?
  seek(-ID3::ID3v1tagSize, IO::SEEK_END)
  if (read(3) == 'TAG')
    seek(-ID3::ID3v1tagSize + ID3::ID3v1versionbyte, IO::SEEK_END)
    return get_byte == 0 ? "1.0" : "1.1"
  else
    return nil
  end
end

#hasID3v2tag?Boolean

Returns:

  • (Boolean)

34
35
36
37
38
39
40
41
42
43
# File 'lib/id3/io_extensions.rb', line 34

def hasID3v2tag?
  rewind
  if (read(3) == "ID3")
    major = get_byte
    minor = get_byte
    return version   = "2." + major.to_s + '.' + minor.to_s
  else
    return nil
  end
end

#id3_versionsObject


9
10
11
# File 'lib/id3/io_extensions.rb', line 9

def id3_versions
  [ hasID3v1tag? ,hasID3v2tag? ].compact  # returns an Array of version numbers
end

#ID3v2_tag_sizeObject


27
28
29
30
31
32
# File 'lib/id3/io_extensions.rb', line 27

def ID3v2_tag_size
  rewind
  return 0 if (read(3) != 'ID3')
  read_bytes(3)  # skip version and flags
  return ID3::ID3v2headerSize + ID3.unmungeSize( read_bytes(4) )
end