-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite-plugin-generate-thumbnails.js
41 lines (36 loc) · 1.2 KB
/
vite-plugin-generate-thumbnails.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { resolve } from 'path';
import ffmpeg from 'fluent-ffmpeg';
import fs from 'node:fs';
function generateThumbnails() {
const videoDir = resolve('public/videos');
const thumbnailDir = resolve('public/thumbnails');
const videos = fs.readdirSync(videoDir).filter(file => file.endsWith('.mp4'));
if (!fs.existsSync(thumbnailDir)) {
console.error(`${thumbnailDir}: Does not exist`, err);
return
}
videos.forEach(video => {
const videoPath = resolve(videoDir, video);
ffmpeg(videoPath)
.screenshots({
timestamps: ['600'], // 10 minutes
filename: `${video.split('.mp4')[0]}_thumbnail.png`,
folder: thumbnailDir,
size: '1280x720'
})
.on('end', () => {
console.log(`Generated thumbnail for ${video}`);
})
.on('error', err => {
console.error(`Error generating thumbnail for ${video}:`, err);
});
});
}
export default function vitePluginGenerateThumbnails() {
return {
name: 'vite-plugin-generate-thumbnails',
buildStart() {
generateThumbnails();
}
};
}