got_mp3 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 03c18932de3d7bcf55555f98c7b4fc677ac64b2262e63330c3629c4bca334d60
4
- data.tar.gz: 595b8aaaa06da6d1f61fefa3605f8ea446952c26e0022f1607d07ad2d2d494e2
3
+ metadata.gz: 94d8fc924be742f45265ce5ced2836859ad920e2813294107861e9530ce00ab0
4
+ data.tar.gz: c60a80063c720c5a97a6cd0dc994688e9c6227c462c4bd857dc51307af2143c4
5
5
  SHA512:
6
- metadata.gz: 29561bc4eacd3a9a17149260c2bb4a1cf504894b09468e8eac7e99ce5a5f94cacf87bbdcd7b12d7017d5e19a157457894c4cb72b31f7cbce778a1e5276dff55f
7
- data.tar.gz: 745f2418ed072cb0b22e74934d996e62daca4b726f90bed3ed7af2d8b528c6bdc3524019b49291b187b039e73cbe4014b5c509f1b9175a7ceba4e0b21bcd3ec2
6
+ metadata.gz: dc555a1313236f60e25892afbee007b6a2953b35c7a7c36ceeb0acf36865315b181a70d748c243b3c7385e2d5bb547a36742884465877bfc3796bd16a11b00af
7
+ data.tar.gz: e07d01081c5002073e8f147559cc19022ce326d438698808d004eaff62bd35c1095139e3ccd0a91971caeade73993b6f5505ed967561458e3d660fed2edc0f86
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/got_mp3.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  # file: got_mp3.rb
4
4
 
5
+ require 'ostruct'
5
6
  require "mp3info"
6
7
 
7
8
 
@@ -40,7 +41,18 @@ class GotMP3
40
41
 
41
42
  end
42
43
 
43
- # Adds the album art, track title, and renames the MP3 file
44
+ def each_mp3_file(directory='.', &blk)
45
+
46
+ found = Dir[File.join(directory, "*.mp3")].sort_by { |x| File.mtime(x) }
47
+ found.each.with_index do |mp3_filepath, i|
48
+
49
+ blk.call(mp3_filepath, i )
50
+
51
+ end
52
+
53
+ end
54
+
55
+ # Adds the album art, track title, renames the MP3 file, and adds a playlist
44
56
  #
45
57
  def go()
46
58
 
@@ -59,16 +71,79 @@ class GotMP3
59
71
 
60
72
  end
61
73
 
74
+ # rename 1 or more mp3 files within 1 or more file directories
75
+ #
76
+ # example usage:
77
+ # rename() {|mp3file| mp3files.sub(/Disc \d - /,'')}
78
+ # rename() {|mp3file| mp3file.sub(/Disc \d - (\d+) - /,'\1. ')}
79
+ #
80
+ def rename()
81
+
82
+ each_mp3_file do |mp3_filepath|
83
+
84
+ mp3_directory = File.dirname(mp3_filepath)
85
+ mp3_filename = File.basename(mp3_filepath)
86
+
87
+ newname = yield(mp3_filename)
88
+ File.rename(mp3_filepath, File.join(mp3_directory, newname))
89
+
90
+ end
91
+
92
+ end
93
+
94
+ def write_titles()
95
+
96
+ find_by_ext('.mp3').each do |directory, _ |
97
+
98
+ tracks = []
99
+
100
+ each_mp3_track(directory) do |mp3, trackno, mp3_filepath|
101
+
102
+ tracks << OpenStruct.new({
103
+ title: mp3.tag.title,
104
+ artist: mp3.tag.artist,
105
+ album: mp3.tag.album,
106
+ album_artist: mp3.tag2['TPE2'],
107
+ disc: mp3.tag2['TPOS'],
108
+ tracknum: mp3.tag.tracknum,
109
+ filename: File.basename(mp3_filepath)
110
+ })
111
+
112
+ end
113
+
114
+ heading = tracks[0].album_artist + ' - ' + tracks[0].album
115
+ s = "# %s\n\n" % [heading]
116
+ h = tracks.group_by(&:disc)
117
+
118
+ body = if h.length == 1 then
119
+
120
+ list(tracks)
121
+
122
+ else
123
+
124
+ "\n" + h.map do |disc, tracks2|
125
+ ("## Disc %d\n\n" % disc) + list(tracks2) + "\n\n"
126
+ end.join("\n")
127
+
128
+ end
129
+
130
+ File.write File.join(directory, heading + '.txt'), s + body
131
+
132
+ end
133
+
134
+ end
135
+
62
136
  private
63
137
 
64
138
  # adds album art to each mp3 file in a file directory
65
139
  #
66
140
  def add_image(directory, img_filename)
67
141
 
142
+ puts 'img_filename: ' + img_filename.inspect if @debug
68
143
  image_file = File.new(File.join(directory, img_filename),'rb')
69
144
  img = image_file.read
70
145
 
71
- each_mp3(directory) do |mp3, _, _|
146
+ each_mp3_track(directory) do |mp3, _, _|
72
147
 
