webp2mp4.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { convert } from "./ezgif-convert.js"
  2. import { FormData, Blob } from 'formdata-node'
  3. import { fileTypeFromBuffer } from "file-type"
  4. import crypto from "crypto"
  5. const randomBytes = crypto.randomBytes(5).toString("hex");
  6. const urlRegex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/;
  7. async function webp2mp4(source) {
  8. const isUrl = typeof source === 'string' && urlRegex.test(source);
  9. try {
  10. return await convert({
  11. type: 'webp-mp4',
  12. ...(isUrl ? {
  13. url: source
  14. } : {
  15. file: new Blob([source]),
  16. filename: randomBytes + "." + (await fileTypeFromBuffer(source)).ext
  17. })
  18. });
  19. } catch (error) {
  20. console.error("Error converting to webp-mp4. Trying fallback types.");
  21. try {
  22. return await convert({
  23. type: 'webp-avif',
  24. ...(isUrl ? {
  25. url: source
  26. } : {
  27. file: new Blob([source]),
  28. filename: randomBytes + "." + (await fileTypeFromBuffer(source)).ext
  29. })
  30. });
  31. } catch (avifError) {
  32. console.error("Error converting to webp-avif. Trying webp-gif.");
  33. try {
  34. return await convert({
  35. type: 'webp-gif',
  36. ...(isUrl ? {
  37. url: source
  38. } : {
  39. file: new Blob([source]),
  40. filename: randomBytes + "." + (await fileTypeFromBuffer(source)).ext
  41. })
  42. });
  43. } catch (gifError) {
  44. console.error("Error converting to webp-gif. All fallback types failed.");
  45. throw gifError;
  46. }
  47. }
  48. }
  49. }
  50. async function webp2png(source) {
  51. const isUrl = typeof source === 'string' && urlRegex.test(source);
  52. try {
  53. return await convert({
  54. type: 'webp-png',
  55. ...(isUrl ? {
  56. url: source
  57. } : {
  58. file: new Blob([source]),
  59. filename: randomBytes + "." + (await fileTypeFromBuffer(source)).ext
  60. })
  61. });
  62. } catch (pngError) {
  63. console.error("Error converting to webp-png. Trying webp-jpg.");
  64. try {
  65. return await convert({
  66. type: 'webp-jpg',
  67. ...(isUrl ? {
  68. url: source
  69. } : {
  70. file: new Blob([source]),
  71. filename: randomBytes + "." + (await fileTypeFromBuffer(source)).ext
  72. })
  73. });
  74. } catch (jpgError) {
  75. console.error("Error converting to webp-jpg. All fallback types failed.");
  76. throw jpgError;
  77. }
  78. }
  79. }
  80. export {
  81. webp2mp4,
  82. webp2png
  83. };
  84. /*import fetch from 'node-fetch';
  85. import {
  86. FormData,
  87. Blob
  88. } from 'formdata-node';
  89. import {
  90. JSDOM
  91. } from 'jsdom';
  92. // @param {Buffer|String} source
  93. async function webp2mp4(source) {
  94. let form = new FormData()
  95. let isUrl = typeof source === 'string' && /https?:\/\//.test(source)
  96. const blob = !isUrl && new Blob([source.toArrayBuffer()])
  97. form.append('new-image-url', isUrl ? blob : '')
  98. form.append('new-image', isUrl ? '' : blob, 'image.webp')
  99. let res = await fetch('https://ezgif.com/webp-to-mp4', {
  100. method: 'POST',
  101. body: form
  102. })
  103. let html = await res.text()
  104. let {
  105. document
  106. } = new JSDOM(html).window
  107. let form2 = new FormData()
  108. let obj = {}
  109. for (let input of document.querySelectorAll('form input[name]')) {
  110. obj[input.name] = input.value
  111. form2.append(input.name, input.value)
  112. }
  113. let res2 = await fetch('https://ezgif.com/webp-to-mp4/' + obj.file, {
  114. method: 'POST',
  115. body: form2
  116. })
  117. let html2 = await res2.text()
  118. let {
  119. document: document2
  120. } = new JSDOM(html2).window
  121. return new URL(document2.querySelector('div#output > p.outfile > video > source').src, res2.url).toString()
  122. }
  123. async function webp2png(source) {
  124. let form = new FormData()
  125. let isUrl = typeof source === 'string' && /https?:\/\//.test(source)
  126. const blob = !isUrl && new Blob([source.toArrayBuffer()])
  127. form.append('new-image-url', isUrl ? blob : '')
  128. form.append('new-image', isUrl ? '' : blob, 'image.webp')
  129. let res = await fetch('https://ezgif.com/webp-to-png', {
  130. method: 'POST',
  131. body: form
  132. })
  133. let html = await res.text()
  134. let {
  135. document
  136. } = new JSDOM(html).window
  137. let form2 = new FormData()
  138. let obj = {}
  139. for (let input of document.querySelectorAll('form input[name]')) {
  140. obj[input.name] = input.value
  141. form2.append(input.name, input.value)
  142. }
  143. let res2 = await fetch('https://ezgif.com/webp-to-png/' + obj.file, {
  144. method: 'POST',
  145. body: form2
  146. })
  147. let html2 = await res2.text()
  148. let {
  149. document: document2
  150. } = new JSDOM(html2).window
  151. return new URL(document2.querySelector('div#output > p.outfile > img').src, res2.url).toString()
  152. }
  153. export {
  154. webp2mp4,
  155. webp2png
  156. }
  157. // By @nm9h*/