-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdowncode
executable file
·309 lines (238 loc) · 6.26 KB
/
downcode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
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
99
100
101
102
103
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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
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
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env ruby
#encoding: utf-8
#
# Copyright 2011-4 James Pharaoh <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "rubygems"
require "fileutils"
require "id3lib"
require "ogginfo"
require "optparse"
require "pp"
require "set"
def sq str
return str if str =~ /^[-a-zA-Z0-9_\/:.=@]+$/
return "'" + str.gsub("'", "'\\\\''") + "'" unless str =~ /'/
return "\"" + str.gsub("\\", "\\\\\\\\").gsub("\"", "\\\\\"").gsub("`", "\\\\`").gsub("$", "\\\\$") + "\""
end
class DowncodeScript
DEFAULT_MAPPINGS = {
"*" => "_",
"\"" => "''",
"\\" => "_",
"?" => "_",
":" => "_",
"é" => "e",
}
attr_accessor :args
attr_accessor :status
attr_accessor :mappings
def initialize
@mappings = DEFAULT_MAPPINGS
end
def main
process_args
scan_source
delete_files
downcode_files
end
def process_args
@opts = {}
op = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename $0} [OPTIONS]"
opts.on "-s", "--source DIR", "Source directory - required" do |arg|
@opts[:source] and raise "Can't specify multiple sources"
@opts[:source] = arg.gsub /\/$/, ""
end
opts.on "-d", "--destination DIR", "Destination directory - required" do |arg|
@opts[:dest] and raise "Can't specify multiple destinations"
@opts[:dest] = arg.gsub /\/$/, ""
end
@opts[:delete] = false
opts.on "--delete", "Delete removed files" do
@opts[:delete] = true
end
@opts[:leeway] = 10
opts.on "--leeway PERCENT", "Use original file if it's less than this much bigger than the re-encoded " +
"version" do |arg|
@opts[:leeway] = arg.to_i
end
end
op.parse! @args
@opts[:source] or raise "Must specify source"
@opts[:dest] or raise "Must specify destination"
end
def map string
ret = ""
string.each_char do |ch|
ret += @mappings[ch] ? @mappings[ch] : ch
end
return ret
end
def scan_source
@todo = []
@delete = []
up_to_date = 0
skipped = {}
dsts = Set.new
Dir["#{@opts[:source]}/**/*"].each do |src|
# work out dest filename and remember it
dst = @opts[:dest] + map(src[@opts[:source].length..-1])
dsts << dst
# skip directories
next if File.directory? src
# check the extension
src =~ /^.+\.([^.]+)$/
ext = $1
if [ "mp3", "ogg" ].include? ext
# add to update list or no update count
if File.exist?(dst) && File.mtime(src) < File.mtime(dst)
up_to_date += 1
else
@todo << [ src, dst, ext ]
end
else
# add to skipped list
skipped[ext] ||= 0
skipped[ext] += 1
end
end
Dir["#{@opts[:dest]}/**/*"].each do |dst|
next if dsts.include? dst
next if dst =~ /\.(lock|tmp|tmp1)$/
@delete << dst
end
puts "Files to convert: #{@todo.size}"
puts "Files to delete: #{@delete.size}"
puts "Already up to date: #{up_to_date}"
skipped.each do |ext, count|
if ext
puts "Skipped with extension #{ext}: #{count}"
else
puts "Skipped with no extension: #{count}"
end
end
puts "Use original leeway: #{@opts[:leeway]}%"
end
def delete_files
unless @opts[:delete]
puts "Skipping delete stage (specify --delete to not skip)"
return
end
@delete.each do |dst|
puts "Deleting #{dst}"
if File.directory? dst
FileUtils.rm_r dst
elsif File.exists? dst
File.delete dst
end
end
end
def downcode_mp3 src, dst, tmp, tmp1
system "
lame \
--preset fast medium \
#{sq src} \
#{sq tmp}
" or raise "Error"
tmp_size = File.size tmp
src_size = File.size src
use_copy = src_size < tmp_size * (100 + @opts[:leeway]) / 100
if use_copy
puts "using copy instead"
FileUtils.cp src, tmp
end
unless use_copy
puts "copying id3 tag"
src_tag = ID3Lib::Tag.new src
tmp_tag = ID3Lib::Tag.new tmp
src_tag.each do |frame| tmp_tag << frame end
tmp_tag.update!
end
end
def downcode_ogg src, dst, tmp, tmp1
# downcode
system "oggdec #{sq src} --output #{sq tmp1}" or raise "Error"
system "oggenc --quality 4 #{sq tmp1} --output #{sq tmp}" or raise "Error"
# check size
tmp_size = File.size tmp
src_size = File.size src
use_copy = src_size < tmp_size * (100 + @opts[:leeway]) / 100
# use copy if appropriate
if use_copy
puts "using copy instead"
FileUtils.cp src, tmp
end
# copy metadata unless it's a copy
unless use_copy
puts "copying ogg comments"
comments = []
%x[ oggz comment --list #{sq src} ].split("\n").each do |line|
next unless line =~ /\t([A-Z0-9_]+): (.*)$/
name, value = $1, $2
comments << "#{sq name}=#{sq value}"
end
system "
oggz comment \
--content-type vorbis \
--output #{sq tmp1} \
#{sq tmp} \
#{comments.join " "}
" or raise "Error"
FileUtils.mv tmp1, tmp
end
end
def downcode_file src, dst, ext
wav = "#{dst}.wav"
tmp = "#{dst}.tmp"
tmp1 = "#{dst}.tmp1"
lock = "#{dst}.lock"
lock_fp = nil
FileUtils.mkdir_p File.dirname dst
lock_fp = File.open(lock, File::RDWR|File::CREAT, 0644)
val = lock_fp.flock(File::LOCK_EX|File::LOCK_NB)
begin
puts ""
puts "----------------------------------------------------------------------"
puts "#{File.basename src}"
puts "----------------------------------------------------------------------"
puts ""
respond_to? "downcode_#{ext}" \
or raise "File extension #{ext} not supported"
send "downcode_#{ext}", src, dst, tmp, tmp1
FileUtils.mv tmp, dst
rescue
@errors << src
ensure
File.delete wav if File.exist? wav
File.delete tmp if File.exist? tmp
File.delete tmp1 if File.exist? tmp1
if File.exist? lock
lock_fp.close
File.delete lock
end
end
end
def downcode_files
@errors = []
@todo.each do |src, dst, ext|
downcode_file src, dst, ext
end
end
end
script = DowncodeScript.new
script.args = ARGV
script.main
exit script.status || 0