1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/model/threading/ThumbnailWorker.ts

250 lines
7.8 KiB
TypeScript
Raw Normal View History

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';
2018-07-29 01:58:17 +08:00
export class ThumbnailWorker {
2018-11-05 02:28:32 +08:00
private static imageRenderer: (input: RendererInput) => Promise<void> = null;
private static videoRenderer: (input: RendererInput) => Promise<void> = null;
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;
}
2018-11-05 02:28:32 +08:00
return ThumbnailWorker.imageRenderer(input);
}
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);
}
}
2018-11-05 02:28:32 +08:00
export enum ThumbnailSourceType {
Image, Video
}
export interface RendererInput {
2018-11-05 02:28:32 +08:00
type: ThumbnailSourceType;
mediaPath: string;
size: number;
makeSquare: boolean;
thPath: string;
qualityPriority: boolean;
}
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-24 05:03:21 +08:00
const path = require('path');
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
}
/// console.log(data);
let width = null;
let height = null;
for (let i = 0; i < data.streams.length; i++) {
if (data.streams[i].width) {
width = data.streams[i].width;
height = data.streams[i].height;
break;
}
}
if (!width || !height) {
return reject('Can not read video dimension');
}
const ratio = height / width;
2018-11-05 02:28:32 +08:00
const command: FfmpegCommand = ffmpeg(input.mediaPath);
2018-11-24 05:03:21 +08:00
const fileName = path.basename(input.thPath);
const folder = path.dirname(input.thPath);
let executedCmd = '';
2018-11-05 02:28:32 +08:00
command
2018-11-24 05:03:21 +08:00
.on('start', (cmd) => {
executedCmd = cmd;
})
2018-11-05 02:28:32 +08:00
.on('end', () => {
resolve();
})
.on('error', (e) => {
2018-11-24 05:03:21 +08:00
reject(e.toString() + ' executed: ' + executedCmd);
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({
2018-11-24 05:03:21 +08:00
timemarks: ['10%'], size: newWidth + 'x?', filename: fileName, folder: folder
2018-11-05 02:28:32 +08:00
});
} else {
command.takeScreenshots({
2018-11-24 05:03:21 +08:00
timemarks: ['10%'], size: input.size + 'x' + input.size, filename: fileName, folder: folder
2018-11-05 02:28:32 +08:00
});
}
});
});
};
}
}
export class ImageRendererFactory {
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();
case ThumbnailProcessingLib.gm:
2018-11-05 02:28:32 +08:00
return ImageRendererFactory.Gm();
case ThumbnailProcessingLib.sharp:
2018-11-05 02:28:32 +08:00
return ImageRendererFactory.Sharp();
}
throw new Error('unknown renderer');
}
public static Jimp() {
2018-03-31 03:30:30 +08:00
const Jimp = require('jimp');
return async (input: RendererInput): Promise<void> => {
// generate thumbnail
2018-11-05 02:28:32 +08:00
Logger.silly('[JimpThRenderer] rendering thumbnail:' + input.mediaPath);
const image = await Jimp.read(input.mediaPath);
/**
* 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;
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);
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');
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);
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;
const kernel = input.qualityPriority === true ? sharp.kernel.lanczos3 : sharp.kernel.nearest;
if (input.makeSquare === false) {
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
});
} else {
image
.resize(input.size, input.size, {
kernel: kernel,
position: sharp.gravity.centre,
fit: 'cover'
});
}
await image.jpeg().toFile(input.thPath);
};
}
public static Gm() {
2018-03-31 03:30:30 +08:00
const gm = require('gm');
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);
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;
const filter = input.qualityPriority === true ? 'Lanczos' : 'Point';
image.filter(filter);
if (input.makeSquare === false) {
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);
}
image.write(input.thPath, (e) => {
if (e) {
return reject(e);
}
return resolve();
});
} catch (err) {
return reject(err);
}
});
});
};
}
}