-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-images.js
More file actions
40 lines (37 loc) · 1.58 KB
/
build-images.js
File metadata and controls
40 lines (37 loc) · 1.58 KB
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
import { execSync } from "child_process";
import { readdirSync, accessSync, constants } from "fs"
const baseFolder = "./posts/"
function checkFileExists(path) {
console.log("Checking file", path);
try {
accessSync(path, constants.F_OK);
console.log("File exists", path);
return true;
} catch (err) {
console.log(`File ${path} does not exist`);
return false;
}
}
function createImage(fromPath, toPath, scale = [1980, 1080]) {
try {
execSync(`ffmpeg -i ${fromPath} -an -vf "fps=15,scale=${scale.join(":")}" \ -c:v libwebp -lossless 0 -q:v 75 -loop 0 ${toPath}`)
console.log(`Created image ${toPath} from ${fromPath}`)
} catch (e) {
console.log(`Something went wrong, when generating ${toPath} from ${fromPath}`);
}
}
export default function buildImages() {
readdirSync(baseFolder, { withFileTypes: true })
.filter(f => f.isDirectory())
.forEach(folder => {
const id = folder.name;
const filePath = `./${baseFolder}${id}/${id}`;
try {
if (!checkFileExists(`${filePath}.webp`)) createImage(`${filePath}.webm`, `${filePath}.webp`, [1980, 1080])
if (!checkFileExists(`${filePath}_medium.webp`)) createImage(`${filePath}.webm`, `${filePath}_medium.webp`, [990, 540])
if (!checkFileExists(`${filePath}_small.webp`)) createImage(`${filePath}.webm`, `${filePath}_small.webp`, [293, 160])
} catch (e) {
console.log("Something went wrong, while trying to generate images");
}
})
}