簡體   English   中英

使用 node.js 將視頻上傳到 Gfycat 從 Gfycat API 返回 `NotFoundo`

[英]Uploading a video to Gfycat using node.js returns `NotFoundo` from the Gfycat API

我正在構建一個節點應用程序,它使用Gfycat 的 API將本地 MP4 上傳到它並給我轉換后的 gif 的 URL。 這是我到目前為止所擁有的:

const fs = require("fs");
const axios = require("axios");

async function main() {
  const res = await axios({
    method: "POST",
    url: "https://api.gfycat.com/v1/gfycats",
    data: { title: "test" }
  });

  const name = res.data.gfyname;
  console.log(`The key name is: ${name}`)

  const stream = fs.createReadStream("./Video.mp4");
  const { size } = fs.statSync("./Video.mp4");

  stream.on("error", console.warn);

  const sendResult = await axios({
    method: "PUT",
    url: `https://filedrop.gfycat.com/${name}`,
    data: stream,
    headers: {
      "Content-Type": "video/mp4",
      "Content-Length": size
    }
  });

  // Check the status of the encoding every n seconds until it says complete. When complete, return the gfyname.
  await waitTillPosted(name);

  return gfyname = checkResult.data.gfyname;
}

// Basic GET API request to check the encoding status of the gif
async function isPosted(name) {
  var checkResult = await axios({
    method: "GET",
    url: `https://api.gfycat.com/v1/gfycats/fetch/status/${name}`,
  });
  console.log(`Current status: ${checkResult.data.task}`)

  if (checkResult.data.task == "complete") {
    return true;
  }
  throw new Error('The gif is still encoding.');
}

// Generic sleep function
function sleep(ms) {
  console.log(`Sleeping for ${ms} ms.`)
  return new Promise(resolve => setTimeout(resolve, ms));
}

// If the gif is not done encoding, sleep 30s
async function waitTillPosted(name) {
  return isPosted(name).catch(() => sleep(30e3).then(waitTillPosted));
}

// ======================================================

main()
  .then(name => {
    console.log(`https://gfycat.com/${gfyname}`);
  })
  .catch(err => {
    console.error(err);
  });

根據API,您可以通過GET https://api.gfycat.com/v1/gfycats/fetch/status/gfyname查看轉換任務的狀態。

當我運行這個應用程序時,第一個GET請求會按預期生成報告encoding 我每 30 秒發出一次GET請求,直到最終,我希望它說complete ,在那里我將返回 Gfycat URL。 但是我得到了一個NotFoundo

我使用了我在測試中得到的一個關鍵名稱,並分別通過GET請求檢查了狀態,然后我得到了包含 gif 詳細信息的正確complete響應。 它有效並且 gif 存在於 URL 中。

當我在主函數中執行相同的檢查時,為什么會得到NotFoundo

這可能是 gfycat 的一個錯誤。 當我認為他們的服務器超載時,我經常在高峰時間收到它。

我的解決方案:在等待短暫的超時后重新開始。 這就是對我“有效”的東西。

您也可能想嘗試不同的視頻。 也許他們無法解碼您的文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM