-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen-thumbs.ts
73 lines (56 loc) · 1.95 KB
/
gen-thumbs.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {
ImageMagick,
IMagickImage,
initialize,
initializeImageMagick,
MagickFormat,
} from "imagemagick";
import { walk } from "https://deno.land/std/fs/mod.ts";
import { ffmpeg } from "https://deno.land/x/[email protected]/ffmpeg.ts";
// await caches.delete("magick_native");
await initialize();
const asyncGenDirectories = await walk(`thumbs`, {
maxDepth: 3,
includeDirs: true,
includeFiles: false,
match: [`[^/]+/`],
});
const paths = [];
for await (let item of asyncGenDirectories) {
paths.push(item.path);
}
for await (let path of paths) {
await Deno.rename(path, path.replaceAll(' ', '_'));
}
const asyncGen = await walk(`thumbs`, {
maxDepth: 3,
includeDirs: false,
includeFiles: true,
match: [`^.+\\.(jpg|jpeg|png|gif|webp|bmp|mp4|mov|tiff)$`],
});
for await (let item of asyncGen) {
const tempFilePath = item.path;
if(tempFilePath.includes('mp4') || tempFilePath.includes('mov')) {
const thumbName = item.path.replace(item.name, '') + 'thumb-' + item.name.split('.')[0] + '.jpeg';
const cmd = [
'/Users/adamgasiorek/Dev/ffmpeg/ffmpeg', '-i', item.path, '-ss', '00:00:00.000', '-frames:v', '1', thumbName
];
await Deno.run({ cmd: cmd }).status();
} else {
const data = await Deno.readFile(tempFilePath);
await Deno.remove(item.path);
await ImageMagick.read(new Uint8Array(data), async (img: IMagickImage) => {
await img.write(
MagickFormat.Jpeg,
(data: Uint8Array) =>
Deno.writeFile(item.path.replace(item.name, '') + item.name.split('.')[0] + '.jpeg', data ),
);
img.resize(500);
await img.write(
MagickFormat.Jpeg,
(data: Uint8Array) =>
Deno.writeFile(item.path.replace(item.name, '') + 'thumb-' + item.name.split('.')[0] + '.jpeg', data ),
);
});
}
}