Fix video thumbnails (#4095)

* Fix video thumbnails

* Fix import
This commit is contained in:
MeiMei 2019-02-02 23:30:34 +09:00 committed by syuilo
parent 2b0cb6d728
commit 84931003ea
2 changed files with 14 additions and 6 deletions

View file

@ -122,8 +122,6 @@ async function save(path: string, name: string, type: string, hash: string, size
} else if (type.startsWith('video/')) {
try {
thumbnail = await GenerateVideoThumbnail(path);
thumbnailExt = 'png';
thumbnailType = 'image/png';
} catch (e) {
console.log(`GenerateVideoThumbnail failed: ${e}`);
}

View file

@ -1,5 +1,6 @@
import * as fs from 'fs';
import * as tmp from 'tmp';
import * as sharp from 'sharp';
const ThumbnailGenerator = require('video-thumbnail-generator').default;
export async function GenerateVideoThumbnail(path: string): Promise<Buffer> {
@ -15,18 +16,27 @@ export async function GenerateVideoThumbnail(path: string): Promise<Buffer> {
thumbnailPath: outDir,
});
await tg.generateOneByPercent(10, {
size: '498x280',
await tg.generateOneByPercent(5, {
size: '100%',
filename: 'output.png',
});
const outPath = `${outDir}/output.png`;
const buffer = fs.readFileSync(outPath);
const thumbnail = await sharp(outPath)
.resize(498, 280, {
fit: 'inside',
withoutEnlargement: true
})
.jpeg({
quality: 85,
progressive: true
})
.toBuffer();
// cleanup
fs.unlinkSync(outPath);
cleanup();
return buffer;
return thumbnail;
}