node.js - 如何从 youtube-dl 获取视频作为响应?
问题描述
我正在编写一个电报机器人从 YouTube 下载视频,我使用 youtube-dl api,当我尝试获取视频作为响应时,但视频只是下载到根目录,但脚本不会将其发送到用户,它给了我一个错误。
错误类型:
Test video sending
[youtube] En8go1kP3rg: Downloading webpage
[download] Destination: DJ SMASH feat Po�t - (REMIX 2020) TOP Xit-En8go1kP3rg.mp4
[download] 100% of 7.47MiB in 00:01
(node:10996) UnhandledPromiseRejectionWarning: ReferenceError: output is not defined
at TelegramBot.<anonymous> (C:\Users\MR.ROBOT\Desktop\TM-Bot\index.js:29:31)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:10996) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:10996) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
有我的代码:
const telegramApi = require("node-telegram-bot-api");
const token = "myApi";
const fs = require("fs");
const youtubedl = require("youtube-dl-exec");
const bot = new telegramApi(token, { polling: true });
const start = () => {
bot.on("message", async (msg) => {
const text = msg.text;
const chatId = msg.chat.id;
const mode = "youtube";
if (text === "/start") {
await bot.sendSticker(
chatId,
"https://tlgrm.ru/_/stickers/b50/063/b5006369-8faa-44d7-9f02-1ca97d82cd49/1.webp"
);
await bot.sendMessage(chatId, "Пошла жара...");
}
if (mode === "youtube") {
console.log("Test video sending");
const video = await youtubedl(text, {
noWarnings: true,
preferFreeFormats: true,
}).then((output) => console.log(output));
bot.sendMessage(chatId, output);
}
});
};
start();
我究竟做错了什么?
PS忽略模式变量,这是为了以防我想添加除youtube之外的其他服务。
UPD .: 如果我写 bot.sendMessage(chatId, output)
它在电报中给出了回应:
[youtube] 7rlRet5t5pU: Downloading webpage
[download] Destination: miyagi & - (slowed + reverb)-7rlRet5t5pU.mp4
[download] 0.0% of 8.63MiB at 139.74KiB/s ETA 01:03[download] 0.0% of 8.63MiB at 367.33KiB/s ETA 00:24[download] 0.1% of 8.63MiB at 857.10KiB/s ETA 00:10[download] 0.2% of 8.63MiB at 1.79MiB/s ETA 00:04 [download] 0.4% of 8.63MiB at 1.90MiB/s ETA 00:04 [download] 0.7% of 8.63MiB at 2.15MiB/s ETA 00:03 [download] 1.4% of 8.63MiB at 3.14MiB/s ETA 00:02 [download] 2.9% of 8.63MiB at 4.31MiB/s ETA 00:01 [download] 5.8% of 8.63MiB at 5.62MiB/s ETA 00:01 [download] 11.6% of 8.63MiB at 7.51MiB/s ETA 00:01 [download] 23.2% of 8.63MiB at 9.09MiB/s ETA 00:00 [download] 46.3% of 8.63MiB at 10.05MiB/s ETA 00:00 [download] 92.6% of 8.63MiB at 10.44MiB/s ETA 00:00 [download] 100.0% of 8.63MiB at 10.55MiB/s ETA 00:00 [download] 100% of 8.63MiB in 00:01
如果我写:bot.sendVideo(chatId, output)
终端错误:
Test video sending
[youtube] 7rlRet5t5pU: Downloading webpage
[download] Destination: miyagi & - (slowed + reverb)-7rlRet5t5pU.mp4
[download] 100% of 8.63MiB in 00:00
Unhandled rejection Error: ETELEGRAM: 400 Bad Request: invalid file HTTP URL specified: Wrong URL host
at C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\node-telegram-bot-api\src\telegram.js:291:15
at tryCatcher (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\promise.js:547:31)
at Promise._settlePromise (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\promise.js:604:18)
at Promise._settlePromise0 (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\promise.js:649:10)
at Promise._settlePromises (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\promise.js:729:18)
at _drainQueueStep (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\async.js:93:12)
at _drainQueue (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\async.js:86:9)
at Async._drainQueues (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\async.js:102:5)
at Immediate.Async.drainQueues [as _onImmediate] (C:\Users\MR.ROBOT\Desktop\TM-Bot\node_modules\bluebird\js\release\async.js:15:14)
at processImmediate (internal/timers.js:461:21)
解决方案
你试过用sendVideo
方法吗?它支持 mp4 文件格式 - 在此处查看更多信息https://github.com/yagop/node-telegram-bot-api/blob/release/doc/api.md#TelegramBot+sendVideo
此外,该output
变量未定义,因为它在不同的范围内。你应该把bot.sendX
call 放在你有功能的回调中console.log
,所以:
}).then((output) => {
console.log(output)
bot.sendMessage(chatId, output); // or sendVideo
});
推荐阅读
- spring-boot - Spring Boot webservice (REST) - 如何将 JUnit 5 测试从基本身份验证更改为 OAuth2 (Keycloak)
- react-native - 在 Expo React Native 应用程序中处理访问令牌存储
- java - 用于弹性搜索中通配符搜索的动态生成字段的java代码
- c# - WPF Web 视图 InvokeScript 未知名称错误
- regex - 如何以 (A(B(C))) 格式获取正则表达式中的文本字符串
- verilog - 为什么我的反价值产生 StX?
- excel - 使用 VB.net 更改 Excel 的单元格颜色
- r - 如何将 R Plotly 图从 GitHub 嵌入到 Medium
- azure-devops - Azure Devops 管道应用中心任务错误:未能加载组织列表
- c++ - 具有未命名类型的完美转发变量