-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp3.rb
69 lines (56 loc) · 1.84 KB
/
mp3.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
require "mp3info"
class Mp3
attr_reader :filepath, :artist, :album, :title, :tracknum, :info
def initialize(filepath)
@filepath = filepath
begin
@info = {}
Mp3Info.open(filepath) do |mp3|
# puts "path: #{filepath}"
# puts "Artist: #{mp3.tag.artist}"
# puts "Title: #{mp3.tag.title}"
# puts "Album: #{mp3.tag.album}"
# puts "Track Num: #{mp3.tag.tracknum}"
# puts "TAG 2 INFO"
%w(TIT1 TIT2 TIT3 TALB TOAL TOPE WOAR TPE1 TPE2).each do |key|
#puts "#{key} : #{mp3.tag2[key]}"
@info[key] = mp3.tag2[key]
end
@artist = (extract_artist(mp3) || "Unknown Artist").sanitize
@title = mp3.tag.title.present? ? mp3.tag.title.sanitize.strip[0...36] : "Unknown Title"
@album = mp3.tag.album.present? ? mp3.tag.album.sanitize.strip[0...40].strip.sanitize : "Unknown Album"
@tracknum = if !mp3.tag.tracknum.nil?
mp3.tag.tracknum < 10 ? "0#{mp3.tag.tracknum}" : mp3.tag.tracknum
end
end
rescue StandardError => e
puts "ERROR while processing: #{filepath}"
raise e
end
end
def filepath(basepath=nil)
tracknum_str = @tracknum.nil? ? "" : "#{@tracknum} "
path = "#{@artist}/#{@album}/#{tracknum_str}#{@title}.mp3"
return (basepath.blank? ? path : "#{basepath}/#{path}" )
end
def inspect
puts "Path: #{@filepath}"
puts "Artist: #{@artist}"
puts "Album: #{@album}"
puts "Title: #{@title}"
puts "Track No.: #{@tracknum}"
end
private
def purify()
end
def extract_artist(mp3)
tpe2 = mp3.tag2["TPE2"]
tpe2 = tpe2.first if tpe2.is_a?(Array)
return tpe2 if tpe2.present?
tpe1 = mp3.tag2["TPE1"]
tpe1 = tpe1.first if tpe1.is_a?(Array)
return tpe1 if tpe1.present?
return mp3.tag.artist if mp3.tag.artist.present?
return nil
end
end