Class: GotMP3
- Inherits:
-
Object
- Object
- GotMP3
- Defined in:
- lib/got_mp3.rb
Instance Method Summary collapse
-
#add_jpg ⇒ Object
Adds album art to each MP3 file.
-
#add_titles ⇒ Object
Adds a track title to each MP3 file.
- #each_mp3_file(directory = '.', &blk) ⇒ Object
-
#go ⇒ Object
Adds the album art, track title, renames the MP3 file, and adds a playlist.
-
#initialize(dir: '.', debug: false) ⇒ GotMP3
constructor
A new instance of GotMP3.
-
#rename ⇒ Object
rename 1 or more mp3 files within 1 or more file directories.
- #write_titles ⇒ Object
Constructor Details
#initialize(dir: '.', debug: false) ⇒ GotMP3
Returns a new instance of GotMP3.
11 12 13 |
# File 'lib/got_mp3.rb', line 11 def initialize(dir: '.', debug: false) @dir, @debug = dir, debug end |
Instance Method Details
#add_jpg ⇒ Object
Adds album art to each MP3 file
Example usage:
gotmp3 = GotMP3.new(dir: '/tmp/tree/Da Fustra - Over The Waves To Shetland')
gotmp3.add_jpg
22 23 24 25 26 27 28 29 30 |
# File 'lib/got_mp3.rb', line 22 def add_jpg() find_by_ext('.jpg').each do |directory, img_filename| puts 'add_jpg to directory: ' + directory.inspect if @debug add_image directory, img_filename end end |
#add_titles ⇒ Object
Adds a track title to each MP3 file
Example usage:
gotmp3 = GotMP3.new(dir: '/tmp/tree/Da Fustra - Over The Waves To Shetland')
gotmp3.add_titles
38 39 40 41 42 43 44 |
# File 'lib/got_mp3.rb', line 38 def add_titles() find_by_ext('.txt').each do |directory, txt_filename| add_tracktitles directory, txt_filename end end |
#each_mp3_file(directory = '.', &blk) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/got_mp3.rb', line 46 def each_mp3_file(directory='.', &blk) puts 'each_mp3 - directory: ' + directory.inspect if @debug found = Dir[File.join(directory, "*.mp3")].sort_by { |x| File.mtime(x) } puts 'each_mp3 - found: ' + found.inspect if @debug found.reverse.each.with_index do |mp3_filepath, i| puts 'each_mp3 - mp3_filepath: ' + mp3_filepath.inspect if @debug next unless File.exists? mp3_filepath blk.call(mp3_filepath, i ) end end |
#go ⇒ Object
Adds the album art, track title, renames the MP3 file, and adds a playlist
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/got_mp3.rb', line 66 def go() find_by_ext('.mp3').each do |directory, _ | # find the image file img_filename = Dir[File.join(directory, '*.jpg')].first puts 'img_filename: ' + img_filename.inspect if @debug # find the text file txt_filename = Dir[File.join(directory, '*.txt')].first next unless txt_filename add_image_and_titles(directory, img_filename, txt_filename) end end |
#rename ⇒ Object
rename 1 or more mp3 files within 1 or more file directories
example usage:
rename() {|mp3file| mp3files.sub(/Disc \d - /,'')}
rename() {|mp3file| mp3file.sub(/Disc \d - (\d+) - /,'\1. ')}
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/got_mp3.rb', line 90 def rename() each_mp3_file do |mp3_filepath| mp3_directory = File.dirname(mp3_filepath) mp3_filename = File.basename(mp3_filepath) newname = yield(mp3_filename) File.rename(mp3_filepath, File.join(mp3_directory, newname)) end end |
#write_titles ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/got_mp3.rb', line 104 def write_titles() puts 'inside write_titles()' if @debug find_by_ext('.mp3').each do |directory, _ | puts 'write_titles() - directory: ' + directory.inspect if @debug txt_filename = Dir[File.join(directory, '*.txt')].first next if txt_filename tracks = [] each_mp3_track(directory) do |mp3, trackno, mp3_filepath| tracks << OpenStruct.new({ title: mp3.tag.title, artist: mp3.tag.artist, album: mp3.tag.album, album_artist: mp3.tag2['TPE2'], disc: mp3.tag2['TPOS'], tracknum: mp3.tag.tracknum, filename: File.basename(mp3_filepath) }) end heading = tracks[0].album_artist + ' - ' + tracks[0].album s = "# %s\n\n" % [heading] h = tracks.group_by(&:disc) body = if h.length == 1 then list(tracks) else "\n" + h.map do |disc, tracks2| ("## Disc %d\n\n" % disc) + list(tracks2) + "\n\n" end.join("\n") end File.write File.join(directory, heading + '.txt'), s + body end end |