mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.6 KiB
42 lines
1.6 KiB
const gm = require('gm')
|
|
, ffmpeg = require('fluent-ffmpeg')
|
|
, { thumbSize, ffmpegGifThumbnails } = require(__dirname+'/../../configs/main.js')
|
|
, uploadDirectory = require(__dirname+'/uploadDirectory.js');
|
|
|
|
module.exports = (file, firstFrameOnly=true) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
if (ffmpegGifThumbnails && !firstFrameOnly) {
|
|
const thumbSizeFilter = file.geometry.width > file.geometry.height ? `${thumbSize}:-1` : `-1:${thumbSize}`;
|
|
const complexFilters = [
|
|
/* this complex filter scales (resizes), and works some magic to preserve transparency,
|
|
with an additional filter to reduce the aliasing on outlines of transparent parts. */
|
|
`[0:v] scale=${thumbSizeFilter},split [a][b]; [a] palettegen=reserve_transparent=on:transparency_color=ffffff [p]; [b][p] paletteuse`
|
|
];
|
|
ffmpeg(`${uploadDirectory}/file/${file.filename}`)
|
|
.on('end', () => {
|
|
return resolve();
|
|
})
|
|
.on('error', function(err, stdout, stderr) {
|
|
return reject(err);
|
|
})
|
|
.complexFilter(complexFilters)
|
|
.save(`${uploadDirectory}/file/thumb-${file.hash}${file.thumbextension}`);
|
|
} else {
|
|
//[0] for first frame (gifs, etc)
|
|
const thumbnailing = gm(`${uploadDirectory}/file/${file.filename}${firstFrameOnly ? '[0]' : ''}`);
|
|
if (!firstFrameOnly) {
|
|
//try (and fail) to make gm thumbnailing less shit.
|
|
thumbnailing.coalesce();
|
|
}
|
|
thumbnailing.resize(Math.min(thumbSize, file.geometry.width), Math.min(thumbSize, file.geometry.height))
|
|
.write(`${uploadDirectory}/file/thumb-${file.hash}${file.thumbextension}`, function (err) {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
return resolve();
|
|
});
|
|
}
|
|
});
|
|
|
|
};
|
|
|