File size: 1,978 Bytes
4b17ed0
d145b59
 
4b17ed0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d145b59
 
 
 
 
 
4b17ed0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d145b59
4b17ed0
 
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
const { spawn } = require("child_process");
const path = require("path");

const ytdlp = "yt-dlp"; // Asumsi yt-dlp sudah terinstall global di Docker
const cookiesPath = path.join(__dirname, "./cookies/twt.txt");

function runYtDlp(args) {
  return new Promise((resolve, reject) => {
    const process = spawn(ytdlp, args, { stdio: ["ignore", "pipe", "pipe"] });
    let stdout = "";
    let stderr = "";

    process.stdout.on("data", (data) => (stdout += data.toString()));
    process.stderr.on("data", (data) => (stderr += data.toString()));

    process.on("close", (code) => {
      if (code === 0) resolve(stdout.trim());
      else reject(new Error(stderr.trim() || `yt-dlp exited with code ${code}`));
    });
  });
}

/**
 * Mengambil metadata video Twitter menggunakan yt-dlp.
 * @param {string} url - URL tweet yang berisi video.
 * @returns {Promise<Object>} - Metadata video dalam format JSON.
 */
async function getTwitterVideoInfo(url) {
  try {
    const result = await runYtDlp(["-j", "--cookies", cookiesPath, url]);
    const info = JSON.parse(result);

    // Pastikan `formats` ada, jika tidak buat array kosong
    const videoFormats = (info.formats || []).filter(
      (f) => (f.video_ext !== "none" && f.audio_ext !== "none") || f.url.includes(".mp4")
    );

    const audioFormats = (info.formats || []).filter((f) => f.video_ext === "none" && f.audio_ext !== "none");

    return {
      status: 200,
      title: info.title || "No Title",
      description: info.description || "",
      thumbnail: info.thumbnail || "",
      video: videoFormats.map((v) => ({
        format_id: v.format_id,
        resolution: v.resolution || `${v.width}x${v.height}`,
        url: v.url,
      })),
      audio: audioFormats.map((a) => ({
        format_id: a.format_id,
        bitrate: a.abr,
        url: a.url,
      })),
    };
  } catch (err) {
    throw new Error(`Failed to get video info: ${err.message}`);
  }
}

module.exports = getTwitterVideoInfo;