-
Notifications
You must be signed in to change notification settings - Fork 76
/
install_ffmpeg.js
239 lines (217 loc) · 7.82 KB
/
install_ffmpeg.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
/*
Aerostat Beam Coder - Node.js native bindings to FFmpeg.
Copyright (C) 2019 Streampunk Media Ltd.
This program 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 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
https://www.streampunk.media/ mailto:[email protected]
14 Ormiscaig, Aultbea, Achnasheen, IV22 2JJ U.K.
*/
const os = require('os');
const fs = require('fs');
const util = require('util');
const https = require('https');
const cp = require('child_process');
const [ mkdir, access, rename, execFile, exec ] = // eslint-disable-line
[ fs.mkdir, fs.access, fs.rename, cp.execFile, cp.exec ].map(util.promisify);
async function get(ws, url, name) {
let received = 0;
let totalLength = 0;
return new Promise((comp, err) => {
https.get(url, res => {
if (res.statusCode === 301 || res.statusCode === 302) {
err({ name: 'RedirectError', message: res.headers.location });
} else {
res.pipe(ws);
if (totalLength == 0) {
totalLength = +res.headers['content-length'];
}
res.on('end', () => {
process.stdout.write(`Downloaded 100% of '${name}'. Total length ${received} bytes.\n`);
comp();
});
res.on('error', err);
res.on('data', x => {
received += x.length;
process.stdout.write(`Downloaded ${received * 100/ totalLength | 0 }% of '${name}'.\r`);
});
}
}).on('error', err);
});
}
async function getHTML(url, name) {
let received = 0;
let totalLength = 0;
return new Promise((resolve, reject) => {
https.get(url, res => {
const chunks = [];
if (totalLength == 0) {
totalLength = +res.headers['content-length'];
}
res.on('end', () => {
process.stdout.write(`Downloaded 100% of '${name}'. Total length ${received} bytes.\n`);
resolve(Buffer.concat(chunks));
});
res.on('error', reject);
res.on('data', (chunk) => {
chunks.push(chunk);
received += chunk.length;
process.stdout.write(`Downloaded ${received * 100/ totalLength | 0 }% of '${name}'.\r`);
});
}).on('error', reject);
});
}
async function inflate(rs, folder, name) {
const unzip = require('unzipper');
const directory = await unzip.Open.file(`${folder}/${name}.zip`);
const directoryName = directory.files[0].path;
return new Promise((comp, err) => {
console.log(`Unzipping '${folder}/${name}.zip'.`);
rs.pipe(unzip.Extract({ path: folder }).on('close', () => {
fs.rename(`./${folder}/${directoryName}`, `./${folder}/${name}`, () => {
console.log(`Unzipping of '${folder}/${name}.zip' completed.`);
comp();
});
}));
rs.on('error', err);
});
}
async function win32() {
console.log('Checking/Installing FFmpeg dependencies for Beam Coder on Windows.');
await mkdir('ffmpeg').catch(e => {
if (e.code === 'EEXIST') return;
else throw e;
});
const ffmpegFilename = 'ffmpeg-5.x-win64-shared';
await access(`ffmpeg/${ffmpegFilename}`, fs.constants.R_OK).catch(async () => {
const html = await getHTML('https://github.com/BtbN/FFmpeg-Builds/wiki/Latest', 'latest autobuilds');
const htmlStr = html.toString('utf-8');
const autoPos = htmlStr.indexOf('<p><a href=');
const endPos = htmlStr.indexOf('</div>', autoPos);
const autoStr = htmlStr.substring(autoPos, endPos);
const sharedEndPos = autoStr.lastIndexOf('">win64-gpl-shared-5.');
if (sharedEndPos === -1)
throw new Error('Failed to find latest v4.x autobuild from "https://github.com/BtbN/FFmpeg-Builds/wiki/Latest"');
const startStr = '<p><a href="';
const sharedStartPos = autoStr.lastIndexOf(startStr, sharedEndPos) + startStr.length;
const downloadSource = autoStr.substring(sharedStartPos, sharedEndPos);
let ws_shared = fs.createWriteStream(`ffmpeg/${ffmpegFilename}.zip`);
await get(ws_shared, downloadSource, `${ffmpegFilename}.zip`)
.catch(async (err) => {
if (err.name === 'RedirectError') {
const redirectURL = err.message;
await get(ws_shared, redirectURL, `${ffmpegFilename}.zip`);
} else console.error(err);
});
await exec('npm install unzipper --no-save');
let rs_shared = fs.createReadStream(`ffmpeg/${ffmpegFilename}.zip`);
await inflate(rs_shared, 'ffmpeg', `${ffmpegFilename}`);
});
}
async function linux() {
console.log('Checking FFmpeg dependencies for Beam Coder on Linux.');
const { stdout } = await execFile('ldconfig', ['-p']).catch(console.error);
let result = 0;
if (stdout.indexOf('libavcodec.so.59') < 0) {
console.error('libavcodec.so.59 is not installed.');
result = 1;
}
if (stdout.indexOf('libavformat.so.59') < 0) {
console.error('libavformat.so.59 is not installed.');
result = 1;
}
if (stdout.indexOf('libavdevice.so.59') < 0) {
console.error('libavdevice.so.59 is not installed.');
result = 1;
}
if (stdout.indexOf('libavfilter.so.8') < 0) {
console.error('libavfilter.so.8 is not installed.');
result = 1;
}
if (stdout.indexOf('libavutil.so.57') < 0) {
console.error('libavutil.so.57 is not installed.');
result = 1;
}
if (stdout.indexOf('libpostproc.so.56') < 0) {
console.error('libpostproc.so.56 is not installed.');
result = 1;
}
if (stdout.indexOf('libswresample.so.4') < 0) {
console.error('libswresample.so.4 is not installed.');
result = 1;
}
if (stdout.indexOf('libswscale.so.6') < 0) {
console.error('libswscale.so.6 is not installed.');
result = 1;
}
if (result === 1) {
console.log(`Try running the following (Ubuntu/Debian):
sudo add-apt-repository ppa:jonathonf/ffmpeg-4
sudo apt-get install libavcodec-dev libavformat-dev libavdevice-dev libavfilter-dev libavutil-dev libpostproc-dev libswresample-dev libswscale-dev`);
process.exit(1);
}
return result;
}
async function darwin() {
console.log('Checking for FFmpeg dependencies via HomeBrew.');
let output;
let returnMessage;
try {
output = await exec('brew list ffmpeg');
returnMessage = 'FFmpeg already present via Homebrew.';
} catch (err) {
if (err.stderr !== 'Error: No such keg: /usr/local/Cellar/ffmpeg\n') {
console.error(err);
console.log('Either Homebrew is not installed or something else is wrong.\nExiting');
process.exit(1);
}
console.log('FFmpeg not installed. Attempting to install via Homebrew.');
try {
output = await exec('brew install nasm pkg-config texi2html ffmpeg');
returnMessage = 'FFmpeg installed via Homebrew.';
} catch (err) {
console.log('Failed to install ffmpeg:\n');
console.error(err);
process.exit(1);
}
}
console.log(output.stdout);
console.log(returnMessage);
return 0;
}
switch (os.platform()) {
case 'win32':
if (os.arch() != 'x64') {
console.error('Only 64-bit platforms are supported.');
process.exit(1);
} else {
win32().catch(console.error);
}
break;
case 'linux':
if (os.arch() != 'x64' && os.arch() != 'arm64') {
console.error('Only 64-bit platforms are supported.');
process.exit(1);
} else {
linux();
}
break;
case 'darwin':
if (os.arch() != 'x64' && os.arch() != 'arm64') {
console.error('Only 64-bit platforms are supported.');
process.exit(1);
} else {
darwin();
}
break;
default:
console.error(`Platfrom ${os.platform()} is not supported.`);
break;
}