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:UsersMR.ROBOTDesktopTM-Botindex.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();

我做错了什么?

注:忽略mode变量,这是为了防止我想添加除youtube之外的其他服务。

乌利希期刊指南。如果我写机器人。sendMessage (chatId、输出)

以电报形式答复:

[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

如果我写:机器人。sendVideo (chatId、输出)

终端错误:

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:UsersMR.ROBOTDesktopTM-Botnode_modulesnode-telegram-bot-apisrctelegram.js:291:15
at tryCatcher (C:UsersMR.ROBOTDesktopTM-Botnode_modulesbluebirdjsreleaseutil.js:16:23)
at Promise._settlePromiseFromHandler (C:UsersMR.ROBOTDesktopTM-Botnode_modulesbluebirdjsreleasepromise.js:547:31)
at Promise._settlePromise (C:UsersMR.ROBOTDesktopTM-Botnode_modulesbluebirdjsreleasepromise.js:604:18)
at Promise._settlePromise0 (C:UsersMR.ROBOTDesktopTM-Botnode_modulesbluebirdjsreleasepromise.js:649:10)
at Promise._settlePromises (C:UsersMR.ROBOTDesktopTM-Botnode_modulesbluebirdjsreleasepromise.js:729:18)
at _drainQueueStep (C:UsersMR.ROBOTDesktopTM-Botnode_modulesbluebirdjsreleaseasync.js:93:12)
at _drainQueue (C:UsersMR.ROBOTDesktopTM-Botnode_modulesbluebirdjsreleaseasync.js:86:9)
at Async._drainQueues (C:UsersMR.ROBOTDesktopTM-Botnode_modulesbluebirdjsreleaseasync.js:102:5)
at Immediate.Async.drainQueues [as _onImmediate] (C:UsersMR.ROBOTDesktopTM-Botnode_modulesbluebirdjsreleaseasync.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调用放在有console.log函数的回调中,因此:

}).then((output) => {
console.log(output)
bot.sendMessage(chatId, output); // or sendVideo
});

最新更新