Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/lib/exif.js
2591 views
1
//═══════════════════════════════════════════════════════//
2
//If you want to recode, reupload
3
//or copy the codes/script,
4
//pls give credit
5
//no credit? i will take action immediately
6
//© 2022 Xeon Bot Inc. Cheems Bot MD
7
//Thank you to Lord Buddha, Family and Myself
8
//════════════════════════════//
9
const fs = require('fs')
10
const { tmpdir } = require("os")
11
const Crypto = require("crypto")
12
const ff = require('fluent-ffmpeg')
13
const webp = require("node-webpmux")
14
const path = require("path")
15
16
17
async function imageToWebp (media) {
18
19
const tmpFileOut = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
20
const tmpFileIn = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.jpg`)
21
22
fs.writeFileSync(tmpFileIn, media)
23
24
await new Promise((resolve, reject) => {
25
ff(tmpFileIn)
26
.on("error", reject)
27
.on("end", () => resolve(true))
28
.addOutputOptions([
29
"-vcodec",
30
"libwebp",
31
"-vf",
32
"scale='min(320,iw)':min'(320,ih)':force_original_aspect_ratio=decrease,fps=15, pad=320:320:-1:-1:[email protected], split [a][b]; [a] palettegen=reserve_transparent=on:transparency_color=ffffff [p]; [b][p] paletteuse"
33
])
34
.toFormat("webp")
35
.save(tmpFileOut)
36
})
37
38
const buff = fs.readFileSync(tmpFileOut)
39
fs.unlinkSync(tmpFileOut)
40
fs.unlinkSync(tmpFileIn)
41
return buff
42
}
43
44
async function videoToWebp (media) {
45
46
const tmpFileOut = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
47
const tmpFileIn = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.mp4`)
48
49
fs.writeFileSync(tmpFileIn, media)
50
51
await new Promise((resolve, reject) => {
52
ff(tmpFileIn)
53
.on("error", reject)
54
.on("end", () => resolve(true))
55
.addOutputOptions([
56
"-vcodec",
57
"libwebp",
58
"-vf",
59
"scale='min(320,iw)':min'(320,ih)':force_original_aspect_ratio=decrease,fps=15, pad=320:320:-1:-1:[email protected], split [a][b]; [a] palettegen=reserve_transparent=on:transparency_color=ffffff [p]; [b][p] paletteuse",
60
"-loop",
61
"0",
62
"-ss",
63
"00:00:00",
64
"-t",
65
"00:00:05",
66
"-preset",
67
"default",
68
"-an",
69
"-vsync",
70
"0"
71
])
72
.toFormat("webp")
73
.save(tmpFileOut)
74
})
75
76
const buff = fs.readFileSync(tmpFileOut)
77
fs.unlinkSync(tmpFileOut)
78
fs.unlinkSync(tmpFileIn)
79
return buff
80
}
81
82
async function writeExifImg (media, metadata) {
83
let wMedia = await imageToWebp(media)
84
const tmpFileIn = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
85
const tmpFileOut = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
86
fs.writeFileSync(tmpFileIn, wMedia)
87
88
if (metadata.packname || metadata.author) {
89
const img = new webp.Image()
90
const json = { "sticker-pack-id": `https://github.com/DikaArdnt/Hisoka-Morou`, "sticker-pack-name": metadata.packname, "sticker-pack-publisher": metadata.author, "emojis": metadata.categories ? metadata.categories : [""] }
91
const exifAttr = Buffer.from([0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00])
92
const jsonBuff = Buffer.from(JSON.stringify(json), "utf-8")
93
const exif = Buffer.concat([exifAttr, jsonBuff])
94
exif.writeUIntLE(jsonBuff.length, 14, 4)
95
await img.load(tmpFileIn)
96
fs.unlinkSync(tmpFileIn)
97
img.exif = exif
98
await img.save(tmpFileOut)
99
return tmpFileOut
100
}
101
}
102
103
async function writeExifVid (media, metadata) {
104
let wMedia = await videoToWebp(media)
105
const tmpFileIn = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
106
const tmpFileOut = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
107
fs.writeFileSync(tmpFileIn, wMedia)
108
109
if (metadata.packname || metadata.author) {
110
const img = new webp.Image()
111
const json = { "sticker-pack-id": `https://github.com/DikaArdnt/Hisoka-Morou`, "sticker-pack-name": metadata.packname, "sticker-pack-publisher": metadata.author, "emojis": metadata.categories ? metadata.categories : [""] }
112
const exifAttr = Buffer.from([0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00])
113
const jsonBuff = Buffer.from(JSON.stringify(json), "utf-8")
114
const exif = Buffer.concat([exifAttr, jsonBuff])
115
exif.writeUIntLE(jsonBuff.length, 14, 4)
116
await img.load(tmpFileIn)
117
fs.unlinkSync(tmpFileIn)
118
img.exif = exif
119
await img.save(tmpFileOut)
120
return tmpFileOut
121
}
122
}
123
124
async function writeExif (media, metadata) {
125
let wMedia = /webp/.test(media.mimetype) ? media.data : /image/.test(media.mimetype) ? await imageToWebp(media.data) : /video/.test(media.mimetype) ? await videoToWebp(media.data) : ""
126
const tmpFileIn = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
127
const tmpFileOut = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
128
fs.writeFileSync(tmpFileIn, wMedia)
129
130
if (metadata.packname || metadata.author) {
131
const img = new webp.Image()
132
const json = { "sticker-pack-id": `https://github.com/DikaArdnt/Hisoka-Morou`, "sticker-pack-name": metadata.packname, "sticker-pack-publisher": metadata.author, "emojis": metadata.categories ? metadata.categories : [""] }
133
const exifAttr = Buffer.from([0x49, 0x49, 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01, 0x00, 0x41, 0x57, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00])
134
const jsonBuff = Buffer.from(JSON.stringify(json), "utf-8")
135
const exif = Buffer.concat([exifAttr, jsonBuff])
136
exif.writeUIntLE(jsonBuff.length, 14, 4)
137
await img.load(tmpFileIn)
138
fs.unlinkSync(tmpFileIn)
139
img.exif = exif
140
await img.save(tmpFileOut)
141
return tmpFileOut
142
}
143
}
144
145
module.exports = { imageToWebp, videoToWebp, writeExifImg, writeExifVid, writeExif }
146
147