0

Creating a Local media player using node js to stream video in the browser.

I am using below node js code to stream local videos.

const express = require('express')
const fs = require('fs')
const path = require('path')
const os = require('os')
var walk    = require('walk');
const app = express()
const port = 3000;
const ipaddr = "localhost";

app.use(express.static(path.join(__dirname, 'public')))

app.set('view engine', 'ejs');
const videoHomeDir = os.homedir() + `/Videos`;
var files   = [];

var walker  = walk.walk(videoHomeDir, { followLinks: false });

walker.on('file', function(root, stat, next) {
    files.push(root + '/' + stat.name);
    next();
});

walker.on('end', function() {
    console.log("GOT ALL VIDEOS")
});

app.get('/', function(req, res) {
    res.render('pages/index', {files: files, ipaddress:ipaddr,port:port});
})


app.get('/video/:id', function(req, res) {
  var video = files[req.params.id];
  const path = `${video}`;
  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

    if(start >= fileSize) {
      res.status(416).send('Requested range not satisfiable\n'+start+' >= '+fileSize);
      return
    }
    
    const chunksize = (end-start)+1
    const file = fs.createReadStream(path, {start, end})
    const head = {
      'Content-Range': 'bytes chunkStart****chunkEnd/chunkSize' ,
      'Accept-Ranges': 'bytes',
      'Content-Length': chunksize,
      'Content-Type': 'video/mp4',
    }

    res.writeHead(206, head)
    file.pipe(res)
  } else {
    const head = {
      'Content-Range': `bytes`,
      'Content-Length': fileSize,
      'Content-Type': 'video/mp4',
    }
    res.writeHead(200, head)
    fs.createReadStream(path).pipe(res)
  }

})


app.listen(port, function () {
  console.log(`Listening on port ${port}!`)
})

Some video are playing properly but some video with High data rate video are not playing in browser, why? & How to resolve this issue with node js.

Here is the video details shown :

enter image description here

How to resolve this issue with node js

  • It seems the client is unable to process this much data when you have the high res video. It might need more BW to process the same amount of high. I think you might need to send some data chunks when sending High res videos. Check this if you can get some idea [here](https://stackoverflow.com/questions/39049903/why-is-fs-createreadstream-piperes-locking-the-read-file/39055780#39055780) – Apoorva Chikara Jan 02 '23 at 06:26

0 Answers0