-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
329 lines (306 loc) · 8.97 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
const ffmpeg = require('fluent-ffmpeg')
const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const express = require('express')
const exec = require('child_process').exec
const moment = require('moment')
const SpacebroClient = require('spacebro-client').SpacebroClient
const standardSettings = require('standard-settings')
const packageInfos = require('./package.json')
const uniquefilename = require('uniquefilename')
const download = require('download')
const { promisify } = require('util')
const access = promisify(fs.access)
const assignment = require('assignment')
const deepIterator = require('deep-iterator').default
const validUrl = require('valid-url')
const filenamify = require('filenamify')
const settings = standardSettings.getSettings()
const recipes = require('./recipes')
mkdirp.sync(settings.folder.output)
mkdirp.sync(settings.folder.tmp)
let filename
if (process.argv.indexOf('-f') !== -1) {
// does our flag exist?
filename = process.argv[process.argv.indexOf('-f') + 1] // grab the next item
const jsonfile = JSON.parse(fs.readFileSync(filename, 'utf8'))
if (jsonfile.edit.input && jsonfile.edit.output) {
const proc = ffmpeg()
.audioCodec('libmp3lame')
.on('end', function () {
console.log('files have been merged succesfully')
})
.on('error', function (err, stdout, stderr) {
console.error('an error happened: ' + err.message, stdout, stderr)
})
.on('start', function (commandLine) {
console.log('Spawned Ffmpeg with command: ' + commandLine)
})
jsonfile.edit.input.forEach(function (file) {
proc.input(path.join(path.dirname(filename), file))
})
proc.mergeToFile(path.join(path.dirname(filename), jsonfile.edit.output))
}
} else {
// console.log("Mention a json file: node index.js -f example/edit.json")
// process.exit(1)
}
let lastCommand
// init static server to serve generated files
const app = express()
app.use(express.static(settings.folder.output))
const stateServe = require('./src/state-serve')
stateServe.init(app, {
app: {
name: packageInfos.name,
version: packageInfos.version,
site: {
url: packageInfos.repository.url,
name: packageInfos.name
}
}
})
app.listen(process.env.PORT || settings.server.port)
/*
var spacebroClient = new SpacebroClient(settings.service.spacebro.host, settings.service.spacebro.port, {
channelName: settings.service.spacebro.channelName,
client: settings.service.spacebro.client,
verbose: false,
sendBack: false
})
*/
const spacebroClient = new SpacebroClient()
console.log(
`Connecting to spacebro on ${settings.service.spacebro.host}:${settings.service.spacebro.port}`
)
spacebroClient.on('connect', () => {
console.log(
`spacebro: ${settings.service.spacebro.client.name} connected to ${settings.service.spacebro.host}:${settings.service.spacebro.port}#${settings.service.spacebro.channelName}`
)
})
spacebroClient.on('newClient', (data) => {
console.log(`spacebro: ${data.name} has joined.`)
})
spacebroClient.on('disconnect', () => {
console.error('spacebro: connection lost.')
})
const sendMedia = function (data) {
if (
data.input &&
data.input.includes &&
data.input.includes(settings.folder.tmpDownload)
) {
fs.unlink(data.input, () => {})
}
delete data.input
delete data.output
delete data.outputTempPath
spacebroClient.emit(
settings.service.spacebro.client.out.outVideo.eventName,
data
)
console.log(data)
}
const getDuration = (path) => {
return new Promise((resolve) => {
ffmpeg.ffprobe(path, function (err, metadata) {
if (err) {
resolve(0)
} else {
resolve(metadata.format.duration)
}
})
})
}
const downloadWithCache = async function (url) {
const filename = filenamify(url)
const filepath = path.join(settings.folder.tmpDownload, filename)
if (!settings.cache) {
console.log('downloading ' + url)
await download(url, settings.folder.tmpDownload, { filename })
} else {
let exists = false
try {
await access(filepath)
exists = true
} catch (err) {
exists = false
}
if (!exists) {
console.log('downloading ' + url)
try {
await download(url, settings.folder.tmpDownload, { filename })
} catch (err) {
console.error('an error occured while downloading with cache')
throw err
}
} else {
console.log('in cache: ' + url)
}
}
return filepath
}
const downloadFile = async function (data) {
if (data.url) {
let exists = false
if (data.path) {
try {
await access(data.path)
exists = true
} catch (e) {
exists = false
}
}
if (!exists) {
try {
data.path = await downloadWithCache(data.url)
} catch (e) {
console.log(e)
}
}
}
return data
}
const downloadFilesInMeta = async function (data) {
const values = data.meta
for (const { parent, key, value } of deepIterator(values)) {
if (
key !== 'mjpgStream' &&
key !== 'baseURL' &&
value &&
typeof value === 'string' &&
validUrl.isUri(value)
) {
try {
const filepath = await downloadWithCache(value)
parent[key] = filepath
} catch (e) {
console.log(e)
}
}
}
return data
}
const setFilenames = async function (data) {
if (data.path && typeof data.input !== 'string') {
data.input = data.path
}
if (data.input) {
data.output =
data.output ||
path.join(settings.folder.output, path.basename(data.input))
data.output += '.mp4'
} else {
if (!data.output) {
const date = moment()
const timestampName = date.format('YYYY-MM-DDTHH-mm-ss-SSS') + '.mp4'
data.output = path.join(
settings.folder.output,
path.basename(timestampName)
)
}
}
data.output = await uniquefilename.get(data.output)
data.outputTempPath =
data.outputTempPath ||
path.join(settings.folder.tmp, path.basename(data.output))
return data
}
const isImage = (data) => {
if (data.filename) {
if (data.filename.match(/png$/)) {
return true
}
}
return false
}
const onInputReceived = async (data) => {
try {
console.log(
`Received event ${
settings.service.spacebro.client.in.inMedia.eventName
}, new media: ${JSON.stringify(data)}`
)
if (settings.minDuration) {
const duration = await getDuration(data.path)
if (duration < settings.minDuration) {
throw Error('File too small to be processed: ' + duration + ' seconds')
}
}
if (isImage(data)) {
console.log('Image, pass through, send media without changes')
sendMedia(data)
return
}
// data = assignment(data, JSON.parse(JSON.stringify(settings.media)))
data = assignment(JSON.parse(JSON.stringify(settings.media)), data)
// save input in meta
if (data.meta === undefined) {
data.meta = {}
}
const etnaInput = JSON.parse(JSON.stringify(data))
// download
// removing this function allow to use the default recipe. This might
// breaks something else.
delete data.input
data = await downloadFile(data)
for (const key in data.details) {
await downloadFile(data.details[key])
}
data = await downloadFilesInMeta(data)
// process
data = await setFilenames(data)
data.meta.etnaInput = etnaInput
const recipe = data.recipe || settings.recipe
const recipeFn = recipes.recipe(recipe)
lastCommand = recipeFn(data, function () {
// fs.rename(data.outputTempPath, data.output, function (err) {
if (recipe !== 'addThumbnail') {
exec('mv ' + data.outputTempPath + ' ' + data.output, function (err) {
if (err) {
console.error('an error occured', err)
} else {
console.log('finished processing ' + data.output)
data.type = 'video/' + path.extname(data.output).substring(1)
data.path = data.output
data.file = path.basename(data.output)
data.url = `http://${settings.server.host}:${
settings.server.port
}/${path.basename(data.output)}`
const meta = standardSettings.getMeta(data)
if (meta.thumbnail) {
data.input = data.output
recipes.recipe('addThumbnail')(data, () => {
sendMedia(data)
})
} else {
sendMedia(data)
}
}
})
} else {
sendMedia(data)
}
})
} catch (error) {
console.error(error)
}
}
// TODO: document 'new-media' data.recipe, data.input, data.output
// add data.options, like the path for an image to watermark, framerate, ...
spacebroClient.on(
settings.service.spacebro.client.in.inMedia.eventName,
(data) => {
onInputReceived(data)
}
)
spacebroClient.on(
settings.service.spacebro.client.in.stopLastProcess.eventName,
function (data) {
console.log('kill')
if (lastCommand) {
lastCommand.kill('SIGINT')
}
}
)