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.
-
#compile(source_directory: '', target_directory: '') ⇒ Object
copy all MP3 directories through the category file-directory stucture.
- #consolidate_txt(target_directory: '') ⇒ Object
- #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
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
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 |
#compile(source_directory: '', target_directory: '') ⇒ Object
copy all MP3 directories through the category file-directory stucture
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/got_mp3.rb', line 48 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/got_mp3.rb', line 74 def consolidate_txt(target_directory: '') raise 'target_directory cannot be empty' if target_directory.empty? find_by_ext('.mp3').each do |directory, _ | puts 'write_titles() - directory: ' + directory.inspect if @debug 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_mp3_file(directory = '.', &blk) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/got_mp3.rb', line 94 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
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/got_mp3.rb', line 114 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. ')}
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/got_mp3.rb', line 138 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
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/got_mp3.rb', line 152 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 |