kyriesent / node-rtsp-stream

Stream any RTSP stream and output to websocket for consumption by jsmpeg (https://github.com/phoboslab/jsmpeg). HTML5 streaming video! Requires ffmpeg.
MIT License
451 stars 166 forks source link

Electron application without FFMpeg #77

Open erikmartinjordan opened 3 years ago

erikmartinjordan commented 3 years ago

I'm building an Electron application, which launches an Express server in the background and sends back a stream as a response.

Sample code:

const express = require('express');
Stream        = require('node-rtsp-stream');

let app = express();

let server = app.listen(3000);

app.get('/video', function(req, res){

    stream = new Stream({

        name: 'name',
        streamUrl: 'rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov',
        wsPort: 9000

    });

    res.send(
        `<!DOCTYPE html>
            <html>
            <head>
                <meta name = "viewport" content = "width=640, initial-scale=1"/>
                <style type = "text/css">
                    html, body {
                        width: 800px;
                        height: 600px;
                        margin: 0;
                    }
                    canvas {
                        margin: 0;
                    }
                </style>
            </head>
            <body>
                <canvas id = "video"></canvas>
                <script src = "https://cdnjs.cloudflare.com/ajax/libs/jsmpeg/0.2/jsmpg.js"></script>
                <script>
                    var client = new WebSocket( 'ws://localhost:9000/' );
                    var canvas = document.getElementById('video');
                    var player = new jsmpeg(client, {canvas: canvas});
                </script>
            </body>
            </html>`
    );

});

It seems that node-rtsp-stream isn't creating the WebSocket and sending the stream. I guess it's failing because FFMpeg isn't installed on the server.

The problem is that it's a client application and I don't have control if the user has FFMpeg installed on his computer.

Is there a way to make node-rtsp-stream work? Can I force the application to install FFMpeg in some way? Can I combine ffmpeg Node.js package to make it work?

erikmartinjordan commented 3 years ago

Ok, I figured out that you can install ffmpeg-static to include FFMpeg as a binary library (no need to preinstall FFMPeg in your computer).

Then, you can include ffmpegPath as an option in the stream:

const express     = require('express');
const ffmpegPath  = require('ffmpeg-static');
Stream            = require('node-rtsp-stream');

let app = express();

let server = app.listen(3000);

app.get('/video', function(req, res){

    stream = new Stream({

        name: 'name',
        streamUrl: 'rtsp://184.72.239.149/vod/mp4:BigBuckBunny_115k.mov',
        wsPort: 9000,
        ffmpegPath: ffmpegPath

    });

    res.send(
        `<!DOCTYPE html>
            <html>
            <head>
                <meta name = "viewport" content = "width=640, initial-scale=1"/>
                <style type = "text/css">
                    html, body {
                        width: 800px;
                        height: 600px;
                        margin: 0;
                    }
                    canvas {
                        margin: 0;
                    }
                </style>
            </head>
            <body>
                <canvas id = "video"></canvas>
                <script src = "https://cdnjs.cloudflare.com/ajax/libs/jsmpeg/0.2/jsmpg.js"></script>
                <script>
                    var client = new WebSocket( 'ws://localhost:9000/' );
                    var canvas = document.getElementById('video');
                    var player = new jsmpeg(client, {canvas: canvas});
                </script>
            </body>
            </html>`
    );

});

Props to Alexander Cleasby, and his useful article.

punjasin commented 3 years ago

@erikmartinjordan for me my pc have ffpmpeg install but when we build the app to .app for macos the ffmpeg is not loaded even i try using static image

erikmartinjordan commented 3 years ago

Could you share your code @punjasin?