-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedia.js
159 lines (151 loc) · 4.11 KB
/
media.js
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
var media,
events = require('events'),
glob = require('glob'),
util = require('util'),
async = require('async'),
ProgressBar = require('progress'),
exec = require('child_process').exec,
spawn = require('child_process').spawn,
media = function(srcFile) {
this.src = srcFile;
this.duration = 0;
this.encoded = false;
this.segments = [];
this.index = null;
return this;
}
util.inherits(media, events.EventEmitter);
media.prototype.end = function () {
var self = this;
console.log(this.src + " [ending]")
this.encoded = false;
setTimeout(function() {
console.log(self.src + " [deleting]")
exec('rm -rf ./tmp/'+ self.ts_prefix()+'*');
}, 20 * 1000)
this.emit('ended')
}
media.prototype.play = function() {
var self = this;
console.log(this.src + " [playing]");
this.emit('play');
}
media.prototype.ready = function(cb) {
if (this.encoded === true) {
cb.call(this);
} else {
this.once('ready', function() {
cb.call(this);
})
}
return this;
}
media.prototype.ts_prefix = function() {
return this.index + '-';
}
media.prototype.avconv_flags = function() {
return [
"-i", "./"+this.src,
"-codec","copy",
"-hls_time","10",
"-bsf","h264_mp4toannexb",
"tmp/" + this.ts_prefix() + '.m3u8'
];
}
media.prototype.probe = function() {
}
media.prototype.addSegments = function() {
var self = this,
total_files = 0,
current_files = 1,
bar;
// crude way of retrieving the ts files
glob("tmp/"+this.ts_prefix()+"*.ts", {}, function (er, files) {
total_files = files.length;
bar = new ProgressBar(self.src + ' [analyzing segments :bar]', { total: total_files });
async.eachSeries(files, function doProbe(file, next) {
var prb = spawn('avprobe', [
'-of','json',
'-show_streams','-show_format',
'-loglevel','panic',
file
]);
prb.stdout.on('data', function(d) {
var parsed_result;
try {
parsed_result = JSON.parse(d.toString());
if (parsed_result.streams && parsed_result.streams[0]) {
self.segments.push({
file : file,
duration : parsed_result.streams[0].duration
})
}
} catch (e) {
}
// console.log(self.src + " [segment "+current_files+" of "+total_files+"]")
current_files++;
bar.tick();
next();
});
prb.on('close', function(probe_result) {
if (probe_result === 1) next();
});
}, function finishedProbe() {
console.log(self.src + " [gathered segments]")
if (self.segments.length > 0) {
self.segments.sort(function(a,b) { // account for the stupid single/double digit integer sort
var n1 = parseInt(a.file.replace('tmp/'+self.ts_prefix(),'').replace('.ts','')),
n2 = parseInt(b.file.replace('tmp/'+self.ts_prefix(),'').replace('.ts',''));
return n1-n2;
})
self.segments[self.segments.length - 1].last_segment = true;
}
self.encoded = true;
self.emit('ready');
})
});
}
media.prototype.encode = function() {
var self = this,
total_seconds = 0,
total_frames,
frames_per_second = 25,
last_length = 0,
hours = 0, minutes = 0, seconds = 0,
convert,
progress;
console.log(self.src + " [enqueuing]")
if (self.encoded === true) {
self.emit('ready');
} else {
convert = spawn('avconv', this.avconv_flags());
convert.stderr.on('data', function(data) {
var s = data.toString().replace(/\[0;39m/g,'').replace(/\[0m/g,'').replace(/\u001b/g,''), // ctrl chars?
dur = /Duration: ([\.:0-9]+)/g.exec(s),
fps = /, ([0-9]+) fps,/.exec(s),
frame = /frame=([ 0-9]+)/.exec(s);
if (dur) {
dur = dur[1].split(":");
hours = parseInt(dur[0]), minutes = parseInt(dur[1]), seconds = parseFloat(dur[2]);
total_seconds = (hours * 60 * 60) + (minutes*60) + seconds;
}
if (fps) { // fps always follows duration?
frames_per_second = parseInt(fps[1]);
total_frames = total_seconds * frames_per_second;
progress = new ProgressBar(self.src + ' [encoding :bar]', { total: total_frames });
}
if (total_frames) {
if (frame) {
frame = parseInt(frame[1]);
progress.tick(frame - last_length);
last_length = frame;
}
}
});
convert.on('close', function(err) {
self.addSegments();
});
}
return this;
}
module.exports = media;