This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 257
/
extract.js
384 lines (363 loc) · 11.2 KB
/
extract.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
* Copyright 2016 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const async = require('async');
const fs = require('fs');
const ffmpeg = require('fluent-ffmpeg');
const tmp = require('tmp');
const rimraf = require('rimraf');
// Show some info about the docker action
try {
console.log('Docker action built on:', require('./build.json').date);
} catch (err) {
console.log('No build.json file found', err);
}
// argv[2] is expected to be the payload JSON object as a string
const payload = JSON.parse(process.argv[2]);
console.log('Payload', payload);
const doc = payload.doc;
const extractOptions = {
videoThumbnailSize: 640,
speechDuration: 15 * 60 // export only the first 15 minutes of audio
};
function getFps(durationInSeconds) {
// this gives around 15 images per video
return `1/${Math.ceil(durationInSeconds / 15)}`;
// for a more complete analysis,
// use this code that will extract up to 100 images
/*
if (durationInSeconds <= 10) {
return '2/1'; // 2 images per seconds
} else if (durationInSeconds > 10 && durationInSeconds <= 100) {
return '1/1'; // 1 image per seconds
} else { // eslint-disable-line no-else-return
return `1/${Math.ceil(durationInSeconds / 100)}`;
}
*/
}
// create a temporary directory to process the video
const workingDirectory = tmp.dirSync({
prefix: `extractor-${doc._id}`
});
// plan to clean up temp directories
tmp.setGracefulCleanup();
console.log('Using temp dir', workingDirectory.name);
const framesDirectory = `${workingDirectory.name}/frames`;
fs.mkdirSync(framesDirectory);
let mediaStorage;
let videoDocument;
const inputFilename = `${workingDirectory.name}/video.mp4`;
async.waterfall([
// establish the storage connection if configured in payload
function(callback) {
if (payload.cosApiKey) {
require('./lib/cloudobjectstorage')({
endpoint: payload.cosEndpoint,
apikey: payload.cosApiKey,
instanceId: payload.cosInstanceId,
bucket: payload.cosBucket,
}, (err, fileStore) => {
if (err) {
callback(err);
} else {
callback(null, fileStore);
}
});
console.log('Media files are stored in Cloud Object Storage.');
} else {
console.log('Media files are stored in Cloudant.');
callback(null, null);
}
},
// connect to the database
(initializedFileStore, callback) => {
console.log('Initializing Cloudant...');
mediaStorage = require('./lib/cloudantstorage')({
cloudantUrl: payload.cloudantUrl,
cloudantDbName: payload.cloudantDbName,
fileStore: initializedFileStore
});
callback(null);
},
// load the document from the database
(callback) => {
mediaStorage.get(doc._id, (err, body) => {
if (err) {
callback(err);
} else {
videoDocument = body;
console.log('Video is', videoDocument);
// if metadata exists we consider this document as already processed and do nothing
if (videoDocument.metadata) {
callback('already processed');
} else {
callback(null);
}
}
});
},
// save its video attachment to disk
(callback) => {
console.log('Downloading video attachment...');
const videoStream = fs.createWriteStream(inputFilename);
const totalSize = mediaStorage.getAttachmentSize(videoDocument, 'video.mp4');
let currentSize = 0;
let lastProgress;
mediaStorage.read(videoDocument, 'video.mp4')
.on('data', (data) => {
currentSize += data.length;
const progress = Math.round((currentSize * 100) / totalSize);
if (progress !== lastProgress && progress % 5 === 0) {
console.log('Downloaded', progress, '% (', currentSize, '/', totalSize, ')');
lastProgress = progress;
}
})
.pipe(videoStream)
.on('finish', () => {
videoStream.end();
console.log('write complete');
callback(null);
})
.on('error', (err) => {
videoStream.end();
console.log('error while writing', err);
callback(err);
});
},
// extract video metadata
(callback) => {
ffmpeg.ffprobe(inputFilename, (err, metadata) => {
if (!err) {
videoDocument.metadata = metadata;
console.log('Extracted video metadata', videoDocument);
}
callback(err);
});
},
// persist the videoDocument with its metadata
(callback) => {
mediaStorage.insert(videoDocument, (err, body) => {
if (err) {
callback(err);
} else {
videoDocument._rev = body.rev;
callback(null);
}
});
},
// extract the audio
(callback) => {
ffmpeg()
.input(inputFilename)
.outputOptions([
'-qscale:a',
'3',
'-acodec',
'vorbis',
'-map',
'a',
'-strict',
'-2',
// get only first n seconds
'-ss 0',
`-t ${extractOptions.speechDuration}`,
// force dual channel audio as vorbis encoder only supports 2 channels
'-ac',
'2'
])
.output(`${workingDirectory.name}/audio.ogg`)
.on('progress', (progress) => {
console.log(`Exporting audio: ${Math.round(progress.percent)}% done`);
})
.on('error', (err) => {
console.log('Audio export', err);
callback(err);
})
.on('end', () => {
callback(null);
})
.run();
},
// create a new "audio" document
(callback) => {
const audioDocument = {
type: 'audio',
video_id: videoDocument._id,
language_model: videoDocument.language_model
};
mediaStorage.insert(audioDocument, (err, insertedDoc) => {
if (err) {
callback(err);
} else {
audioDocument._id = insertedDoc.id;
audioDocument._rev = insertedDoc.rev;
callback(null, audioDocument);
}
});
},
// persist the audio attachment with the video
(audioDocument, callback) => {
console.log('Uploading audio...');
fs.createReadStream(`${workingDirectory.name}/audio.ogg`).pipe(
mediaStorage.attach(audioDocument, 'audio.ogg', 'audio/ogg', (attachErr, attachedDoc) => {
fs.unlink(`${workingDirectory.name}/audio.ogg`, (unlinkErr) => {
if (unlinkErr) {
console.log(unlinkErr);
}
});
if (attachErr) {
console.log('Audio upload failed', attachErr);
callback(attachErr);
} else {
console.log('Audio upload completed', attachedDoc.id);
callback(null);
}
}
));
},
// split frames
(callback) => {
const fps = getFps(videoDocument.metadata.streams[0].duration);
console.log('FPS', fps);
ffmpeg()
.input(inputFilename)
.outputOptions([
'-filter:v',
`fps=fps=${fps}`
])
.output(`${framesDirectory}/%0d.jpg`)
.on('progress', (progress) => {
console.log(`Processed ${progress.frames} frames`);
})
.on('error', (err) => {
console.log('split frames', err);
callback(err);
})
.on('end', () => {
callback(null);
})
.run();
},
// persist frames
(callback) => {
const fps = getFps(videoDocument.metadata.streams[0].duration);
const timeBetweenFrame = 1 / eval(fps); // eslint-disable-line no-eval
fs.readdir(framesDirectory, (err, files) => {
const uploadFrames = [];
files.forEach((file) => {
uploadFrames.push((uploadCallback) => {
console.log('Persist', file);
const frameDocument = {
type: 'image',
createdAt: new Date(),
video_id: videoDocument._id,
frame_number: parseInt(file, 10),
frame_timecode: (parseInt(file, 10) - 1) * timeBetweenFrame
};
createDocument(frameDocument, 'image.jpg', 'image/jpeg', `${framesDirectory}/${file}`, uploadCallback);
});
});
// keep track of the number of frames we extracted
videoDocument.frame_count = uploadFrames.length;
async.parallelLimit(uploadFrames, 5, (uploadErr) => {
callback(uploadErr);
});
});
},
// persist the frame count
(callback) => {
mediaStorage.insert(videoDocument, (err, body) => {
if (err) {
callback(err);
} else {
videoDocument._rev = body.rev;
callback(null);
}
});
},
// pick one frame as preview for the video
(callback) => {
fs.readdir(framesDirectory, (err, files) => {
// use the frame in the middle
const candidate = files[Math.ceil(files.length / 2)];
console.log(`Candidate is ${framesDirectory}/${candidate}`);
// convert it to the right size
ffmpeg()
.input(`${framesDirectory}/${candidate}`)
.outputOptions([
`-vf scale=${extractOptions.videoThumbnailSize}:-1`
])
.output(`${workingDirectory.name}/thumbnail.jpg`)
.on('error', (ffErr) => {
console.log('Error processing thumbnail');
callback(ffErr);
})
.on('end', () => {
console.log('End of thumbnail processing');
callback(null);
})
.run();
});
},
// attach it to the video
(callback) => {
console.log('Attaching thumbnail to video');
fs.readFile(`${workingDirectory.name}/thumbnail.jpg`, (err, data) => {
mediaStorage.attachFile(videoDocument, 'thumbnail.jpg', data, 'image/jpeg',
(attachErr, body) => {
console.log('Video thumbnail result', body);
if (attachErr) {
console.log(attachErr.statusCode, attachErr.request);
callback(attachErr);
} else {
callback(null, body);
}
});
});
}], (err) => {
if (err) {
console.log('Waterfall failed with', err);
} else {
console.log('Waterfall completed successfully');
}
console.log('Removing temp directory');
rimraf.sync(workingDirectory.name);
});
function createDocument(frameDocument, attachmentName,
attachmentMimetype, attachmentFile, callback) {
console.log('Persisting', frameDocument.type);
mediaStorage.insert(frameDocument, (err, body) => {
if (err) {
console.log('error saving image', err);
callback(err);
} else {
frameDocument._id = body.id;
frameDocument._rev = body.rev;
console.log('Created new document', frameDocument);
fs.readFile(attachmentFile, (rErr, data) => {
mediaStorage.attachFile(frameDocument, attachmentName, data, attachmentMimetype,
(aErr, aBody) => {
console.log('Upload completed', aBody);
if (aErr) {
console.log(aErr.statusCode, aErr.request);
callback(aErr);
} else {
callback(null);
}
});
});
}
});
}