yanwsh / videojs-panorama

a plugin for videojs run a full 360 degree panorama video.
http://yanwsh.github.io/videojs-panorama/
Other
485 stars 160 forks source link

Status 416 Requested Range Not Satisfiable #73

Open SabaTD opened 7 years ago

SabaTD commented 7 years ago

Hi, i am using plugin on website, sometimes video file takes too long to load so i got Status 416 Requested Range Not Satisfiable on .mp4 files.

When there is no 416 status video file loads and plays perfectly, at that time there is status 206. This happens about 50% of time, depends on internet speed.

Would increasing Accept-Ranges:bytes helps ? Any solutions ?

yanwsh commented 7 years ago

Hey, did you use nodejs server?

I have the same problem before. MP4 video on real server needs to do buffering. So it's possible to play the video in middle without download the full video. If this didn't be set on server level, it will have problem.

check the following code if you use nodejs

        if(type == "video/mp4" || type == "audio/mp4" || type == "video/webm" || type == "audio/webm" || type == "video/ogg" || type == "audio/ogg" || type == "video/mpeg"
            || type == "video/h264" || type == "video/x-flv" || type == "video/3gpp" || type == "video/3gpp2"){
            var stat = fs.statSync(filePath);
            var total = stat.size;
            if (req.headers['range']) {
                var range = req.headers.range;
                var parts = range.replace(/bytes=/, "").split("-");
                var partialstart = parts[0];
                var partialend = parts[1];

                var start = parseInt(partialstart, 10);
                var end = partialend ? parseInt(partialend, 10) : total-1;
                var chunksize = (end-start)+1;

                var file = fs.createReadStream(filePath, {start: start, end: end});
                res.writeHead(206, { 'Content-Range': 'bytes ' + start + '-' + end + '/' + total, 'Accept-Ranges': 'bytes', 'Content-Length': chunksize, 'Content-Type': type });
                file.pipe(res);
            } else {
                res.writeHead(200, { 'Content-Length': total, 'Content-Type': type });
                fs.createReadStream(filePath).pipe(res);
            }
            return;
        }