forked from inlife/nexrender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
166 lines (140 loc) · 5.15 KB
/
index.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
const fs = require('fs')
const path = require('path')
const {name} = require('./package.json')
const {spawn} = require('child_process')
// TODO: make it work
/* make sure pkg will pick up only needed binaries */
const binaries = {
'darwin': path.join(__dirname, 'node_modules/ffmpeg-static/bin/darwin/x64/ffmpeg'),
'win32': path.join(__dirname, 'node_modules/ffmpeg-static/bin/win32/x64/ffmpeg.exe'),
}
// /snapshot/nexrender/packages/nexrender-cli/node_modules/@nexrender/core/node_modules/@nexrender/action-encode/node_modules/ffmpeg-static/bin/darwin/x64/ffmpeg
const getBinary = (job, settings) => {
return new Promise((resolve, reject) => {
if (process.pkg) {
const output = path.join(job.workpath, process.platform == 'win32' ? 'ffmpeg.exe' : 'ffmpeg')
if (fs.existsSync(output)) {
return resolve(output);
}
const rd = fs.createReadStream(binaries[process.platform])
const wr = fs.createWriteStream(output)
const handleError = err => {
rd.destroy()
wr.end()
reject(err)
}
rd.on('error', handleError)
wr.on('error', handleError)
wr.on('finish', () => {
fs.chmodSync(output, 0o765)
resolve(output)
})
rd.pipe(wr);
} else {
const mymodule = 'ffmpeg'; /* prevent pkg from including everything */
resolve(require(mymodule + '-static').path)
}
})
}
/* pars of snippet taken from https://github.com/xonecas/ffmpeg-node/blob/master/ffmpeg-node.js#L136 */
const constructParams = (job, settings, { preset, input, output, params }) => {
input = input || job.output;
if (!path.isAbsolute(input)) input = path.join(job.workpath, input);
if (!path.isAbsolute(output)) output = path.join(job.workpath, output);
settings.logger.log(`[${job.uid}] action-encode: input file ${input}`)
settings.logger.log(`[${job.uid}] action-encode: output file ${output}`)
switch(preset) {
case 'mp4':
params = Object.assign({}, {
'-i': input,
'-acodec': 'aac',
'-ab': '128k',
'-ar': '44100',
'-vcodec': 'libx264',
'-r': '25',
'-pix_fmt' : 'yuv420p',
}, params, {
'-y': output
});
break;
case 'ogg':
params = Object.assign({}, {
'-i': input,
'-acodec': 'libvorbis',
'-ab': '128k',
'-ar': '44100',
'-vcodec': 'libtheora',
'-r': '25',
}, params, {
'-y': output
});
break;
case 'webm':
params = Object.assign({}, {
'-i': input,
'-acodec': 'libvorbis',
'-ab': '128k',
'-ar': '44100',
'-vcodec': 'libvpx',
'-b': '614400',
'-aspect': '16:9',
}, params, {
'-y': output
});
break;
case 'mp3':
params = Object.assign({}, {
'-i': input,
'-acodec': 'libmp3lame',
'-ab': '128k',
'-ar': '44100',
}, params, {
'-y': output
});
break;
case 'm4a':
params = Object.assign({}, {
'-i': input,
'-acodec': 'aac',
'-ab': '64k',
'-ar': '44100',
'-strict': '-2',
}, params, {
'-y': output
});
break;
default:
params = Object.assign({}, {
'-i': input
}, params, {
'-y': output
});
break;
}
/* convert to plain array */
return Object.keys(params).reduce(
(cur, key) => cur.concat(key, params[key]), []
);
}
module.exports = (job, settings, options, type) => {
if (type != 'postrender') {
throw new Error(`Action ${name} can be only run in postrender mode, you provided: ${type}.`)
}
settings.logger.log(`[${job.uid}] starting action-encode action (ffmpeg)`)
return new Promise((resolve, reject) => {
const params = constructParams(job, settings, options);
const binary = getBinary(job, settings).then(binary => {
const instance = spawn(binary, params);
instance.on('error', err => reject(new Error(`Error starting ffmpeg process: ${err}`)));
instance.stderr.on('data', (data) => settings.logger.log(`[${job.uid}] ${data.toString()}`));
instance.stdout.on('data', (data) => settings.debug && settings.logger.log(`[${job.uid}] ${data.toString()}`));
/* on finish (code 0 - success, other - error) */
instance.on('close', (code) => {
if (code !== 0) {
return reject(new Error('Error in action-encode module (ffmpeg)'))
}
resolve(job)
});
});
});
}