简体   繁体   中英

Stream multiple files with node js

I am trying to send multiple video files with express and mediaserver . Is there a way to check if a file has finished streaming and then change the video to the next one? Below is the express request. So to clarify I can send a video over with the below request but I do not know how to change the file once it has finished streaming, I read that mediaserver is great at sending media files but there aren't any docs, so I could not find a way or a description of how to do this.

const express = require('express');
const app = express();
const ms = require('mediaserver');

app.get('/video', function(req, res){
   const filename = 'sample1.mp4'
   const filename1 = 'sample2.mp4'
   ms.pipe(req, res, assetsPath + filename);
   console.log ('[streaming video]: ' + filename);
});

app.listen(3000, function () {
   console.log('Listening on port 3000...');
});

The best way to dynamically host files that I could find is to have a generic 'streaming' route and then pass the file as a query:

const express = require('express'); 
const app = express();
const ms = require('fs'); 

app.get('/stream', function(req,res){
    var fileId = req.query.id,
    file = __dirname + fileId;
    fs.exists(file,function(exists){
        if(exists) {
            console.log("[Streaming:] %s", file);
            var rstream = fs.createReadStream(file); 
            rstream.pipe(res);
        } else {
            console.log("[ERROR Streaming:] %s", file);
            res.send("Playback 404");
            res.end();
        }
    });
}); 

app.listen(3000, function () {
    console.log('Listening on port 3000...');
});

You can then look through a directory and create a html string to send to the page, creating a button that streams the files on click:

var serverAddress = "http://localhost:3000"
var file = "sample.mp4"
stream = "<a href='" +serverAddress+ "/stream?id=" + file + "' target='_blank'><button class='btn btn-success'>Stream</button></a>";
res.send (stream)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM