| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import { convert } from "./ezgif-convert.js"
- import { FormData, Blob } from 'formdata-node'
- import { fileTypeFromBuffer } from "file-type"
- import crypto from "crypto"
- const randomBytes = crypto.randomBytes(5).toString("hex");
- const urlRegex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/;
- async function webp2mp4(source) {
- const isUrl = typeof source === 'string' && urlRegex.test(source);
-
- try {
- return await convert({
- type: 'webp-mp4',
- ...(isUrl ? {
- url: source
- } : {
- file: new Blob([source]),
- filename: randomBytes + "." + (await fileTypeFromBuffer(source)).ext
- })
- });
- } catch (error) {
- console.error("Error converting to webp-mp4. Trying fallback types.");
- try {
- return await convert({
- type: 'webp-avif',
- ...(isUrl ? {
- url: source
- } : {
- file: new Blob([source]),
- filename: randomBytes + "." + (await fileTypeFromBuffer(source)).ext
- })
- });
- } catch (avifError) {
- console.error("Error converting to webp-avif. Trying webp-gif.");
- try {
- return await convert({
- type: 'webp-gif',
- ...(isUrl ? {
- url: source
- } : {
- file: new Blob([source]),
- filename: randomBytes + "." + (await fileTypeFromBuffer(source)).ext
- })
- });
- } catch (gifError) {
- console.error("Error converting to webp-gif. All fallback types failed.");
- throw gifError;
- }
- }
- }
- }
- async function webp2png(source) {
- const isUrl = typeof source === 'string' && urlRegex.test(source);
- try {
- return await convert({
- type: 'webp-png',
- ...(isUrl ? {
- url: source
- } : {
- file: new Blob([source]),
- filename: randomBytes + "." + (await fileTypeFromBuffer(source)).ext
- })
- });
- } catch (pngError) {
- console.error("Error converting to webp-png. Trying webp-jpg.");
- try {
- return await convert({
- type: 'webp-jpg',
- ...(isUrl ? {
- url: source
- } : {
- file: new Blob([source]),
- filename: randomBytes + "." + (await fileTypeFromBuffer(source)).ext
- })
- });
- } catch (jpgError) {
- console.error("Error converting to webp-jpg. All fallback types failed.");
- throw jpgError;
- }
- }
- }
- export {
- webp2mp4,
- webp2png
- };
- /*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://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://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
- }
- // By @nm9h*/
|