-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmpv.js
149 lines (125 loc) · 3.59 KB
/
mpv.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
const cp = require('child_process'),
FIFO = require('fifo-js'),
commands = require('./commands');
const pausedStrings = [
"(Paused)",
"(Buffering)"
];
const AV = "AV:";
class mpv {
constructor(listener) {
this.dataHandler = new DataHandler(listener);
this.registerControlFunctions();
}
registerControlFunctions() {
for (let name in commands) {
let f = commands[name];
this[name] = (...args) => this.sendCommand(f(...args));
}
}
setListener(listener) {
this.dataHandler.setListener(listener);
}
removeListener() {
this.dataHandler.setListener();
}
limitStatusMessages(mod) {
this.dataHandler.limit = mod;
}
play(first, second, third) {
this.fifo = new FIFO();
let flags = ["--input-file=" + this.fifo.path];
if (Array.isArray(first)) {
flags = flags.concat(first);
} else {
flags.push(first);
if (Array.isArray(second)) {
flags = flags.concat(second);
} else {
flags.push('--sub-file="' + second + '"');
if (Array.isArray(third)) {
flags = flags.concat(third);
}
}
}
this.startMpv(flags);
}
startMpv(flags) {
this.player = cp.spawn('mpv', flags);
this.player.stderr.setEncoding('utf8');
this.player.stderr.on('data',
this.dataHandler.handleData.bind(this.dataHandler));
this.player.stderr.on('close', this.closed.bind(this));
}
closed() {
this.fifo.close();
this.dataHandler.closed();
this.player = null;
}
sendCommand(command) {
if (this.player) {
this.fifo.write(command)
}
}
kill() {
this.fifo.close();
this.player.kill();
}
}
class DataHandler {
constructor(listener) {
this.listener = listener;
this._limit = 1;
this.statusCounter = 0;
this.lastStatus;
}
setListener(listener) {
this.listener = listener;
}
set limit(mod) {
this._limit = mod < 1 ? 1 : mod;
}
handleData(data) {
let status = this.parseData(data.toString());
if (status !== undefined && typeof this.listener !== 'undefined'
&& (this.statusCounter++ % this._limit === 0
|| this.lastStatus !== status.playing)) {
this.lastStatus = status.playing;
this.listener(status);
}
}
closed() {
if (typeof this.listener !== 'undefined') {
this.listener({
exit: true
});
}
}
parseData(data) {
let parts = data.split(' ');
let playing = pausedStrings.indexOf(parts[0]) === -1;
let buffering = parts[0] === pausedStrings[1];
if (!playing) {
parts.shift();
}
if (parts[0] === AV) {
let percentage = typeof parts[4] === 'undefined' ? 0 : parts[4].replace(/\(|\)|%/g, '')
let status = {
playing: playing,
buffering: buffering,
elapsed: this.parseTime(parts[1]),
total: this.parseTime(parts[3]),
elapsedStr: parts[1],
totalStr: parts[3],
progress: percentage / 100
}
return status;
}
}
parseTime(time) {
var arr = time.split(':');
var seconds = (+arr[0]) * 60 * 60 + (+arr[1]) * 60 + (+arr[2]);
return seconds;
}
}
module.exports = mpv;