- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdowncode
More file actions
Latest commit
executable file
·309 lines (238 loc) · 6.26 KB
/
Copy pathdowncode
File metadata and controls
executable file
·309 lines (238 loc) · 6.26 KB
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 <james@phsys.co.uk>
#
# 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"
defsqstr
returnstrifstr =~ /^[-a-zA-Z0-9_\/:.=@]+$/
return"'" + str.gsub("'","'\\\\''") + "'"unlessstr =~ /'/
return"\"" + str.gsub("\\","\\\\\\\\").gsub("\"","\\\\\"").gsub("`","\\\\`").gsub("$","\\\\$") + "\""
end
classDowncodeScript
DEFAULT_MAPPINGS={
"*"=>"_",
"\""=>"''",
"\\"=>"_",
"?"=>"_",
":"=>"_",
"é"=>"e",
}
attr_accessor:args
attr_accessor:status
attr_accessor:mappings
definitialize
@mappings=DEFAULT_MAPPINGS
end
defmain
process_args
scan_source
delete_files
downcode_files
end
defprocess_args
@opts={}
op=OptionParser.newdo |opts|
opts.banner="Usage: #{File.basename $0} [OPTIONS]"
opts.on"-s","--source DIR","Source directory - required"do |arg|
@opts[:source]andraise"Can't specify multiple sources"
@opts[:source]=arg.gsub/\/$/,""
end
opts.on"-d","--destination DIR","Destination directory - required"do |arg|
@opts[:dest]andraise"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]orraise"Must specify source"
@opts[:dest]orraise"Must specify destination"
end
defmapstring
ret=""
string.each_chardo |ch|
ret += @mappings[ch] ? @mappings[ch] : ch
end
returnret
end
defscan_source
@todo=[]
@delete=[]
up_to_date=0
skipped={}
dsts=Set.new
Dir["#{@opts[:source]}/**/*"].eachdo |src|
# work out dest filename and remember it
dst=@opts[:dest] + map(src[@opts[:source].length..-1])
dsts << dst
# skip directories
nextifFile.directory?src
# check the extension
src =~ /^.+\.([^.]+)$/
ext= $1
if["mp3","ogg"].include?ext
# add to update list or no update count
ifFile.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]}/**/*"].eachdo |dst|
nextifdsts.include?dst
nextifdst =~ /\.(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.eachdo |ext,count|
ifext
puts"Skipped with extension #{ext}: #{count}"
else
puts"Skipped with no extension: #{count}"
end
end
puts"Use original leeway: #{@opts[:leeway]}%"
end
defdelete_files
unless@opts[:delete]
puts"Skipping delete stage (specify --delete to not skip)"
return
end
@delete.eachdo |dst|
puts"Deleting #{dst}"
ifFile.directory?dst
FileUtils.rm_rdst
elsifFile.exists?dst
File.deletedst
end
end
end
defdowncode_mp3src,dst,tmp,tmp1
system"
lame \
--preset fast medium \
#{sqsrc}\
#{sqtmp}
"orraise"Error"
tmp_size=File.sizetmp
src_size=File.sizesrc
use_copy=src_size < tmp_size * (100 + @opts[:leeway]) / 100
ifuse_copy
puts"using copy instead"
FileUtils.cpsrc,tmp
end
unlessuse_copy
puts"copying id3 tag"
src_tag=ID3Lib::Tag.newsrc
tmp_tag=ID3Lib::Tag.newtmp
src_tag.eachdo |frame| tmp_tag << frameend
tmp_tag.update!
end
end
defdowncode_oggsrc,dst,tmp,tmp1
# downcode
system"oggdec #{sqsrc} --output #{sqtmp1}"orraise"Error"
system"oggenc --quality 4 #{sqtmp1} --output #{sqtmp}"orraise"Error"
# check size
tmp_size=File.sizetmp
src_size=File.sizesrc
use_copy=src_size < tmp_size * (100 + @opts[:leeway]) / 100
# use copy if appropriate
ifuse_copy
puts"using copy instead"
FileUtils.cpsrc,tmp
end
# copy metadata unless it's a copy
unlessuse_copy
puts"copying ogg comments"
comments=[]
%x[ oggz comment --list #{sqsrc} ].split("\n").eachdo |line|
nextunlessline =~ /\t([A-Z0-9_]+): (.*)$/
name,value= $1, $2
comments << "#{sqname}=#{sqvalue}"
end
system"
oggz comment \
--content-type vorbis \
--output #{sqtmp1}\
#{sqtmp}\
#{comments.join" "}
"orraise"Error"
FileUtils.mvtmp1,tmp
end
end
defdowncode_filesrc,dst,ext
wav="#{dst}.wav"
tmp="#{dst}.tmp"
tmp1="#{dst}.tmp1"
lock="#{dst}.lock"
lock_fp=nil
FileUtils.mkdir_pFile.dirnamedst
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.basenamesrc}"
puts"----------------------------------------------------------------------"
puts""
respond_to?"downcode_#{ext}" \
orraise"File extension #{ext} not supported"
send"downcode_#{ext}",src,dst,tmp,tmp1
FileUtils.mvtmp,dst
rescue
@errors << src
ensure
File.deletewavifFile.exist?wav
File.deletetmpifFile.exist?tmp
File.deletetmp1ifFile.exist?tmp1
ifFile.exist?lock
lock_fp.close
File.deletelock
end
end
end
defdowncode_files
@errors=[]
@todo.eachdo |src,dst,ext|
downcode_filesrc,dst,ext
end
end
end
script=DowncodeScript.new
script.args=ARGV
script.main
exitscript.status || 0