How to stream video data to a video element?

前端 未结 3 1557
盖世英雄少女心
盖世英雄少女心 2021-02-06 02:47

I have a Node.js process which outputs a video stream into my Node.js app.

On the client end, there is a tag. I would like to stream the video

3条回答
  •  抹茶落季
    2021-02-06 03:12

    I've actually tried this at a hackathon two weeks ago. I ended up barely getting this flv stream to work, which I've posted below. My intent was to make a library to automate much of the processes this would entail.

    As you can see, I've opened a new port on the server to handle the separate stream of data flowing to the client. This is reflected in the client's src tag.

    THREE THINGS YOU NEED:

    1. This Linux version of ffmpeg.

    2. Flowplayer on the js side.

    3. npm fluent-ffmpeg

      // StreamServer.js

      var express = require('express'),
        app = express(),
        ffmpeg = require('fluent-ffmpeg');
      
      module.exports = function () {
          app.stream(req, res)
          {
              res.contentType('flv');
              // make sure you set the correct path to your video file storage 
              var pathToMovie = '/path/to/storage/' + req.params.filename;
              var proc = ffmpeg(pathToMovie)
                // use the 'flashvideo' preset (located in /lib/presets/flashvideo.js) 
                .preset('flashvideo')
                // setup event handlers 
                .on('end', function () {
                    console.log('file has been converted succesfully');
                })
                .on('error', function (err) {
                    console.log('an error happened: ' + err.message);
                })
                // save to stream 
                .pipe(res, { end: true });
      
          };
      
      }
      

      //routes.js

      'use strict';
      var stream = require('../controllers/streaming.server.controller'),
       streamServer = require('../controllers/StreamServer.js'),
      express = require('express');
      

      //streaming.server.controller.js

      module.exports = function (app) {
          app.get('/api/stream', function (req, res) {
              streamServer.stream(req, res);
          });
      };
      
      var path = require('path'),
       express = require('express'),
       app = express(),
       routes = require('../routes/routes.js')(app),
       ffmpeg = require('fluent-ffmpeg');
      
      app.listen(4000);
      

    EDIT: Client side part:

    https://github.com/fluent-ffmpeg/node-fluent-ffmpeg/tree/master/examples/flowplayer

    
        
            
            
            node-fluent-ffmpeg
        
        
    
            
             
             
    
            
            
        
    
    

    (Just change the href attribute)

提交回复
热议问题