2018-10-22 06:24:17 +08:00
|
|
|
import {Metadata, Sharp} from 'sharp';
|
2018-03-31 03:30:30 +08:00
|
|
|
import {Dimensions, State} from 'gm';
|
|
|
|
import {Logger} from '../../Logger';
|
2018-11-05 02:28:32 +08:00
|
|
|
import {FfmpegCommand, FfprobeData} from 'fluent-ffmpeg';
|
2018-03-31 03:30:30 +08:00
|
|
|
import {ThumbnailProcessingLib} from '../../../common/config/private/IPrivateConfig';
|
2018-11-19 03:26:29 +08:00
|
|
|
import {FFmpegFactory} from '../FFmpegFactory';
|
2017-07-04 16:24:20 +08:00
|
|
|
|
2018-07-29 01:58:17 +08:00
|
|
|
export class ThumbnailWorker {
|
2017-07-04 16:24:20 +08:00
|
|
|
|
2018-11-05 02:28:32 +08:00
|
|
|
private static imageRenderer: (input: RendererInput) => Promise<void> = null;
|
|
|
|
private static videoRenderer: (input: RendererInput) => Promise<void> = null;
|
2017-07-04 16:24:20 +08:00
|
|
|
private static rendererType = null;
|
|
|
|
|
|
|
|
public static render(input: RendererInput, renderer: ThumbnailProcessingLib): Promise<void> {
|
2018-11-05 02:28:32 +08:00
|
|
|
if (input.type === ThumbnailSourceType.Image) {
|
|
|
|
return this.renderFromImage(input, renderer);
|
|
|
|
}
|
|
|
|
return this.renderFromVideo(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static renderFromImage(input: RendererInput, renderer: ThumbnailProcessingLib): Promise<void> {
|
2018-07-29 01:58:17 +08:00
|
|
|
if (ThumbnailWorker.rendererType !== renderer) {
|
2018-11-05 02:28:32 +08:00
|
|
|
ThumbnailWorker.imageRenderer = ImageRendererFactory.build(renderer);
|
2018-07-29 01:58:17 +08:00
|
|
|
ThumbnailWorker.rendererType = renderer;
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
2018-11-05 02:28:32 +08:00
|
|
|
return ThumbnailWorker.imageRenderer(input);
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-05 02:28:32 +08:00
|
|
|
public static renderFromVideo(input: RendererInput): Promise<void> {
|
|
|
|
if (ThumbnailWorker.videoRenderer === null) {
|
|
|
|
ThumbnailWorker.videoRenderer = VideoRendererFactory.build();
|
|
|
|
}
|
|
|
|
return ThumbnailWorker.videoRenderer(input);
|
|
|
|
}
|
|
|
|
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
|
|
|
|
2018-11-05 02:28:32 +08:00
|
|
|
export enum ThumbnailSourceType {
|
|
|
|
Image, Video
|
|
|
|
}
|
2017-07-04 16:24:20 +08:00
|
|
|
|
|
|
|
export interface RendererInput {
|
2018-11-05 02:28:32 +08:00
|
|
|
type: ThumbnailSourceType;
|
|
|
|
mediaPath: string;
|
2017-07-04 16:24:20 +08:00
|
|
|
size: number;
|
|
|
|
makeSquare: boolean;
|
|
|
|
thPath: string;
|
2018-05-13 00:19:51 +08:00
|
|
|
qualityPriority: boolean;
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
|
|
|
|
2018-11-05 02:28:32 +08:00
|
|
|
export class VideoRendererFactory {
|
|
|
|
public static build(): (input: RendererInput) => Promise<void> {
|
2018-11-19 03:26:29 +08:00
|
|
|
const ffmpeg = FFmpegFactory.get();
|
2018-11-05 02:28:32 +08:00
|
|
|
return (input: RendererInput): Promise<void> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
Logger.silly('[FFmpeg] rendering thumbnail: ' + input.mediaPath);
|
|
|
|
|
|
|
|
ffmpeg(input.mediaPath).ffprobe((err: any, data: FfprobeData) => {
|
|
|
|
if (!!err || data === null) {
|
2018-11-19 05:30:41 +08:00
|
|
|
return reject(err.toString());
|
2018-11-05 02:28:32 +08:00
|
|
|
}
|
|
|
|
const ratio = data.streams[0].height / data.streams[0].width;
|
|
|
|
const command: FfmpegCommand = ffmpeg(input.mediaPath);
|
|
|
|
command
|
|
|
|
.on('end', () => {
|
|
|
|
resolve();
|
|
|
|
})
|
|
|
|
.on('error', (e) => {
|
2018-11-19 05:30:41 +08:00
|
|
|
reject(e.toString());
|
2018-11-05 02:28:32 +08:00
|
|
|
})
|
|
|
|
.outputOptions(['-qscale:v 4']);
|
|
|
|
if (input.makeSquare === false) {
|
|
|
|
const newWidth = Math.round(Math.sqrt((input.size * input.size) / ratio));
|
|
|
|
command.takeScreenshots({
|
|
|
|
timemarks: ['10%'], size: newWidth + 'x?', filename: input.thPath
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
command.takeScreenshots({
|
|
|
|
timemarks: ['10%'], size: input.size + 'x' + input.size, filename: input.thPath
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ImageRendererFactory {
|
2017-07-04 16:24:20 +08:00
|
|
|
|
|
|
|
public static build(renderer: ThumbnailProcessingLib): (input: RendererInput) => Promise<void> {
|
|
|
|
switch (renderer) {
|
|
|
|
case ThumbnailProcessingLib.Jimp:
|
2018-11-05 02:28:32 +08:00
|
|
|
return ImageRendererFactory.Jimp();
|
2017-07-04 16:24:20 +08:00
|
|
|
case ThumbnailProcessingLib.gm:
|
2018-11-05 02:28:32 +08:00
|
|
|
return ImageRendererFactory.Gm();
|
2017-07-04 16:24:20 +08:00
|
|
|
case ThumbnailProcessingLib.sharp:
|
2018-11-05 02:28:32 +08:00
|
|
|
return ImageRendererFactory.Sharp();
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
2018-05-13 00:19:51 +08:00
|
|
|
throw new Error('unknown renderer');
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static Jimp() {
|
2018-03-31 03:30:30 +08:00
|
|
|
const Jimp = require('jimp');
|
2017-07-04 16:24:20 +08:00
|
|
|
return async (input: RendererInput): Promise<void> => {
|
2018-05-13 00:19:51 +08:00
|
|
|
// generate thumbnail
|
2018-11-05 02:28:32 +08:00
|
|
|
Logger.silly('[JimpThRenderer] rendering thumbnail:' + input.mediaPath);
|
|
|
|
const image = await Jimp.read(input.mediaPath);
|
2017-07-04 16:24:20 +08:00
|
|
|
/**
|
|
|
|
* newWidth * newHeight = size*size
|
|
|
|
* newHeight/newWidth = height/width
|
|
|
|
*
|
|
|
|
* newHeight = (height/width)*newWidth
|
|
|
|
* newWidth * newWidth = (size*size) / (height/width)
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
const ratio = image.bitmap.height / image.bitmap.width;
|
2018-05-13 00:19:51 +08:00
|
|
|
const algo = input.qualityPriority === true ? Jimp.RESIZE_BEZIER : Jimp.RESIZE_NEAREST_NEIGHBOR;
|
|
|
|
if (input.makeSquare === false) {
|
|
|
|
const newWidth = Math.sqrt((input.size * input.size) / ratio);
|
2017-07-04 16:24:20 +08:00
|
|
|
|
|
|
|
image.resize(newWidth, Jimp.AUTO, algo);
|
|
|
|
} else {
|
|
|
|
image.resize(input.size / Math.min(ratio, 1), Jimp.AUTO, algo);
|
|
|
|
image.crop(0, 0, input.size, input.size);
|
|
|
|
}
|
|
|
|
image.quality(60); // set JPEG quality
|
|
|
|
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
image.write(input.thPath, (err) => { // save
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Sharp() {
|
2018-03-31 03:30:30 +08:00
|
|
|
const sharp = require('sharp');
|
2017-07-04 16:24:20 +08:00
|
|
|
return async (input: RendererInput): Promise<void> => {
|
|
|
|
|
2018-11-05 02:28:32 +08:00
|
|
|
Logger.silly('[SharpThRenderer] rendering thumbnail:' + input.mediaPath);
|
|
|
|
const image: Sharp = sharp(input.mediaPath);
|
2017-07-04 16:24:20 +08:00
|
|
|
const metadata: Metadata = await image.metadata();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* newWidth * newHeight = size*size
|
|
|
|
* newHeight/newWidth = height/width
|
|
|
|
*
|
|
|
|
* newHeight = (height/width)*newWidth
|
|
|
|
* newWidth * newWidth = (size*size) / (height/width)
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
const ratio = metadata.height / metadata.width;
|
2018-05-13 00:19:51 +08:00
|
|
|
const kernel = input.qualityPriority === true ? sharp.kernel.lanczos3 : sharp.kernel.nearest;
|
|
|
|
if (input.makeSquare === false) {
|
2017-07-04 16:24:20 +08:00
|
|
|
const newWidth = Math.round(Math.sqrt((input.size * input.size) / ratio));
|
|
|
|
image.resize(newWidth, null, {
|
2018-01-16 10:12:51 +08:00
|
|
|
kernel: kernel
|
2017-07-04 16:24:20 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
image
|
|
|
|
.resize(input.size, input.size, {
|
2018-10-22 06:24:17 +08:00
|
|
|
kernel: kernel,
|
|
|
|
position: sharp.gravity.centre,
|
|
|
|
fit: 'cover'
|
|
|
|
});
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
|
|
|
await image.jpeg().toFile(input.thPath);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Gm() {
|
2018-03-31 03:30:30 +08:00
|
|
|
const gm = require('gm');
|
2017-07-04 16:24:20 +08:00
|
|
|
return (input: RendererInput): Promise<void> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-11-05 02:28:32 +08:00
|
|
|
Logger.silly('[GMThRenderer] rendering thumbnail:' + input.mediaPath);
|
|
|
|
let image: State = gm(input.mediaPath);
|
2017-07-04 16:24:20 +08:00
|
|
|
image.size((err, value: Dimensions) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* newWidth * newHeight = size*size
|
|
|
|
* newHeight/newWidth = height/width
|
|
|
|
*
|
|
|
|
* newHeight = (height/width)*newWidth
|
|
|
|
* newWidth * newWidth = (size*size) / (height/width)
|
|
|
|
*
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
const ratio = value.height / value.width;
|
2018-05-13 00:19:51 +08:00
|
|
|
const filter = input.qualityPriority === true ? 'Lanczos' : 'Point';
|
2017-07-04 16:24:20 +08:00
|
|
|
image.filter(filter);
|
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
if (input.makeSquare === false) {
|
2017-07-04 16:24:20 +08:00
|
|
|
const newWidth = Math.round(Math.sqrt((input.size * input.size) / ratio));
|
|
|
|
image = image.resize(newWidth);
|
|
|
|
} else {
|
|
|
|
image = image.resize(input.size, input.size)
|
|
|
|
.crop(input.size, input.size);
|
|
|
|
}
|
2018-05-13 00:19:51 +08:00
|
|
|
image.write(input.thPath, (e) => {
|
|
|
|
if (e) {
|
|
|
|
return reject(e);
|
2017-07-04 16:24:20 +08:00
|
|
|
}
|
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|