forked from nestorInc/jukebox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.rb
More file actions
205 lines (172 loc) · 4.87 KB
/
encode.rb
File metadata and controls
205 lines (172 loc) · 4.87 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
#!/usr/bin/env ruby
require 'rev'
require 'thread'
require 'date'
require 'mp3.rb'
require 'id3.rb'
ENCODE_DELAY_SCAN = 30; # seconds
MAX_ENCODE_JOB = 2;
DEFAULT_BITRATE = 192;
class EncodingThread < Rev::IO
attr_reader :pid;
attr_reader :file;
attr_reader :bitrate;
def initialize(song, bitrate, *args, &block)
@block = block;
@args = args;
@file = file;
@bitrate = bitrate;
log("Encoding #{song.src} -> #{song.dst}");
tag = Id3.decode(song.src);
song.album = tag.album;
song.artist = tag.artist;
song.title = tag.title;
song.years = tag.date;
trackStr = "#{tag.track}";
if trackStr.include?("/")
song.track = tag.track.split('/')[0];
song.trackNb = tag.track.split('/')[1];
else
song.track = trackStr;
song.trackNb = nil;
end
song.genre = tag.genre;
if(tag.title == nil || tag.artist == nil || tag.album == nil)
song.status = Library::FILE_BAD_TAG;
@block.call(song, *@args);
raise "Bad tag";
end
begin
src = song.src.gsub(/(["\\$`])/, "\\\\\\1");
dst = song.dst.gsub(/(["\\$`])/, "\\\\\\1");
song.bitrate = bitrate;
@song = song;
rd, wr = IO.pipe
rd2, wr2 = IO.pipe
@pid_encoder = fork {
wr2.close();
rd2.close();
rd.close()
STDOUT.reopen(wr)
wr.close();
Process.setpriority(Process::PRIO_PROCESS, 0, 2)
exec("mpg123 --stereo -r 44100 -s \"#{src}\"");
}
@pid_decoder = fork {
rd2.close();
wr.close();
STDIN.reopen(rd)
STDOUT.reopen(wr2)
rd.close()
Process.setpriority(Process::PRIO_PROCESS, 0, 2)
exec("lame - \"#{dst}\" -r -b #{bitrate} -t > /dev/null 2> /dev/null");
}
rd.close();
wr.close()
debug("Process encoding #{@pid_encoder} decoding#{@pid_decoder}");
wr2.close();
@fd = rd2;
super(@fd);
rescue => e
error("Encode execution error on file #{src}: #{([ e.to_s ] + e.backtrace).join("\n")}", true, $error_file);
@song.status = Library::FILE_ENCODING_FAIL;
@block.call(song, *@args);
raise e;
end
end
private
def on_read(data)
end
def on_close()
frames = Mp3File.open(@song.dst);
@song.frames = frames;
@song.duration = frames.inject(&:+) * 8 / (@song.bitrate * 1000);
pid, status = Process.waitpid2(@pid_decoder);
if(status.exitstatus() == 0)
@song.status = Library::FILE_OK;
else
@song.status = Library::FILE_ENCODING_FAIL;
end
@block.call(@song, *@args);
end
end
class Encode < Rev::TimerWatcher
def initialize(library, conf)
@library = library;
@th = [];
@cfile = [];
@originDir = conf["source_dir"] if(conf && conf["source_dir"]);
raise "Config: encode::source_dir not found" if(@originDir == nil);
@originDir.force_encoding(Encoding.locale_charmap)
@encodedDir = conf["encoded_dir"] if(conf && conf["encoded_dir"]);
raise "Config: encode::encoded_dir not found" if(@encodedDir == nil);
@encodedDir.force_encoding(Encoding.locale_charmap)
@delay_scan = conf["delay_scan"] if(conf && conf["delay_scan"]);
@delay_scan ||= ENCODE_DELAY_SCAN;
@max_job = conf["max_job"] if(conf && conf["max_job"]);
@max_job ||= MAX_ENCODE_JOB;
@bitrate = conf["bitrate"] if(conf && conf["bitrate"]);
@bitrate ||= DEFAULT_BITRATE;
super(@delay_scan, true);
end
def files()
@files;
end
def nextEncode(th)
@th.delete(th);
encode();
end
def attach(loop)
@loop = loop;
@th.each { |t|
t.attach(@loop);
}
super(loop);
end
private
def encode()
return if(@th.size >= @max_job);
song = @library.encode_file()
return if(song == nil);
mid = song.mid;
@library.change_stat(mid, Library::FILE_ENCODING_PROGRESS);
begin
enc = EncodingThread.new(song, @bitrate, self, @library) { |song, obj, lib|
lib.update(song);
obj.nextEncode(enc);
}
enc.attach(@loop) if(@loop != nil);
@th.push(enc);
rescue
end
encode();
end
def on_timer
scan();
end
def scan()
files = Dir.glob(@originDir + "/**/*.mp3");
new_files = files - @cfile;
@cfile = files;
now = Time.now;
new_files.each { | f |
name = f.scan(/.*\/(.*)/);
name = name[0][0];
if(@library.check_file(f))
# Check file is not used actualy
if(now - File::Stat.new(f).mtime < @delay_scan * 2)
@cfile.delete(f);
next;
end
song = Song.new({
"src" => f,
"dst" => @encodedDir + "/" + name,
"status" => Library::FILE_WAIT
})
@library.add(song);
encode();
end
}
encode();
end
end