My goal is to stream a video file into chunks rather than loading and playing the entire file at once. It works great on Google Chrome and I am able to see the chunk sizes as they are logged to the console. Now when I try this on firefox but I'm getting nothing logged to the console. The video will play, but I am unable to fast forward past the 10 minute or so mark. Just curious why it works flawlessly in Chrome and not firefox?
router.get("/video", ensureAuthenticated, function(req, res) {
var id = req.query.id;
let path = "";
for (let k in movieLinks) {
if (k === id) {
path = movieLinks[k]
}
}
// const path = "public/movies/noes2.mp4";
const stat = fs.statSync(path);
const fileSize = stat.size;
const range = req.headers.range;
if (range) {
const parts = range.replace(/bytes=/, "").split("-");
const start = parseInt(parts[0],10);
const end = parts[1] ? parseInt(parts[1], 10) : fileSize-1;
const chunkSize = (end - start) + 1;
console.log('RANGE: ' + start + ' - ' + end + ' = ' + chunkSize);
const file = fs.createReadStream(path, {start,end} );
const head = {
'Content-Range' : `bytes ${start}-${end}/${fileSize}`,
'Content-Ranges' : 'bytes',
'Content-Length' : chunkSize,
'Content-Type' : 'video/mp4'
}
res.writeHead(206, head);
file.pipe(res);
} else {
const head = {
'Content-Length' : fileSize,
'Content-Type' : 'video/mp4'
}
res.writeHead(200,head);
fs.createReadStream(path).pipe(res);
}
// res.render('video', {layout: 'videoLayout'});
})
I done this before splitting video files with tool call ffmpeg.
You can use the script below to split your video in 30 second chunks read them from a s3 bucket or wherever they may live. This will make browser download easier.
ffmpeg script example:
ffmpeg -i input -c copy -segment_time 30 -f segment input.mov
I hope this helps