From 84931003eab16ed72da952949b38dbff4e80f44c Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Sat, 2 Feb 2019 23:30:34 +0900 Subject: [PATCH] Fix video thumbnails (#4095) * Fix video thumbnails * Fix import --- src/services/drive/add-file.ts | 2 -- src/services/drive/generate-video-thumbnail.ts | 18 ++++++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/services/drive/add-file.ts b/src/services/drive/add-file.ts index 1d5f31c50..c6fa44a3e 100644 --- a/src/services/drive/add-file.ts +++ b/src/services/drive/add-file.ts @@ -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}`); } diff --git a/src/services/drive/generate-video-thumbnail.ts b/src/services/drive/generate-video-thumbnail.ts index 0ee69be4a..14b3b98f9 100644 --- a/src/services/drive/generate-video-thumbnail.ts +++ b/src/services/drive/generate-video-thumbnail.ts @@ -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 { @@ -15,18 +16,27 @@ export async function GenerateVideoThumbnail(path: string): Promise { 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; }