-
-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathwebp2mp4.js
65 lines (63 loc) · 2.1 KB
/
webp2mp4.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
import fetch from 'node-fetch';
import { FormData, Blob } from 'formdata-node';
import { JSDOM } from 'jsdom';
/**
*
* @param {Buffer|String} source
*/
async function webp2mp4(source) {
let form = new FormData()
let isUrl = typeof source === 'string' && /https?:\/\//.test(source)
const blob = !isUrl && new Blob([source.toArrayBuffer()])
form.append('new-image-url', isUrl ? blob : '')
form.append('new-image', isUrl ? '' : blob, 'image.webp')
let res = await fetch('https://s6.ezgif.com/webp-to-mp4', {
method: 'POST',
body: form
})
let html = await res.text()
let { document } = new JSDOM(html).window
let form2 = new FormData()
let obj = {}
for (let input of document.querySelectorAll('form input[name]')) {
obj[input.name] = input.value
form2.append(input.name, input.value)
}
let res2 = await fetch('https://ezgif.com/webp-to-mp4/' + obj.file, {
method: 'POST',
body: form2
})
let html2 = await res2.text()
let { document: document2 } = new JSDOM(html2).window
return new URL(document2.querySelector('div#output > p.outfile > video > source').src, res2.url).toString()
}
async function webp2png(source) {
let form = new FormData()
let isUrl = typeof source === 'string' && /https?:\/\//.test(source)
const blob = !isUrl && new Blob([source.toArrayBuffer()])
form.append('new-image-url', isUrl ? blob : '')
form.append('new-image', isUrl ? '' : blob, 'image.webp')
let res = await fetch('https://s6.ezgif.com/webp-to-png', {
method: 'POST',
body: form
})
let html = await res.text()
let { document } = new JSDOM(html).window
let form2 = new FormData()
let obj = {}
for (let input of document.querySelectorAll('form input[name]')) {
obj[input.name] = input.value
form2.append(input.name, input.value)
}
let res2 = await fetch('https://ezgif.com/webp-to-png/' + obj.file, {
method: 'POST',
body: form2
})
let html2 = await res2.text()
let { document: document2 } = new JSDOM(html2).window
return new URL(document2.querySelector('div#output > p.outfile > img').src, res2.url).toString()
}
export {
webp2mp4,
webp2png
}