Search code examples
ffmpegvideo-streaming

Webcam Stream With ffmpeg: UDP Stream remotely


I've a C920 HD camera connected to a Raspberry Pi 4 and my goal is to be able to access a stream of that camera anytime from my phone / laptop both connected to my network with a VPN.

Now, I managed to use ffmpeg like this:

ffmpeg -f v4l2 -input_format h264 \
-video_size 1920x1080 \
-i /dev/video4 \
-copyinkf -codec copy \
-f mpegts udp://192.168.1.10:5000?pkt_size=1316

On the computer 192.168.1.10 I can launch VLC go into "Network Transmission" and type udp://@:5000 in oder to watch the stream.

This is a single stream and from what I understand my RPi is just "shooting" the frames at that computer whatever it is connected or not, how can I have a proper stream (maybe rtmp?) that I can watch in multiple devices?

Please note: I'm using -copyinkf -codec copy in order to avoid transcoding and other operations that might result in a very high CPU usage. Can I do it this way also?

Thank you.


Solution

  • Nginx can be configured to host an RTMP video stream that will be used to play the stream coming from ffmpeg in all my devices. For this we need to install libnginx-mod-rtmp and configure nginx for RTMP:

    1. apt install libnginx-mod-rtmp
    2. Append the following to /etc/nginx/nginx.conf:
    rtmp {
            server {
                    listen 1935;
                    chunk_size 4096;
                    allow publish 127.0.0.1;
                    deny publish all;
    
                    application live {
                            live on;
                            record off;
                    }
            }
    }
    
    1. systemctl restart nginx
    2. Point ffmpeg to the nginx server:
    ffmpeg -f v4l2 -input_format h264 \
    -video_size 1920x1080 \
    -i /dev/video4 \
    -copyinkf -codec copy \
    -f flv rtmp://127.0.0.1/live/stream
    

    I also changed the output format to flv in order to improve compatibility with players.

    Enjoy.