Ayogiakbar

IMAGE & VIDEO ECHANCE

Oct 2nd, 2025 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * HD Media (Image & Video)
  3.  * Author : gienetic
  4.  * Base   : imgupscaler.ai / unblurimage.ai
  5.  */
  6.  
  7. const axios = require("axios");
  8. const FormData = require("form-data");
  9. const fs = require("fs");
  10. const { v4: uuidv4 } = require("uuid");
  11.  
  12. /**
  13.  * Upscale Image menggunakan imgupscaler.ai (serial random tiap request)
  14.  * @param {string} imagePath - path ke file image
  15.  * @param {string} authorization - optional, default kosong
  16.  * @returns {Promise<string>} - URL hasil upscale
  17.  */
  18. async function imageHD(imagePath, authorization = "") {
  19.     const serial = uuidv4();
  20.     const form = new FormData();
  21.     form.append("original_image_file", fs.createReadStream(imagePath));
  22.  
  23.     const uploadRes = await axios.post(
  24.         "https://api.imgupscaler.ai/api/image-upscaler/v2/upscale/create-job",
  25.         form,
  26.         {
  27.             headers: {
  28.                 ...form.getHeaders(),
  29.                 "Host": "api.imgupscaler.ai",
  30.                 "sec-ch-ua-platform": "\"Android\"",
  31.                 "authorization": authorization,
  32.                 "product-serial": serial,
  33.                 "user-agent": "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
  34.                 "sec-ch-ua": "\"Chromium\";v=\"140\", \"Not=A?Brand\";v=\"24\", \"Android WebView\";v=\"140\"",
  35.                 "sec-ch-ua-mobile": "?1",
  36.                 "accept": "*/*",
  37.                 "origin": "https://imgupscaler.ai",
  38.                 "x-requested-with": "com.chromasterZ.vn",
  39.                 "sec-fetch-site": "same-site",
  40.                 "sec-fetch-mode": "cors",
  41.                 "sec-fetch-dest": "empty",
  42.                 "referer": "https://imgupscaler.ai/",
  43.                 "accept-encoding": "gzip, deflate",
  44.                 "accept-language": "id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7",
  45.                 "priority": "u=1, i"
  46.             },
  47.             maxBodyLength: Infinity
  48.         }
  49.     );
  50.  
  51.     const jobId = uploadRes.data.result?.job_id;
  52.     if (!jobId) throw new Error("Gagal membuat job image HD");
  53.  
  54.     // Polling status job
  55.     while (true) {
  56.         const statusRes = await axios.get(
  57.             `https://api.imgupscaler.ai/api/image-upscaler/v1/universal_upscale/get-job/${jobId}`,
  58.             {
  59.                 headers: {
  60.                     "Host": "api.imgupscaler.ai",
  61.                     "sec-ch-ua-platform": "\"Android\"",
  62.                     "authorization": authorization,
  63.                     "product-serial": serial,
  64.                     "user-agent": "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
  65.                     "sec-ch-ua": "\"Chromium\";v=\"140\", \"Not=A?Brand\";v=\"24\", \"Android WebView\";v=\"140\"",
  66.                     "sec-ch-ua-mobile": "?1",
  67.                     "accept": "*/*",
  68.                     "origin": "https://imgupscaler.ai",
  69.                     "x-requested-with": "com.chromasterZ.vn",
  70.                     "sec-fetch-site": "same-site",
  71.                     "sec-fetch-mode": "cors",
  72.                     "sec-fetch-dest": "empty",
  73.                     "referer": "https://imgupscaler.ai/",
  74.                     "accept-encoding": "gzip, deflate",
  75.                     "accept-language": "id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7",
  76.                     "priority": "u=1, i"
  77.                 }
  78.             }
  79.         );
  80.  
  81.         const result = statusRes.data.result;
  82.         if (result && result.output_url?.length) return result.output_url[0];
  83.  
  84.         await new Promise(r => setTimeout(r, 3000));
  85.     }
  86. }
  87.  
  88. /**
  89.  * Upscale Video menggunakan unblurimage.ai (tidak perlu serial)
  90.  * @param {string} filePath - path ke file video
  91.  * @param {string} authorization - optional
  92.  * @returns {Promise<string>} - URL hasil upscale
  93.  */
  94. async function videoHD(filePath, authorization = "") {
  95.     const fileName = filePath.split("/").pop();
  96.     const formSlot = new FormData();
  97.     formSlot.append("video_file_name", fileName);
  98.  
  99.     const slotRes = await axios.post(
  100.         "https://api.unblurimage.ai/api/upscaler/v1/ai-video-enhancer/upload-video",
  101.         formSlot,
  102.         {
  103.             headers: {
  104.                 ...formSlot.getHeaders(),
  105.                 "origin": "https://imgupscaler.ai",
  106.                 "user-agent": "Gienetic/1.2.0 Mobile",
  107.                 "accept": "*/*",
  108.             },
  109.         }
  110.     );
  111.  
  112.     const slot = slotRes.data.result;
  113.     const stats = fs.statSync(filePath);
  114.     const stream = fs.createReadStream(filePath);
  115.  
  116.     await axios.put(slot.url, stream, {
  117.         headers: {
  118.             "Content-Type": "video/mp4",
  119.             "Content-Length": stats.size,
  120.         },
  121.         maxBodyLength: Infinity,
  122.         maxContentLength: Infinity,
  123.     });
  124.  
  125.     const videoUrl = `https://cdn.unwatermark.ai/${slot.object_name}`;
  126.     const formJob = new FormData();
  127.     formJob.append("original_video_file", videoUrl);
  128.     formJob.append("is_preview", "false");
  129.  
  130.     const jobRes = await axios.post(
  131.         "https://api.unblurimage.ai/api/upscaler/v2/ai-video-enhancer/create-job",
  132.         formJob,
  133.         {
  134.             headers: {
  135.                 ...formJob.getHeaders(),
  136.                 "origin": "https://imgupscaler.ai",
  137.                 "user-agent": "Gienetic/1.2.0 Mobile",
  138.                 "accept": "*/*",
  139.                 "authorization": authorization,
  140.             },
  141.         }
  142.     );
  143.  
  144.     const jobId = jobRes.data.result?.job_id;
  145.     if (!jobId) throw new Error("Gagal membuat job video HD");
  146.  
  147.     // Polling status job
  148.     while (true) {
  149.         const statusRes = await axios.get(
  150.             `https://api.unblurimage.ai/api/upscaler/v2/ai-video-enhancer/get-job/${jobId}`,
  151.             {
  152.                 headers: {
  153.                     "origin": "https://imgupscaler.ai",
  154.                     "user-agent": "Gienetic/1.2.0 Mobile",
  155.                     "accept": "*/*",
  156.                 },
  157.             }
  158.         );
  159.  
  160.         const result = statusRes.data.result;
  161.         if (result && result.output_url) return result.output_url;
  162.  
  163.         await new Promise(r => setTimeout(r, 5000));
  164.     }
  165. }
  166.  
  167. module.exports = { imageHD, videoHD };
  168.  
Advertisement
Add Comment
Please, Sign In to add comment