/
video-thumbnail.ts
59 lines (50 loc) · 1.77 KB
/
video-thumbnail.ts
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
/*
* Copyright ©️ 2019 GaltProject Society Construction and Terraforming Company
* (Founded by [Nikolai Popeka](https://github.com/npopeka)
*
* Copyright ©️ 2019 Galt•Core Blockchain Company
* (Founded by [Nikolai Popeka](https://github.com/npopeka) by
* [Basic Agreement](ipfs/QmaCiXUmSrP16Gz8Jdzq6AJESY1EAANmmwha15uR3c1bsS)).
*/
import {DriverInput, OutputSize} from "../interface";
import {Stream} from "stream";
import AbstractDriver from "../abstractDriver";
const ffmpeg = require('fluent-ffmpeg');
const _ = require('lodash');
const stream = require('stream');
export class VideoThumbnail extends AbstractDriver {
supportedInputs = [DriverInput.Stream];
supportedOutputSizes = [OutputSize.Small, OutputSize.Medium, OutputSize.Large];
async processByStream(inputStream, options: any = {}) {
const transformStream = new stream.Transform();
transformStream._transform = function (chunk,encoding,done) {
this.push(chunk);
done();
};
ffmpeg()
.input(inputStream)
.inputFormat(options.extension)
.outputOptions(["-movflags faststart", '-f image2', '-vframes 1', '-vcodec png', '-f rawvideo', '-ss 00:00:05'])//, '-s 320x240'
.output(transformStream)
.on('error', function (err, stdout, stderr) {
console.error('An error occurred: ' + err.message, err, stderr);
options.onError && options.onError(err);
})
.run();
return {
stream: transformStream,
type: 'image/png',
extension: 'png'
};
}
async isInputExtensionSupported(extension) {
return (new Promise((resolve, reject) => {
ffmpeg.getAvailableFormats(function(err, formats) {
if(err) {
reject(err);
}
resolve(!!formats[extension]);
});
})) as any;
}
}