Search code examples
node.jsffmpegwebrtcvideo-streamingrtmp

WebRTC Video Track to ffmpeg in Node


I have succesfully managed to establish a WebRTC connection between Node (server) and a browser. Server gets the video track on onTrack callback inside the RTCPeerConnection. Is there any way I can potentially convert the video track and make it work on ffmpeg so I can output it to rtmp.

Thanks in advance.


Solution

  • The way I have done this is to use a socket to the node server, and then use ffmpeg to convert to RTMP:

    I spawn FFMPEG

    var spawn = require('child_process').spawn;
    spawn('ffmpeg',['-h']).on('error',function(m){
        console.error("FFMpeg not found in system cli; please install ffmpeg properly or make a softlink to ./!");
        process.exit(-1);
    });
    

    I make sure Im getting video from the socket, and then I pipe it into FFMPEG and out to my RTMP server:

    var ops=[ '-i','-',

            '-c:v', 'libx264', '-preset', 'ultrafast', '-tune', 'zerolatency',  // video codec config: low latency, adaptive bitrate
            '-c:a', 'aac', '-ar', audioBitrate, '-b:a', audioEncoding, // audio codec config: sampling frequency (11025, 22050, 44100), bitrate 64 kbits
            //'-max_muxing_queue_size', '4000', 
            //'-y', //force to overwrite
            //'-use_wallclock_as_timestamps', '1', // used for audio sync
            //'-async', '1', // used for audio sync
            //'-filter_complex', 'aresample=44100', // resample audio to 44100Hz, needed if input is not 44100
            //'-strict', 'experimental', 
            '-bufsize', '5000',
            
            '-f', 'flv', socket._rtmpDestination
        
            
        ];
    }
    console.log("ops", ops);
        console.log(socket._rtmpDestination);
        ffmpeg_process=spawn('ffmpeg', ops);
        console.log("ffmpeg spawned");
    

    you can see my code:https://github.com/dougsillars/browserLiveStream/blob/master/server.js

    and a working example at livestream.a.video