gpt4 book ai didi

node.js - 使用 ytdl 在 Node js 中下载 YouTube 视频文件

转载 作者:太空宇宙 更新时间:2023-11-04 02:05:05 25 4
gpt4 key购买 nike

我想让用户能够使用 node-ytdl 下载 YouTube 视频。例如,当客户端对特定路由发出 GET 请求时,应下载视频作为响应。

  var ytdl = require('ytdl-core');
var express= require('express');

//Init App Instance
var app=express();



app.get('/video',function(req,res){


var ytstream=ytdl("https://www.youtube.com/watch?v=hgvuvdyzYFc");


ytstream.on('data',function(data){
res.write(data);
})

ytstream.on('end',function(data){
res.send();
})

})

上面是我的nodejs代码。即使在网络中,它似乎下载了响应,但它并不会让用户下载为文件。我不想在服务器上存储任何文件。如果有人可以帮助我解决问题,那就太好了。

最佳答案

res 对象是一个可写流,因此您可以像这样直接将 ytdl 的输出通过管道传输到 res 对象 -

    ytdl("http://www.youtube.com/watch?v=xzjxhskd")
.on("response", response => {
// If you want to set size of file in header
res.setHeader("content-length", response.headers["content-length"]);
})
.pipe(res);

关于node.js - 使用 ytdl 在 Node js 中下载 YouTube 视频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44605425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com