Class: GotMP3
- Inherits:
-
Object
- Object
- GotMP3
- Defined in:
- lib/got_mp3.rb
Instance Attribute Summary collapse
-
#dir ⇒ Object
Returns the value of attribute dir.
Instance Method Summary collapse
-
#add_jpg ⇒ Object
Adds album art to each MP3 file.
-
#add_playlist(dir: @dir) ⇒ Object
Adds an XSPF playlist file to each file directory containing MP3s.
-
#add_titles ⇒ Object
Adds a track title to each MP3 file.
-
#compile(source_directory: '', target_directory: '') ⇒ Object
Copies all MP3 directories through the category file-directory stucture.
-
#consolidate_txt(target_directory: '') ⇒ Object
copy the .txt file from each mp3 directory into a central file directory.
- #each_media_file(directory = @dir, &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.
-
#sort_by_tracknum! ⇒ Object
sort MP3 or OGG by track number in the filename.
-
#write_titles(format: 'txt') ⇒ Object
write either a track listing into the media filepath, either in plain text or XML format.
Constructor Details
#initialize(dir: '.', debug: false) ⇒ GotMP3
Returns a new instance of GotMP3.
16 17 18 |
# File 'lib/got_mp3.rb', line 16 def initialize(dir: '.', debug: false) @dir, @debug = dir, debug end |
Instance Attribute Details
#dir ⇒ Object
Returns the value of attribute dir.
14 15 16 |
# File 'lib/got_mp3.rb', line 14 def dir @dir end |
Instance Method Details
#add_jpg ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/got_mp3.rb', line 27 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_playlist(dir: @dir) ⇒ Object
Adds an XSPF playlist file to each file directory containing MP3s
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/got_mp3.rb', line 53 def add_playlist(dir: @dir) # .new('/home/james/Music2/Christmas) #mp3s = Dir[File.join(@dir, '*.mp3')].sort_by {|x| File.mtime(x)}.reverse a = Dir.glob(File.join(dir, '*')).select {|f| File.directory? f} tracks = [] a.each do |dir| tracks += add_playlist(dir: dir) each_media_track(dir) do |media, trackno, filepath| info = case File.extname(filepath) when '.mp3' mp3info(media) when '.ogg' ogginfo(media) end h = { title: info[:title], location: File.join(File.basename(dir), File.basename(filepath)), album: info[:album] } tracks << h end end pc = PlaylistCreator.new() tracks.each do |x| pc.add title: x[:title], location: x[:location], album: x[:album] end File.write File.join(dir,'playlist.xspf'), pc.to_xspf return tracks end |
#add_titles ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/got_mp3.rb', line 43 def add_titles() find_by_ext('.txt').each do |directory, txt_filename| add_tracktitles directory, txt_filename end end |
#compile(source_directory: '', target_directory: '') ⇒ Object
Copies all MP3 directories through the category file-directory stucture. To accomplish this, it need a reference (source) directory containing the .txt for each album.
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 |
# File 'lib/got_mp3.rb', line 104 def compile(source_directory: '', target_directory: '') raise 'target_directory cannot be empty' if target_directory.empty? find_by_ext('.txt').each do |directory, _ | Dir[File.join(directory, '*.txt')].each do |txt_filename| album = File.join(source_directory, File.basename(txt_filename).sub(/\.txt$/,'')) library_dir = File.join(target_directory, File.basename(directory)) FileUtils.mkdir_p library_dir if @debug then puts 'copying from:' + album.inspect puts 'copying to: ' + library_dir.inspect end puts 'copying ' + album + ' ...' FileUtils.cp_r album, library_dir, remove_destination: true end end end |
#consolidate_txt(target_directory: '') ⇒ Object
copy the .txt file from each mp3 directory into a central file directory
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/got_mp3.rb', line 132 def consolidate_txt(target_directory: '') raise 'target_directory cannot be empty' if target_directory.empty? find_by_ext('.mp3').each do |directory, _ | txt_filename = Dir[File.join(directory, '*.txt')].first next unless txt_filename target_file = File.basename(directory) FileUtils.cp txt_filename, File.join(target_directory, target_file + '.txt') end end |
#each_media_file(directory = @dir, &blk) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/got_mp3.rb', line 150 def each_media_file(directory=@dir, &blk) puts 'each_media_file - directory: ' + directory.inspect if @debug found = Dir[File.join(directory, "*.{mp3,ogg}")].sort_by { |x| File.mtime(x) } puts 'each_nedia_file - found: ' + found.inspect if @debug found.each.with_index do |media_filepath, i| puts 'each_medi... - media_filepath: ' + media_filepath.inspect if @debug next unless File.exists? media_filepath blk.call(media_filepath, i ) end end |
#go ⇒ Object
Adds the album art, track title, renames the MP3 file, and adds a playlist
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/got_mp3.rb', line 170 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. ')}
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/got_mp3.rb', line 194 def rename() each_media_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 |
#sort_by_tracknum! ⇒ Object
sort MP3 or OGG by track number in the filename
210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/got_mp3.rb', line 210 def sort_by_tracknum!() each_media_file do |filepath| directory = File.dirname(filepath) a = Dir[File.join(directory, '*.{mp3,ogg}')] a.sort_by {|x| x[/^\d+/]}.each {|file| FileUtils.touch file} end end |
#write_titles(format: 'txt') ⇒ Object
write either a track listing into the media filepath, either in plain text or XML format.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/got_mp3.rb', line 225 def write_titles(format: 'txt') puts 'inside write_titles()' if @debug find_by_ext('.{mp3,ogg}').each do |directory, _ | puts 'write_titles() - directory: ' + directory.inspect if @debug txt_filename = Dir[File.join(directory, '*.txt')].first next if txt_filename and format.to_sym == :txt tracks = [] each_media_track(directory) do |media, trackno, media_filepath| info = case File.extname(media_filepath) when '.mp3' mp3info(media) when '.ogg' ogginfo(media) end info[:filename] = File.basename(media_filepath) tracks << OpenStruct.new(info) end puts 'tracks: ' + tracks.inspect if @debug heading = tracks[0].album_artist + ' - ' + tracks[0].album s = "# %s\n\n" % [heading] if format.to_sym == :txt then 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 else # :xml puts 'xml' if @debug dx = DxLite.new('album[album_artist, album]/track(tracknum, title, ' + 'artist, disc, album_artist)') #h.each {|_,x| puts x.inspect } if @debug #h.each {|_,x| x.each {|track| dx.create(track.to_h) } } tracks.each {|track| dx.create(track.to_h) } dx.album_artist = dx.all[0].album_artist dx.album = dx.all[0].album dx.save File.join(directory, 'playlist.dx') yield(directory) if block_given? end end end |