Vercel错误与youtube-dl-exec和Next.js。

huangapple go评论60阅读模式
英文:

Vercel error with youtube-dl-exec and Next js

问题

I'm using Next.jsyoutube-dl-exec。 我制作了一个名为 /api/video/info 的端点,使用 Next APIs,位于 pages/api/video/info.js 目录下。

info.js

import youtubedl from 'youtube-dl-exec';

export default async function handler(req, res) {
  const { videoLink } = req.query;
  if (!videoLink) {
    res.status(400).json({
      code: 400,
      message: 'Bad request: the video link is missing.',
    });
    return;
  }
  try {
    const videoInfo = await youtubedl(videoLink, {
      dumpSingleJson: true,
      noWarnings: true,
      preferFreeFormats: true,
    });
    res.status(200).json({
      code: 200,
      data: {
        title: videoInfo.title,
        author: videoInfo.uploader,
        lengthSeconds: videoInfo.duration,
        viewCount: videoInfo.view_count,
        thumbnailUrl: videoInfo.thumbnail,
        description: videoInfo.description,
        formats: videoInfo.formats,
      },
    });
  } catch (error) {
    console.log(error);
    res.status(500).json({
      code: 500,
      message: "couldn't get video information",
      error,
    });
  }
}

这段代码在我的本地机器上的端口3000上运行得很好,即使在使用命令 npm run buildnpm start 进行生产后,它在我的本地机器上的两个环境(开发或生产)上都能正常运行。 但是,当我部署到Vercel后,当调用端点时,Vercel日志上出现了这个错误,并显示状态码为500

Error: /usr/bin/env: python3: No such file or directory
    at parse (/var/task/node_modules/youtube-dl-exec/src/index.js:15:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async handler (/var/task/.next/server/pages/api/video/info.js:35:27)
    at async Object.apiResolver (/var/task/node_modules/next/dist/server/api-utils/node.js:184:9)
    at async NextNodeServer.runApi (/var/task/node_modules/next/dist/server/next-server.js:403:9)
    at async Object.fn (/var/task/node_modules/next/dist/server/base-server.js:493:37)
    at async Router.execute (/var/task/node_modules/next/dist/server/router.js:222:36)
    at async NextNodeServer.run (/var/task/node_modules/next/dist/server/base-server.js:612:29)
    at async NextNodeServer.handleRequest (/var/task/node_modules/next/dist/server/base-server.js:311:20)
    at async module.exports (/var/task/___next_launcher.cjs:29:9) {
  shortMessage: 'Command failed with exit code 127: /var/task/node_modules/youtube-dl-exec/bin/yt-dlp https://youtu.be/WwB5w-Rt88k --dump-single-json --no-warnings --prefer-free-formats',
  command: '/var/task/node_modules/youtube-dl-exec/bin/yt-dlp https://youtu.be/WwB5w-Rt88k --dump-single-json --no-warnings --prefer-free-formats',
  escapedCommand: '"/var/task/node_modules/youtube-dl-exec/bin/yt-dlp" "https://youtu.be/WwB5w-Rt88k" --dump-single-json --no-warnings --prefer-free-formats',
  exitCode: 127,
  signal: undefined,
  signalDescription: undefined,
  failed: true,
  timedOut: false,
  isCanceled: false,
  killed: false
}

问题是什么,我该如何解决它?

英文:

I'm using Next js and youtube-dl-exec. I made endpoint /api/video/info with Next APIs in dir pages/api/video/info.js

info.js:

import youtubedl from 'youtube-dl-exec'

export default async function handler(req, res) {
  const { videoLink } = req.query
  if (!videoLink) {
    res.status(400).json({
      code: 400,
      message: 'Bad request: the video link is missing.',
    })
    return
  }
  try {
    const videoInfo = await youtubedl(videoLink, {
      dumpSingleJson: true,
      noWarnings: true,
      preferFreeFormats: true,
    })
    res.status(200).json({
      code: 200,
      data: {
        title: videoInfo.title,
        author: videoInfo.uploader,
        lengthSeconds: videoInfo.duration,
        viewCount: videoInfo.view_count,
        thumbnailUrl: videoInfo.thumbnail,
        description: videoInfo.description,
        formats: videoInfo.formats,
      },
    })
  } catch (error) {
    console.log(error)
    res.status(500).json({
      code: 500,
      message: "couldn't get video information",
      error,
    })
  }
}

The code works perfectly on my local machine on port 3000 and even after production with command npm run build and npm start it works fine on both environment (development or production) on my local machine but after deploying it to vercel when I call the endpoint, this error appears on vercel logs with status code 500:

Error: /usr/bin/env: python3: No such file or directory
    at parse (/var/task/node_modules/youtube-dl-exec/src/index.js:15:23)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async handler (/var/task/.next/server/pages/api/video/info.js:35:27)
    at async Object.apiResolver (/var/task/node_modules/next/dist/server/api-utils/node.js:184:9)
    at async NextNodeServer.runApi (/var/task/node_modules/next/dist/server/next-server.js:403:9)
    at async Object.fn (/var/task/node_modules/next/dist/server/base-server.js:493:37)
    at async Router.execute (/var/task/node_modules/next/dist/server/router.js:222:36)
    at async NextNodeServer.run (/var/task/node_modules/next/dist/server/base-server.js:612:29)
    at async NextNodeServer.handleRequest (/var/task/node_modules/next/dist/server/base-server.js:311:20)
    at async module.exports (/var/task/___next_launcher.cjs:29:9) {
  shortMessage: 'Command failed with exit code 127: /var/task/node_modules/youtube-dl-exec/bin/yt-dlp https://youtu.be/WwB5w-Rt88k --dump-single-json --no-warnings --prefer-free-formats',
  command: '/var/task/node_modules/youtube-dl-exec/bin/yt-dlp https://youtu.be/WwB5w-Rt88k --dump-single-json --no-warnings --prefer-free-formats',
  escapedCommand: '"/var/task/node_modules/youtube-dl-exec/bin/yt-dlp" "https://youtu.be/WwB5w-Rt88k" --dump-single-json --no-warnings --prefer-free-formats',
  exitCode: 127,
  signal: undefined,
  signalDescription: undefined,
  failed: true,
  timedOut: false,
  isCanceled: false,
  killed: false
}

What's the problem and how can I solve it?

答案1

得分: 1

我相当肯定youtube-dl-exec需要权限来下载和安装yt-dlp

所以它在Vercel上无法工作

您需要将它放在Docker容器中,并部署到您自己的服务器上,或者支持Docker的无服务器环境

如果您成功做到了,告诉我,因为我很感兴趣

英文:

I'm pretty sure youtube-dl-exec needs privileges to download and install yt-dlp

So it won't work on vercel

You will have to put this in a Docker container and deploy on a server of yours, or one serverless that supports docker

Let me know if you manage to do it as I'm interested

huangapple
  • 本文由 发表于 2023年4月6日 21:43:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定