Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/lib/exif.js
1126 views
1
/**
2
* Create By Mhankbarbar
3
* Contact -
4
* Follow https://github.com/MhankBarBar
5
*/
6
7
8
const fs = require('fs')
9
const { tmpdir } = require("os")
10
const Crypto = require("crypto")
11
const ff = require('fluent-ffmpeg')
12
const webp = require("node-webpmux")
13
const path = require("path")
14
15
16
async function imageToWebp (media) {
17
18
const tmpFileOut = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
19
const tmpFileIn = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.jpg`)
20
21
fs.writeFileSync(tmpFileIn, media)
22
23
await new Promise((resolve, reject) => {
24
ff(tmpFileIn)
25
.on("error", reject)
26
.on("end", () => resolve(true))
27
.addOutputOptions([
28
"-vcodec",
29
"libwebp",
30
"-vf",
31
"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"
32
])
33
.toFormat("webp")
34
.save(tmpFileOut)
35
})
36
37
const buff = fs.readFileSync(tmpFileOut)
38
fs.unlinkSync(tmpFileOut)
39
fs.unlinkSync(tmpFileIn)
40
return buff
41
}
42
43
async function videoToWebp (media) {
44
45
const tmpFileOut = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
46
const tmpFileIn = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.mp4`)
47
48
fs.writeFileSync(tmpFileIn, media)
49
50
await new Promise((resolve, reject) => {
51
ff(tmpFileIn)
52
.on("error", reject)
53
.on("end", () => resolve(true))
54
.addOutputOptions([
55
"-vcodec",
56
"libwebp",
57
"-vf",
58
"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",
59
"-loop",
60
"0",
61
"-ss",
62
"00:00:00",
63
"-t",
64
"00:00:05",
65
"-preset",
66
"default",
67
"-an",
68
"-vsync",
69
"0"
70
])
71
.toFormat("webp")
72
.save(tmpFileOut)
73
})
74
75
const buff = fs.readFileSync(tmpFileOut)
76
fs.unlinkSync(tmpFileOut)
77
fs.unlinkSync(tmpFileIn)
78
return buff
79
}
80
81
async function writeExifImg (media, metadata) {
82
let wMedia = await imageToWebp(media)
83
const tmpFileIn = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
84
const tmpFileOut = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
85
fs.writeFileSync(tmpFileIn, wMedia)
86
87
if (metadata.packname || metadata.author) {
88
const img = new webp.Image()
89
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 : [""] }
90
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])
91
const jsonBuff = Buffer.from(JSON.stringify(json), "utf-8")
92
const exif = Buffer.concat([exifAttr, jsonBuff])
93
exif.writeUIntLE(jsonBuff.length, 14, 4)
94
await img.load(tmpFileIn)
95
fs.unlinkSync(tmpFileIn)
96
img.exif = exif
97
await img.save(tmpFileOut)
98
return tmpFileOut
99
}
100
}
101
102
async function writeExifVid (media, metadata) {
103
let wMedia = await videoToWebp(media)
104
const tmpFileIn = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
105
const tmpFileOut = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
106
fs.writeFileSync(tmpFileIn, wMedia)
107
108
if (metadata.packname || metadata.author) {
109
const img = new webp.Image()
110
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 : [""] }
111
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])
112
const jsonBuff = Buffer.from(JSON.stringify(json), "utf-8")
113
const exif = Buffer.concat([exifAttr, jsonBuff])
114
exif.writeUIntLE(jsonBuff.length, 14, 4)
115
await img.load(tmpFileIn)
116
fs.unlinkSync(tmpFileIn)
117
img.exif = exif
118
await img.save(tmpFileOut)
119
return tmpFileOut
120
}
121
}
122
123
async function writeExif (media, metadata) {
124
let wMedia = /webp/.test(media.mimetype) ? media.data : /image/.test(media.mimetype) ? await imageToWebp(media.data) : /video/.test(media.mimetype) ? await videoToWebp(media.data) : ""
125
const tmpFileIn = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
126
const tmpFileOut = path.join(tmpdir(), `${Crypto.randomBytes(6).readUIntLE(0, 6).toString(36)}.webp`)
127
fs.writeFileSync(tmpFileIn, wMedia)
128
129
if (metadata.packname || metadata.author) {
130
const img = new webp.Image()
131
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 : [""] }
132
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])
133
const jsonBuff = Buffer.from(JSON.stringify(json), "utf-8")
134
const exif = Buffer.concat([exifAttr, jsonBuff])
135
exif.writeUIntLE(jsonBuff.length, 14, 4)
136
await img.load(tmpFileIn)
137
fs.unlinkSync(tmpFileIn)
138
img.exif = exif
139
await img.save(tmpFileOut)
140
return tmpFileOut
141
}
142
}
143
144
module.exports = { imageToWebp, videoToWebp, writeExifImg, writeExifVid, writeExif }
145
146