I have a node server which serves byte range request from browser. The browser sends the request. I get 206 successful byte-range request status code. But the fetched segment is not playing in html5 video element. I am using firefox.
Error : No video with supported format or mime type found
async function fetchRange(url, startByte, endByte) {
try {
const response = await fetch(url, {
headers: {'Range': `bytes=${startByte}-${endByte}`}
});
const data = await response.blob();
console.log(`Fetched ${data.size} bytes from ${startByte} to ${endByte}.`);
return data;
} catch (error) {
console.error('Error fetching range:', error);
}
}
// Example usage:
const fileUrl = 'http://localhost:8000/video'; // Replace with your file URL
const start = 0;
const end = 1023; // Requesting the first 1024 bytes (0-1023)
fetchRange(fileUrl, start, end)
.then(data => {
if (data) {
// Process the received data (e.g., display it, save it)
console.log('Received data:', data);
let f = new File([data],'haah',{ type: "video/mp4" })
document.getElementById('vid2').src = URL.createObjectURL(f)
}
});
html:
<video controls width="350" id="vid2" >
<source src='' type="video/mp4" >
Sorry, not supported by browser
</source>
</video>
node server code :
if(req.url === '/video' && req.method === 'GET'){
console.log('video get')
//var filePath = 'audiofile2.flac';
var filePath = "output_ah.mp4"
var stat = fs.statSync(filePath);
var total = stat.size;
console.log(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;
//console.log(start,end)
var chunksize = (end-start)+1;
var file = fs.createReadStream(filePath, {start: start, end: end});
res.writeHead(206, {
'Content-Range': 'bytes ' + start + '-' + end + '/' + total,
// 'Content-Length' : total,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/mp4'
// 'ETag' : '"'+"abcd-efgh"+'"'
});
file.pipe(res)
}