Sending Video To Client Not Working For Larger Videos #69
-
I have a video downloader on my site, but when the video length is too long the video download doesn't work. It downloads, but something is wrong with the video file and it won't play. I assume this is because of some limit with the size on it, as when I download a smaller video it works perfectly fine. Is there some sort of setting I need to add to my code to make it work? webserver.get('/youtube-converter/download', async function(req, res, next) {
const queryObject = url.parse(req.url, true).query;
var video = queryObject.url;
var quality = queryObject.quality;
var filename = queryObject.name;
if (filename == '') {
filename = 'youtubetomp4-download';
}
if (!video.includes('youtube.com/watch?v=')) {
res.status(403)
res.header({
'Content-Type': 'text/html'
});
res.send("<hr><h1 style='text-align:center'>Invalid Youtube Link: " + video + "</h1><hr>");
console.log('Invalid Youtube String: ' + video);
return;
}
const youtube = await new Innertube();
var stream;
if (quality != 'mp3') {
stream = youtube.download(video.split('=')[1], {
format: 'mp4', // Optional, ignored when type is set to audio and defaults to mp4, and I recommend to leave it as it is
quality: quality, // if a video doesn't have a specific quality it'll fall back to 360p, also ignored when type is set to audio
type: 'videoandaudio' // can be “video”, “audio” and “videoandaudio”
});
res.header('Content-Disposition', 'attachment;filename="' + filename + '.mp4"');
res.header('Content-Type', ['application/octet-stream', 'video/mp4']);
} else {
stream = youtube.download(video.split('=')[1], {
type: 'mp3',
quality: quality, // if a video doesn't have a specific quality it'll fall back to 360p, also ignored when type is set to audio
type: 'audio' // can be “video”, “audio” and “videoandaudio”
});
res.header('Content-Disposition', 'attachment;filename="' + filename + '.mp3"');
res.header('Content-Type', ['application/octet-stream', 'video/mp3']);
}
res.stream(stream);
var start;
stream.on('start', (info) => {
start = new Date();
console.log('Downloading: ' + video.split('=')[1]);
});
stream.on('end', () => {
var end = new Date();
var seconds = (end.getTime() - start.getTime()) / 1000;
console.log(video.split('=')[1] + ': ' + seconds + ' second(s)');
})
stream.on('error', (error) => {
res.removeHeader('Content-Disposition');
res.removeHeader('Content-Type');
res.header('Content-Type', 'text/html');
res.write('<head><title>Please Try Again...</title></head><body><h1> Error Converting Video </h1><p>' + JSON.stringify(error) + '</body>');
res.end();
});
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey! Could you try your example above in the recently released |
Beta Was this translation helpful? Give feedback.
Hey! Could you try your example above in the recently released
v6.1.0
version of HyperExpress to see If you still encounter the problem with the video not working after download?