I'm trying to create a media server using nodejs, and i have no problem streaming any formats but Since web browsers cant play H265 codec videos i need to convert them to H264 while createReadStream creates a chunk so that i dont have to convert them completely before hand, just that chunk which is sent by the server to the browser.
const path = 'assets/yourfavmov.mkv';
const stat = fs.statSync(path);
const fileSize = stat.size;
const range = req.headers.range;
if (range) {
const rangeArray = range.replace(/bytes=/, "").split("-");
console.log(rangeArray)
const start = parseInt(rangeArray[0], 10);
const end = rangeArray[1] ? parseInt(rangeArray[1], 10) : fileSize-1;
const chunksize = (end-start) + 1;
const fileChunk = fs.createReadStream(path, {start, end});
const head = {
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Type': 'video/x-matroska'
};
res.writeHead(206, head);
fileChunk.pipe(res);
} else {
res.end("wont let you stream");
}
I tried to convert the stream using ffmpeg-stream, like so
const converter = new Converter();
const input = converter.createInputStream({
f: "matroska,webm",
vcodec : "hevc"
})
const fileChunk = fs.createReadStream(path, {start, end});
fileChunk.pipe(input);
converter
.createOutputStream({ f: "matroska,webm", vcodec: "h264" })
.pipe(res);
But i have no idea what i did is correct or wrong, so no luck so is there an way to do it right?
Thanks in advance.