forked from SamPandey001/Secktor-Md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conv.js
188 lines (159 loc) · 4.57 KB
/
conv.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const ffmpeg = require('fluent-ffmpeg')
const {
randomBytes
} = require('crypto')
const fs = require('fs')
const {
isUrl
} = require('.')
const {
getHttpStream,
toBuffer
} = require('@sampandey001/baileys')
const sharp = require('sharp')
const {
spawn
} = require('child_process')
const path = require('path')
const {
fromBuffer
} = require('file-type')
/**
* mboh radong
* @param {Buffer} data video mp4 buffer
* @returns {Promise<Buffer} webp Buffer
*/
async function toGif(data) {
try {
const input = `./${randomBytes(3).toString('hex')}.webp`
const output = `./${randomBytes(3).toString('hex')}.gif`
fs.writeFileSync(input, data.toString('binary'), 'binary')
const file = await new Promise((resolve) => {
spawn(`convert`, [
input,
output
]).on('error', (err) => {
throw err
}).on('exit', () => resolve(output))
})
let result = fs.readFileSync(file)
try {
fs.unlinkSync(input)
fs.unlinkSync(output)
} catch (error) {
console.log(error);
}
return result
} catch (error) {
console.log(error);
}
}
async function toMp4(data) {
try {
let inPath = `./${randomBytes(3).toString('hex')}.gif`
const input = fs.existsSync(data) ? data : save(data, inPath)
const output = `./${randomBytes(3).toString('hex')}.mp4`
const file = await new Promise((resolve) => {
ffmpeg(input).outputOptions(["-pix_fmt yuv420p", "-c:v libx264", "-movflags +faststart", "-filter:v crop='floor(in_w/2)*2:floor(in_h/2)*2'", ]).toFormat('mp4').noAudio().save(output).on('exit', () => resolve(output))
})
let result = await fs.promises.readFile(file)
return result
} catch (error) {
console.log(error);
}
}
/**
* mboh radong
* @param {Buffer|URL|string} data video mp4 buffer | url | path
* @returns {Promise<string>} file path
*/
async function toAudio(data) {
return new Promise(async (resolve, reject) => {
try {
const get = await toBuffer(await getHttpStream(data))
const inputPath = `./video_${randomBytes(3).toString('hex')}.${(await fromBuffer(get)).ext}`
const input = Buffer.isBuffer(data) ? save(data, inputPath) : fs.existsSync(data) ? data : isUrl(data) ? save(get, inputPath) : data
const output = `./${randomBytes(3).toString('hex')}.mp3`
const file = await new Promise((resolve) => {
ffmpeg(input).audioFrequency(44100).audioChannels(2).audioBitrate('128k').audioCodec('libmp3lame').audioQuality(5).toFormat('mp3').save(output).on('end', () => resolve(output))
})
resolve(file)
} catch (error) {
console.log(error);
}
})
}
/**
* convert mp3 to 8D Audio
* @param {string|Buffer} input
* @returns
*/
const EightD = async (input) => {
const inputPath = `./${randomBytes(3).toString('hex')}.mp3`
input = Buffer.isBuffer(input) ? save(input, inputPath) : input
const output = `./${randomBytes(3).toString('hex')}.mp3`
const file = await new Promise((resolve) => {
ffmpeg(input).audioFilter(['apulsator=hz=0.125']).audioFrequency(44100).audioChannels(2).audioBitrate('128k').audioCodec('libmp3lame').audioQuality(5).toFormat('mp3').save(output).on('end', () => resolve(output))
})
return file
}
/**
* write file from buffer
* @param {Buffer} buffer buffer
* @param {string} path path to save file
* @returns
*/
function save(buffer, path) {
try {
fs.writeFileSync(path, buffer.toString('binary'), 'binary')
return path
} catch (error) {
console.log(error);
}
}
/**
* Resize image
* @param {Buffer} buffer
* @param {Number} width
* @param {Number} height
* @returns {Promise<Buffer>}
*/
const resizeImage = (buffer, width, height) => {
if (!Buffer.isBuffer(buffer)) throw 'Input is not a Buffer'
return new Promise(async (resolve) => {
sharp(buffer).resize(width, height, {
fit: 'contain'
}).toBuffer().then(resolve)
})
}
/**
*
* @param {Buffer|string} data Buffer | path | url
* @param {string} attachExtension media extension, eg: jpeg
* @param {string} result output result path | buffer
* @returns
*/
const _parseInput = async (data, attachExtension, result = 'path') => {
const get = await toBuffer(await getHttpStream(data))
const inputPath = `./file_${randomBytes(3).toString('hex')}.${attachExtension ? attachExtension : (await fromBuffer(get)).ext}`
const out = Buffer.isBuffer(data) ? save(data, inputPath) : fs.existsSync(data) ? data : isUrl(data) ? save(get, inputPath) : data
if (result == 'path') {
return out
} else if (result == 'buffer') {
const buff = await fs.promises.readFile(out)
try {
await fs.promises.unlink(out)
} catch (error) {
throw error
}
return buff
}
}
module.exports = {
toGif,
toMp4,
toAudio,
EightD,
_parseInput,
resizeImage
}