73
148
  mp3.tag2.remove_pictures
74
149
  mp3.tag2.add_picture img
@@ -82,7 +157,7 @@ class GotMP3
82
157
  txt_file = File.join(directory, txt_filename)
83
158
  track_titles = File.read(txt_file).lines[1..-1].map(&:strip)
84
159
 
85
- each_mp3(directory) do |mp3, trackno, _|
160
+ each_mp3_track(directory) do |mp3, trackno, _|
86
161
 
87
162
  mp3.tag.title = track_titles[trackno-1]
88
163
 
@@ -97,6 +172,10 @@ class GotMP3
97
172
 
98
173
  track_titles = File.read(txt_file).lines[1..-1].map(&:strip)
99
174
 
175
+ titles_mp3 = track_titles.map do |title|
176
+ title[/^[^\/]+/].gsub(/:/,'_').rstrip + '.mp3'
177
+ end
178
+
100
179
  found = Dir[File.join(directory, "*.mp3")].sort_by { |x| File.mtime(x) }
101
180
  found.each.with_index do |mp3_filepath, i|
102
181
 
@@ -110,16 +189,17 @@ class GotMP3
110
189
  mp3.tag.title = track_titles[i]
111
190
  end
112
191
 
113
- File.rename(mp3_filepath, File.join(directory,
114
- track_titles[i][/^[^\/]+/].gsub(/:/,'_').rstrip + '.mp3'))
192
+ File.rename(mp3_filepath, File.join(directory, titles_mp3[i]))
193
+
115
194
  end
116
195
 
196
+ File.write File.join(directory, 'playlist.m3u'), titles_mp3.join("\n")
197
+
117
198
  end
118
199
 
119
- def each_mp3(directory, &blk)
200
+ def each_mp3_track(directory, &blk)
120
201
 
121
- found = Dir[File.join(directory, "*.mp3")].sort_by { |x| File.mtime(x) }
122
- found.each.with_index do |mp3_filepath, i|
202
+ each_mp3_file do |mp3_filepath, i|
123
203
 
124
204
  Mp3Info.open(mp3_filepath) {|mp3| blk.call(mp3, i+1, mp3_filepath) }
125
205
 
@@ -140,4 +220,56 @@ class GotMP3
140
220
  end
141
221
  end
142
222
 
223
+ def list(tracks)
224
+
225
+ a = if tracks.map(&:artist).uniq.length < 2 then
226
+ tracks.map {|x| "%02d. %s" % [x.tracknum, x.title] }
227
+ else
228
+ tracks.map {|x| "%02d. %s - %s" % [x.tracknum, x.title, x.artist] }
229
+ end
230
+
231
+ a.join("\n")
232
+
233
+ end
234
+
235
+ end
236
+
237
+ class Titles
238
+
239
+ def initialize(filename, target_directory: 'titles')
240
+ @titles = File.read filename
241
+ @target_directory = target_directory
242
+ end
243
+
244
+ def titleize()
245
+ @titles.gsub(/[\w']+/) {|x| x[0].upcase + x[1..-1]}
246
+ end
247
+
248
+ def titleize!()
249
+ @titles.gsub!(/[\w']+/) {|x| x[0].upcase + x[1..-1]}
250
+ end
251
+
252
+ def split(target_directory: @target_directory)
253
+
254
+ FileUtils.mkdir_p @target_directory
255
+ a = @titles.strip.split(/(?=^#)/)
256
+
257
+ a.each do |x|
258
+
259
+ filename = x.lines.first.chomp[/(?<=# cd ).*/i].strip + '.txt'
260
+ puts 'processing file ' + filename.inspect
261
+ heading = x.lstrip.lines.first
262
+
263
+ tracks = x.strip.lines[1..-1].map.with_index do |line, i|
264
+ "%02d. %s" % [i+1, line]
265
+ end
266
+
267
+ File.write File.join(target_directory, filename), heading + tracks.join
268
+
269
+ end
270
+
271
+ puts 'split done'
272
+
273
+ end
274
+
143
275
  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.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -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 as a batch process.
92
92
  test_files: []
metadata.gz.sig CHANGED
@@ -1,2 +1,2 @@
1
- F2���)x70�#SO�Ze��}#�\i��a| ��*�*`.-�Jo'�%��ގ ֕�f��`ZU1��5��������9\��Z�) ^sYJ�(�|���� AX9���B|��kVWic�^�Q!����.=�X��O��GCf�"xx��S`�L2�s�Y�����l6 vձ{z0MZ*�:��3i�K w� ��7��
2
- ��%�F;܁�g��_�� ��t d�-�x�\
1
+ ���9I�D@�!`W�S��,���a�7���I�`/�ZaT7���Z})'RF�ؕ!Μ��P�㩧��|mA�ݡ� ��t�?B|o,� =� GS��/��(�5Qq5aA5@`�ɰ��]ʹ�$�Z1RT Bγ��
2
+ ���g�� ^�����w#D>~4!�0� L���Ψ�"rW�*���Б���fL� O ���k��̄��Ε