-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathencode.rb
164 lines (138 loc) · 4.33 KB
/
encode.rb
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
#!/usr/bin/env ruby
require 'thread'
require 'date'
require 'mp3.rb'
require 'id3.rb'
require 'worker.rb'
ENCODE_DELAY_SCAN = 30; # seconds
MAX_ENCODE_JOB = 2;
DEFAULT_BITRATE = 192;
def job(song, bitrate, messaging)
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;
messaging.send(:library, :add, song);
error("Bad tag #{song.src}");
return
end
begin
src = song.src.gsub(/(["\\$`])/, "\\\\\\1");
dst = song.dst.gsub(/(["\\$`])/, "\\\\\\1");
song.bitrate = bitrate;
rd, wr = IO.pipe
pid_decoder = fork {
rd.close()
STDOUT.reopen(wr)
wr.close();
Process.setpriority(Process::PRIO_PROCESS, 0, 2)
exec("mpg123 --stereo -r 44100 -s \"#{src}\"");
}
pid_encoder = fork {
wr.close();
STDIN.reopen(rd)
STDOUT.close()
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}");
Process.detach(pid_decoder)
pid, status = Process.waitpid2(pid_encoder);
if(status.exitstatus() == 0)
frames = Mp3File.open(song.dst);
song.duration = frames.map(&:duration).inject(&:+);
song.status = Library::FILE_OK;
else
song.status = Library::FILE_ENCODING_FAIL;
end
messaging.send(:library, :add, song);
rescue => e
error("Encode execution error on file #{song.src}: #{([ e.to_s ] + e.backtrace).join("\n")}", true, $error_file);
song.status = Library::FILE_ENCODING_FAIL;
messaging.send(:library, :add, song);
raise e;
end
end
class Encode
def initialize(library, messaging, conf)
@library = library;
@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;
@messaging = messaging;
@messaging.create(:file_scan, 1) do |*args|
scan()
end
@messaging.create(:encode, @max_job) do |*args|
job(*args)
end
@messaging.create(:library, 1) do |action, song|
case(action)
when :add
@library.add(song)
when :update
@library.update(song)
end
end
EM::add_periodic_timer(@delay_scan) do
@messaging.send(:file_scan)
end
end
def files()
@files;
end
def attach(loop)
@loop = loop;
super(loop);
end
private
def scan()
files = Dir.glob(@originDir + "/**/*.mp3");
files.map { |s| s.force_encoding("BINARY") }
new_files = files - @cfile;
@cfile = files;
now = Time.now;
new_files.each do | f |
name = f.scan(/.*\/(.*)/);
name = name[0][0];
# Check file is not used actualy
next if(@library.check_file(f) == false)
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
})
@messaging.send(:encode, song, @bitrate, @messaging)
end
end
end