got_mp3 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba5c71c2a3dd150c8be5e6f1d370c6437145e8b6dd95f139647f67ddba469a1f
4
- data.tar.gz: c267589160c6a9ba438b997ccec0365c6c069766947fdf5bc802a9f8f1687e9d
3
+ metadata.gz: 2ec37a296a75bda0bea9d2ce9b7b40534fa6425e7cddfc29d1002cd067f5cddb
4
+ data.tar.gz: de3b11e29c23e10f1a920f96e82e40c2073de1ff1d25b32761f2e92cdc2dafd3
5
5
  SHA512:
6
- metadata.gz: 988db3e77e2d5695efaf0a47f27f84f5c34c774dd49d81ee63ff095221b1cf641e9f827f79b22c84e88c8c2734c02ef8add3314a2d0da87a25f6ebf989e76763
7
- data.tar.gz: eb1e2afa7793029dc227c55476138bc6aa02664ddf92a57afabe2b3f6d24d219a55e0609905a374d2b762fa8469578f44a9c3a13ba11d50339139d6280721102
6
+ metadata.gz: 6cc885998b65f09146163ef5d68ed3c261a3046ecea177b4b61d2ae64827784a9bf7924bd0d14f6209ede86631e693a31d23aaebcd938b2010bff68f5bc14901
7
+ data.tar.gz: b92b911b2d1f4696a21df1800b4c9047e8ca6e03f8cbac8df6a066c9f399b3ba8a4ffe87870b151d2d09018d3cb72bc625a86bfae75b5464bf3974f0d1da2150
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/got_mp3.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+
2
3
  # file: got_mp3.rb
3
4
 
5
+ require 'ostruct'
4
6
  require "mp3info"
5
7
 
6
8
 
@@ -11,32 +13,155 @@ class GotMP3
11
13
  end
12
14
 
13
15
 
16
+ # Adds album art to each MP3 file
17
+ #
14
18
  # Example usage:
15
19
  # gotmp3 = GotMP3.new(dir: '/tmp/tree/Da Fustra - Over The Waves To Shetland')
16
20
  # gotmp3.add_jpg
17
- #
21
+ #
18
22
  def add_jpg()
19
23
 
20
- a = Dir[File.join(@dir, "**", "*.jpg")]
21
- puts 'a: ' + a.inspect if @debug
22
- directories = a.inject({}) {|r, x| r[File.dirname(x)] = File.basename(x); r }
24
+ find_by_ext('.jpg').each do |directory, img_filename|
23
25
 
24
- directories.each do |directory, img_filename|
26
+ puts 'add_jpg to directory: ' + directory.inspect if @debug
25
27
  add_image directory, img_filename
26
28
  end
27
29
 
28
30
  end
29
31
 
