-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththumbnail.js
54 lines (50 loc) · 1.32 KB
/
thumbnail.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
42
43
44
45
46
47
48
49
50
51
52
53
54
import ffmpeg from "fluent-ffmpeg";
import fs from "fs";
const getVideoThumbnail = (videoFile, videoTimeInSeconds) => {
return new Promise((resolve, reject) => {
if (videoFile.type.match("video")) {
const outputPath = `./thumbnails/${videoFile.name}.jpg`;
ffmpeg(videoFile.path)
.inputOptions(`-ss ${videoTimeInSeconds}`)
.screenshots({
count: 1,
folder: "./thumbnails",
filename: `${videoFile.name}.jpg`,
})
.on("end", () => {
fs.readFile(outputPath, (err, data) => {
if (err) {
reject(err);
} else {
const thumbnail = Buffer.from(data).toString("base64");
resolve(thumbnail);
}
});
})
.on("error", (err) => {
reject(err);
});
} else {
reject("file not valid");
}
});
};
// const videoFile = {
// path: "./Demon Slayer.mkv",
// name: "Demon Slayer.mkv",
// type: "video/mkv",
// };
const videoFile = {
path: "./vid10001-0360.mp4",
name: "vid10001-0360.mp4",
type: "video/mp4",
};
const timestamp = 10; // 10 seconds
getVideoThumbnail(videoFile, timestamp)
.then((thumbnail) => {
// Use the thumbnail image
// console.log(thumbnail);
})
.catch((error) => {
console.error(error);
});