fluent-ffmpeg / node-fluent-ffmpeg

A fluent API to FFMPEG (http://www.ffmpeg.org)
MIT License
7.97k stars 881 forks source link

how to terminate ffmpeg via websocket? #1214

Open ufoozhenghao opened 1 year ago

ufoozhenghao commented 1 year ago

Version information

Want

I created a websocket via server and client by using ws When the connection create ffmpeg start to convert my stream, but when the client interrupted, the ffmpeg is still converting. So, how to stop ffmpeg, if the server already know the client is down?

Code to reproduce

my convert module rtspToFlvHandle in nodejs

const rtspToFlvHandle=(ws, req)=> {
    const url = req.query.url;
    console.log(ws)
    console.log('rtsp url:', url);
    try {
        ffmpeg(url)
            .addInputOption(
                '-rtsp_transport', 'tcp',
                '-buffer_size', '102400'
            )
            .addOutputOption(
                '-threads', '4',
                '-tune', 'zerolatency',
                '-preset', 'superfast'
            )
            .outputFormat('flv') 
            .videoCodec('libx264')

            .noAudio()
    } catch (error) {
        console.log(error);
    }
}

module.exports = rtspToFlvHandle

my appjs

const ws1 = new WebSocket.WebSocketServer({ noServer: true });

ws1.on('connection', function connection(ws) {
    ws.on('error', console.error);
    # start convert
    rtspToFlvHandle(ws, {query:{url:'rtsp://dell.dltdzc.com:554/01'}})
    ws.on('pong', heartbeat);
    ws.on('close', function close(ws) {
        console.log('onclose!');
        console.log(ws)
        clearInterval(interval); 
    })
});

const interval = setInterval(function ping() {
    ws1.clients.forEach(function each(ws) {
        console.log('ping')
        if (ws.isAlive === false){
            console.log('close')
            return ws.terminate();
        }
        ws.isAlive = false;
        ws.ping();
    });
}, 5000);

(note: if the problem only happens with some inputs, include a link to such an input file)

Checklist