32
+ # Adds a track title to each MP3 file
33
+ #
34
+ # Example usage:
35
+ # gotmp3 = GotMP3.new(dir: '/tmp/tree/Da Fustra - Over The Waves To Shetland')
36
+ # gotmp3.add_titles
37
+ #
38
+ def add_titles()
39
+
40
+ find_by_ext('.txt').each do |directory, txt_filename|
41
+ add_tracktitles directory, txt_filename
42
+ end
43
+
44
+ end
45
+
46
+ def each_mp3_file(directory='.', &blk)
47
+
48
+ puts 'each_mp3 - directory: ' + directory.inspect if @debug
49
+ found = Dir[File.join(directory, "*.mp3")].sort_by { |x| File.mtime(x) }
50
+ puts 'each_mp3 - found: ' + found.inspect if @debug
51
+
52
+ found.reverse.each.with_index do |mp3_filepath, i|
53
+
54
+ puts 'each_mp3 - mp3_filepath: ' + mp3_filepath.inspect if @debug
55
+
56
+ next unless File.exists? mp3_filepath
57
+
58
+ blk.call(mp3_filepath, i )
59
+
60
+ end
61
+
62
+ end
63
+
64
+ # Adds the album art, track title, renames the MP3 file, and adds a playlist
65
+ #
66
+ def go()
67
+
68
+ find_by_ext('.mp3').each do |directory, _ |
69
+
70
+ # find the image file
71
+ img_filename = Dir[File.join(directory, '*.jpg')].first
72
+ puts 'img_filename: ' + img_filename.inspect if @debug
73
+
74
+ # find the text file
75
+ txt_filename = Dir[File.join(directory, '*.txt')].first
76
+ next unless txt_filename
77
+
78
+ add_image_and_titles(directory, img_filename, txt_filename)
79
+
80
+ end
81
+
82
+ end
83
+
84
+ # rename 1 or more mp3 files within 1 or more file directories
85
+ #
86
+ # example usage:
87
+ # rename() {|mp3file| mp3files.sub(/Disc \d - /,'')}
88
+ # rename() {|mp3file| mp3file.sub(/Disc \d - (\d+) - /,'\1. ')}
89
+ #
90
+ def rename()
91
+
92
+ each_mp3_file do |mp3_filepath|
93
+
94
+ mp3_directory = File.dirname(mp3_filepath)
95
+ mp3_filename = File.basename(mp3_filepath)
96
+
97
+ newname = yield(mp3_filename)
98
+ File.rename(mp3_filepath, File.join(mp3_directory, newname))
99
+
100
+ end
101
+
102
+ end
103
+
104
+ def write_titles()
105
+
106
+ puts 'inside write_titles()' if @debug
107
+
108
+ find_by_ext('.mp3').each do |directory, _ |
109
+
110
+ puts 'write_titles() - directory: ' + directory.inspect if @debug
111
+ txt_filename = Dir[File.join(directory, '*.txt')].first
112
+
113
+ next if txt_filename
114
+
115
+ tracks = []
116
+
117
+ each_mp3_track(directory) do |mp3, trackno, mp3_filepath|
118
+
119
+ tracks << OpenStruct.new({
120
+ title: mp3.tag.title,
121
+ artist: mp3.tag.artist,
122
+ album: mp3.tag.album,
123
+ album_artist: mp3.tag2['TPE2'],
124
+ disc: mp3.tag2['TPOS'],
125
+ tracknum: mp3.tag.tracknum,
126
+ filename: File.basename(mp3_filepath)
127
+ })
128
+
129
+ end
130
+
131
+ heading = tracks[0].album_artist + ' - ' + tracks[0].album
132
+ s = "# %s\n\n" % [heading]
133
+ h = tracks.group_by(&:disc)
134
+
135
+ body = if h.length == 1 then
136
+
137
+ list(tracks)
138
+
139
+ else
140
+
141
+ "\n" + h.map do |disc, tracks2|
142
+ ("## Disc %d\n\n" % disc) + list(tracks2) + "\n\n"
143
+ end.join("\n")
144
+
145
+ end
146
+
147
+ File.write File.join(directory, heading + '.txt'), s + body
148
+
149
+ end
150
+
151
+ end
152
+
30
153
  private
31
154
 
32
155
  # adds album art to each mp3 file in a file directory
33
156
  #
34
157
  def add_image(directory, img_filename)
35
158
 
36
- image_file = File.new(File.join(directory, img_filename),'rb')
159
+ puts 'inside add_image - directory: ' + directory.inspect if @debug
160
+ puts 'img_filename: ' + img_filename.inspect if @debug
161
+ image_file = File.new(File.join(directory, img_filename),'rb')
37
162
  img = image_file.read
38
163
 
39
- each_mp3(directory) do |mp3|
164
+ each_mp3_track(directory) do |mp3, _, _|
40
165
 
41
166
  mp3.tag2.remove_pictures
42
167
  mp3.tag2.add_picture img
@@ -45,16 +170,125 @@ class GotMP3
45
170
 
46
171
  end
47
172
 
48
- def each_mp3(directory, &blk)
173
+ def add_tracktitles(directory, txt_filename)
174
+
175
+ txt_file = File.join(directory, txt_filename)
176
+ track_titles = File.read(txt_file).lines[1..-1].map(&:strip)
177
+
178
+ each_mp3_track(directory) do |mp3, trackno, _|
179
+
180
+ mp3.tag.title = track_titles[trackno-1]
181
+
182
+ end
183
+ end
184
+
185
+ def add_image_and_titles(directory, img_file, txt_file)
186
+
187
+ if img_file then
188
+ img = File.new(img_file,'rb').read
189
+ end
190
+
191
+ track_titles = File.read(txt_file).lines[1..-1].map(&:strip)
192
+
193
+ titles_mp3 = track_titles.map do |title|
194
+ title[/^[^\/]+/].gsub(/:/,'_').rstrip + '.mp3'
195
+ end
196
+
197
+ found = Dir[File.join(directory, "*.mp3")].sort_by { |x| File.mtime(x) }
198
+ found.each.with_index do |mp3_filepath, i|
49
199
 
