Video Streams with Simple RTSP Server, FFmpeg, and Wireguard
In my tinkering projects and research, I’ve often had to setup cameras to provide video stream over the “global” internet to other devices. By “global”, I mean devices which are not in the same WLAN, and things are therefore a bit more complicated.
The instructions provided here demonstrate streaming from a Raspberry Pi with the Raspberry Pi camera. However, the methodology shouldn’t change drastically when using other cameras/computer systems.
Jumping into the software side of things, first we need to make sure all our devices are connected to each other. A VPN, such as Wireguard, offers a flexible way to do this. I’ve personally preferred applying Wireguard in, well, all of my personal projects and research lately. It’s just so fast, reliable, and simple to setup! Setup is naturally OS specific, but for Ubuntu I’ve typically followed the basic workflow presented here. The basic gist is that first you need server (I like Linode) on which you install Wireguard, and after that you install Wireguard on each of your client devices. Configuration files are modified accordingly, so that in the end each device is connected to each other via the Wireguard server!
Simple RTSP Server
Next we have to setup an RTSP server on the device that is actually doing the video streaming, in our example the Raspberry Pi. A dead simple way to achieve this is with the accordingly named Simple RTSP Server. All you have to do is extract the latest appropriate release, and start the server from the command line:
Doesn’t get simpler than that! Not only that, according to my understanding the actual server is really efficient and well made.
Lastly, we still need to actually publish the video stream on the server. With most cameras, this can be conveniently achieved with FFmpeg. With the RTSP server running, we can publish a stream from our Raspberry Pi camera by executing the following command:
ffmpeg -f video4linux2 -input_format h264 -video_size 1280x720 -framerate 5 -i /dev/video0 -c copy -f rtsp rtsp://localhost:8554/stream
To fully understand what the command is doing, you should check the flags from the documentation. The basic idea is that we are reading 720p video from the Raspberry Pi camera at five fps, and publishing that on the RTSP server, at “rtsp://localhost:8554/stream”.
Accessing the Stream
Now with all that configuration, we finally have a video stream available. Using the Wireguard-IP of the streaming device (Raspberry Pi), we can now view the stream with for example VLC. If we had given the streaming device the IP “10.100.100.11” in the Wireguard network, the stream could be opened with VLC as shown in the image below.