Search code examples
nginxffmpegreverse-proxyrtmpnginx-reverse-proxy

RTMP proxy to crop original video and send it to another RTMP server


I need to crop the video from an RTMP stream and send it to another RTMP server which always change. My understanding is that I should use nginx-proxy and ffmpeg, can anybody help me on how to set it up?

I suppose that i need to send the stream to an endpoint like /stream/:stream-key/:next-server-ip process the stream with ffmpeg and then send it to the :next-server-ip, what language should I use in the backend for this?


Solution

  • There are 2 strategies for processing such task:

    1. "Pull"

    You have some published rtmp stream and use ffmpeg to pull it, convert and send result to another server:

    ffmpeg -i rtmp://source-server/stream -filter:v "crop=out_w:out_h:x:y" -vcodec h264 -acodec copy -f flv rtmp://next-server/stream
    
    1. "Push"

    RTMP stream is pushed to your server which processes it and sends result to another server. For such task you can use nginx-rtmp module for nginx and setup ffmpeg command using exec_push directive:

    application src {
        live on;
        exec_push ffmpeg -i rtmp://localhost/src/$name -filter:v "crop=out_w:out_h:x:y" -vcodec h264 -acodec copy -f flv rtmp://next-server/stream 2>>/var/log/ffmpeg-$name.log;
    }
    

    When someone start to stream to rtmp://your-server/src/stream_name this ffmpeg command will be executed and the processing will begin

    For additional information about video cropping and related ffmpeg parameters see https://video.stackexchange.com/a/4571