-
Notifications
You must be signed in to change notification settings - Fork 257
/
converter.js
260 lines (239 loc) · 6.61 KB
/
converter.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
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
/*
vdhcoapp: Video DownloadHelper Companion app
Copyright (C) 2017 downloadhelper.net
This file is part of vdhcoapp.
Vdhcoapp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Vdhcoapp is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Vdhcoapp. If not, see <http://www.gnu.org/licenses/>
*/
const path = require('path');
const { spawn } = require('child_process');
const open = require('open');
const logger = require('./logger');
const rpc = require('./weh-rpc');
var platform;
if(process.platform=="linux")
platform = "linux";
else if(process.platform=="darwin")
platform = "mac";
else if(/^win/.test(process.platform))
platform = "win";
else
throw new Error("Unsupported platform",process.platform);
var arch;
if(process.arch=="x64")
arch = "64";
else if(process.arch=="ia32")
arch = "32";
else
throw new Error("Unsupported architecture",process.arch);
const binaryDir = path.join(path.dirname(process.execPath),"..","converter","build",platform,arch);
const binaryPath = path.join(binaryDir,"ffmpeg");
const probeBinaryPath = path.join(binaryDir,"ffprobe");
const playBinaryPath = path.join(binaryDir,"ffplay");
const LIBRARY_PATH = platform == "mac" ? "DYLD_LIBRARY_PATH" : "LD_LIBRARY_PATH"
logger.info("process.cwd",process.cwd);
logger.info("__dirname",__dirname);
logger.info("__filename",__filename);
logger.info("path.resolve('.')",path.resolve('.'));
logger.info("process.execPath",process.execPath);
function ExecConverter(args) {
var outBuffers = [];
return new Promise((resolve, reject) => {
var convProcess = spawn(binaryPath, args, {
env: {
[LIBRARY_PATH]: binaryDir
}
});
convProcess.stdout.on("data", (data) => {
outBuffers.push(data);
});
convProcess.stderr.on("data", (data) => {
// need to consume data or process stalls
});
convProcess.on("exit", (exitCode) => {
if(exitCode!==0)
return reject(new Error("Converter returned exit code "+exitCode));
var out = Buffer.concat(outBuffers).toString("utf8");
resolve(out);
});
});
}
const PARSETIME_RE = new RegExp("time=([0-9]+):([0-9]+):([0-9]+)");
rpc.listen({
"convert": (args=["-h"],options={}) => {
return new Promise((resolve, reject) => {
var convProcess = spawn(binaryPath, args, {
env: {
[LIBRARY_PATH]: binaryDir
}
});
var stdErrParts = [];
var stdErrSize = 0;
convProcess.stderr.on("data",(data)=>{
// just consume data
});
convProcess.stderr.on("data",(data)=>{
var str = data.toString("utf8");
var m = PARSETIME_RE.exec(str);
if(m) {
if(options.progressTime) {
var frameTime = parseFloat(m[1])*3600 + parseFloat(m[2])*60 + parseFloat(m[3]);
rpc.call("convertOutput",options.progressTime,frameTime)
.catch((err)=>{
convProcess.kill();
});
}
} else {
const maxSize = 20000;
if(stdErrSize+str.length>=maxSize)
str = str.substr(0,maxSize-stdErrSize);
if(str.length>0) {
stdErrParts.push(str);
stdErrSize+=str.length;
}
}
});
convProcess.on("exit", (exitCode) => {
resolve({exitCode,stderr:stdErrParts.join("")});
});
});
},
"probe": (filePath) => {
return new Promise((resolve, reject) => {
var probeProcess = spawn(probeBinaryPath, [filePath], {
env: {
[LIBRARY_PATH]: binaryDir
}
});
var streams = {
stdout: "",
stderr: ""
}
Object.keys(streams).forEach((stream) => {
probeProcess[stream].on("data", (data) => {
streams[stream] += data.toString("utf8");
});
});
probeProcess.on("exit", (exitCode) => {
if(exitCode!==0)
return reject(new Error("Exit code: "+exitCode+"\n"+streams.stderr));
var info={};
var m=/([0-9]{2,})x([0-9]{2,})/g.exec(streams.stderr);
if(m) {
info.width = parseInt(m[1]);
info.height = parseInt(m[2]);
}
m=/Duration: ([0-9]{2}):([0-9]{2}):([0-9]{2})\.([0-9]{2})/g.exec(streams.stderr);
if(m)
info.duration = parseInt(m[1])*3600+parseInt(m[2])*60+parseInt(m[3]);
m=/Video:\s+([^\s\(,]+)/g.exec(streams.stderr);
if(m)
info.videoCodec = m[1];
m=/([0-9]+(?:\.[0-9]+)?)\s+fps\b/g.exec(streams.stderr);
if(m)
info.fps = parseFloat(m[1]);
resolve(info);
});
});
},
"play": (filePath) => {
return new Promise((resolve, reject) => {
var playProcess = spawn(playBinaryPath, [filePath], {
env: Object.assign({},process.env,{
[LIBRARY_PATH]: binaryDir
})
});
var stderr = [];
playProcess.stderr.on("data", (data) => {
stderr.push(data.toString("utf8"));
});
playProcess.on("exit", (exitCode) => {
if(exitCode!==0)
reject(new Error(stderr.slice(1).join("")));
else
resolve();
});
});
},
"codecs": () => {
return ExecConverter(["-codecs"])
.then((out)=>{
var lines = out.split("\n");
var result = {};
lines.forEach((line)=>{
var m = /^\s*(\.|D)(\.|E)(\.|V|A|S)(\.|I)(\.|L)(\.|S)\s+([^\s]+)\s+(.*?)\s*$/.exec(line);
if(!m || m[7]==='=')
return;
result[m[7]] = {
d: m[1]!=".",
e: m[2]!=".",
t: m[3]=="." && null || m[3],
i: m[4]!=".",
l: m[5]!=".",
s: m[6]!=".",
_: m[8]
}
});
return result;
});
},
"formats": () => {
return ExecConverter(["-formats"])
.then((out)=>{
var lines = out.split("\n");
var result = {};
lines.forEach((line)=>{
var m = /^\s*(\.| |D)(\.| |E)\s+([^\s]+)\s+(.*?)\s*$/.exec(line);
if(!m || m[3]==='=')
return;
result[m[3]] = {
d: m[1]=="D",
e: m[2]=="E",
_: m[4]
}
});
return result;
});
},
"open": (filePath) => {
open(filePath);
},
});
exports.info = () => {
return new Promise((resolve, reject) => {
var convProcess = spawn(binaryPath, ["-h"], {
env: {
[LIBRARY_PATH]: binaryDir
}
});
var done = false;
function Parse(data) {
if(done) return;
var str = data.toString("utf8");
logger.info("stdout:",str);
var m = /^(\S+).*?v.*?((?:\d+\.)+\d+)/.exec(str);
if(m) {
done = true;
resolve({
program: m[1],
version: m[2],
converterBinary: binaryPath
});
}
}
convProcess.stdout.on("data", Parse);
convProcess.stderr.on("data", Parse);
convProcess.on("exit", (code) => {
if(!done)
reject(new Error("Exit without answer"));
});
})
}