Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to Stream Data #164

Open
surajheadsup opened this issue Aug 22, 2022 · 0 comments
Open

Not able to Stream Data #164

surajheadsup opened this issue Aug 22, 2022 · 0 comments

Comments

@surajheadsup
Copy link

Hello, I am trying to load the stream into the player but, am not able to load it.

Full Stream Code on Server :

`const express = require("express");
var cors = require("cors");
var ip = require("ip")
const app = express();
const fs = require("fs");
app.use(cors())
app.use('/video/:name', express.static('uploads'))
app.get("/", function (req, res) {
res.sendFile(__dirname + "/index.html");
});

app.get("/video/:name", function (req, res) {

const range = req.headers.range === undefined ? "byte=0" : req.headers.range;

console.log('inside stream range: ' + JSON.stringify(req.headers));


console.log('range: ' + range);

if (!range) {
    res.status(400).send("Requires Range header");
}
const videoPath = "./video.mp4";
const videoSize = fs.statSync("./video.mp4").size;
const CHUNK_SIZE = 10 ** 6;
const start = Number(range.replace(/\D/g, ""));
const end = Math.min(start + CHUNK_SIZE, videoSize - 1);
const contentLength = end - start + 1;
const headers = {
    "Content-Range": `bytes ${start}-${end}/${videoSize}`,
    "Accept-Ranges": "bytes",
    "Content-Length": contentLength,
    "Content-Type": "video/mp4",
}; 

res.writeHead(206, headers);
const videoStream = fs.createReadStream(videoPath, { start, end });
videoStream.pipe(res);

});

app.get("/final", function (req, res) {

var path = __dirname;

console.log('path', path);
res.sendFile(path+'/video.mp4')

});

app.listen(3004, function () {
console.log("Listening on port 3004!");
console.log('ip address', ip.address());
});`

I have tried both first stream data and other server file as static, Static file working fine but stream not loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@surajheadsup and others