Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix video thumbnails #4095

Merged
merged 2 commits into from
Feb 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/services/drive/add-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down
18 changes: 14 additions & 4 deletions src/services/drive/generate-video-thumbnail.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'fs';
import * as tmp from 'tmp';
import sharp = require('sharp');
acid-chicken marked this conversation as resolved.
Show resolved Hide resolved
const ThumbnailGenerator = require('video-thumbnail-generator').default;

export async function GenerateVideoThumbnail(path: string): Promise<Buffer> {
Expand All @@ -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;
}