Writing video data on a stream
Closed this issue · 3 comments
jai-dewani commented
I could only find examples for writing the data stream coming from youtube-dl to a file, are there any examples or functions for writing the data on a response object by using res.wirte() or other suitable methods?
jai-dewani commented
nevermind I stumbled upon the solution myself. Thanks :)
ElectroluxV2 commented
nevermind I stumbled upon the solution myself. Thanks :)
Could you share your solution please?
jai-dewani commented
app.get("/download/:videoUrl", function(req, res) {
var video = youtube("http://www.youtube.com/watch?v=" + req.params.videoUrl,
["--format=18"],
{cwd: cwd});
var size, filename;
video.on("info", function(info) {
console.log("Download Started");
if (info.track === null) {
track = String(info.title + ".mp4");
}
else {
track = String(info.track + ".mp4");
}
// console.log(track);
size = info.size
filename = info._filename
res.writeHead(200, {
"Content-Disposition": "attachment;filename=" + filename,
'Content-Type': 'video/mp4',
'Content-Length': size
});
});;
video.on('data',(data)=>{
res.write(data)
})
video.on('end',(end)=>{
console.log('Download end')
res.end();
})
});
https://github.com/jai-dewani/umusic/blob/349f40e9e8030b6d66f037fc06ef4c55948ebbba/app.js#L51-L84
If this doesn't make sense, let me know