fluent-ffmpeg / node-fluent-ffmpeg

A fluent API to FFMPEG (http://www.ffmpeg.org)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how to terminate ffmpeg via websocket?

ufoozhenghao opened this issue · comments

Version information

  • fluent-ffmpeg version: ^2.1.2
  • ffmpeg version: ffmpeg version 6.0-static
  • OS: centos7

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

  • [✅ ] I have read the FAQ
  • [ ✅ ] I tried the same with command line ffmpeg and it works correctly (hint: if the problem also happens this way, this is an ffmpeg problem and you're not reporting it to the right place)
  • [✅ ] I have included full stderr/stdout output from ffmpeg