50
- Dir[File.join(directory, "*.mp3")].each do |mp3_filepath|
200
+ Mp3Info.open(mp3_filepath) do |mp3|
51
201
 
52
- Mp3Info.open mp3_filepath do |mp3|
53
- blk.call mp3
202
+ if img_file then
203
+ mp3.tag2.remove_pictures
204
+ mp3.tag2.add_picture img
205
+ end
206
+
207
+ mp3.tag.title = track_titles[i]
54
208
  end
55
209
 
210
+ File.rename(mp3_filepath, File.join(directory, titles_mp3[i]))
211
+
56
212
  end
57
213
 
214
+ File.write File.join(directory, 'playlist.m3u'), titles_mp3.join("\n")
215
+
216
+ end
217
+
218
+ def each_mp3_track(directory, &blk)
219
+
220
+ each_mp3_file(directory) do |mp3_filepath, i|
221
+
222
+ Mp3Info.open(mp3_filepath) {|mp3| blk.call(mp3, i+1, mp3_filepath) }
223
+
224
+ end
225
+
226
+ end
227
+
228
+ def find_by_ext(extension)
229
+
230
+ puts 'find_by_ext() - @dir' + @dir.inspect if @debug
231
+ a = Dir[File.join(@dir, "**", "*" + extension)]
232
+ puts 'a: ' + a.inspect if @debug
233
+
234
+ a.inject({}) do |r, x|
235
+
236
+ r[File.dirname(x)] = File.basename(x)
237
+ r
238
+
239
+ end
240
+ end
241
+
242
+ def list(tracks)
243
+
244
+ a = if tracks.map(&:artist).uniq.length < 2 then
245
+ tracks.map {|x| "%02d. %s" % [x.tracknum, x.title] }
246
+ else
247
+ tracks.map {|x| "%02d. %s - %s" % [x.tracknum, x.title, x.artist] }
248
+ end
249
+
250
+ a.join("\n")
251
+
252
+ end
253
+
254
+ end
255
+
256
+ class Titles
257
+
258
+ def initialize(filename, target_directory: 'titles')
259
+ @titles = File.read filename
260
+ @target_directory = target_directory
261
+ end
262
+
263
+ def titleize()
264
+ @titles.gsub(/[\w']+/) {|x| x[0].upcase + x[1..-1]}
265
+ end
266
+
267
+ def titleize!()
268
+ @titles.gsub!(/[\w']+/) {|x| x[0].upcase + x[1..-1]}
269
+ end
270
+
271
+ def split(target_directory: @target_directory)
272
+
273
+ FileUtils.mkdir_p @target_directory
274
+ a = @titles.strip.split(/(?=^#)/)
275
+
276
+ a.each do |x|
277
+
278
+ filename = x.lines.first.chomp[/(?<=# cd ).*/i].strip + '.txt'
279
+ puts 'processing file ' + filename.inspect
280
+ heading = x.lstrip.lines.first
281
+
282
+ tracks = x.strip.lines[1..-1].map.with_index do |line, i|
283
+ "%02d. %s" % [i+1, line]
284
+ end
285
+
286
+ File.write File.join(target_directory, filename), heading + tracks.join
287
+
288
+ end
289
+
290
+ puts 'split done'
291
+
58
292
  end
59
293
 
60
294
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: got_mp3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  RFrZsrDO1Cwk4v03qCUvuZ8H/nCojYYJGMrJE1JhBBJNJXCAULivh/zlOYcDhG69
36
36
  uPtZOD7YKrG45OftX8aNxlNv
37
37
  -----END CERTIFICATE-----
38
- date: 2021-11-24 00:00:00.000000000 Z
38
+ date: 2021-11-26 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: ruby-mp3info
@@ -87,6 +87,6 @@ rubyforge_project:
87
87
  rubygems_version: 2.7.10
88
88
  signing_key:
89
89
  specification_version: 4
90
- summary: A ruby-mp3info wrapper to make it convenient to update the metadata of multiple
91
- MP3 files at once.
90
+ summary: A ruby-mp3info wrapper to make it convenient to update the album art and
91
+ track titles of multiple MP3 files in a batch process.
92
92
  test_files: []
metadata.gz.sig CHANGED
Binary file