Local Network USB Cam Video Streaming
I have a need to run some cameras that need to be accessible only on LAN. One is just looking outside my window and taking a picture of the sky every hour; the other is just watching my pets.
I wanted cameras that could stream RTSP locally and be watched anywhere on the network using VLC. Before I got such a camera, I thought about streaming using an existing USB camera and Pi/Laptop as an experiment. If possible, make it work with Home Assistant. That way, I can reuse the existing USB webcams and already up-running machines.
Any computer with ffmpeg installed and has an attached camera can read the camera and push the stream to an RTSP server. So now we need an RTSP server. There are many options, but I went with bluenviron/mediamtx, a Swiss army knife for all streaming video.
Ready-to-use RTSP / RTMP / LL-HLS / WebRTC media server and media proxy that allows to read, publish and proxy video and audio streams.
mediamtx
I installed the docker version on my home server - homeserver.local
. The mediamtx server supports multiple protocols on different ports. Currently forcing it to use TCP. I also make Docker to use external mediamtx.yml
. You can download the template mediamtx.yml from git repo. Docker uses /mediamtx.yml
, so you can mount it there.
Ports used
- RTSP - 8554 - rtspAddress
- WebRTC - 8889 - webrtcAddress
- HLS - 8888 - hlsAddress
- RTMP 1934 - rtmpAddress
We are going to use RTSP. So on my camera, I will push the stream to the server using ffmpeg
ffmpeg -f v4l2 -i /dev/video2 -vcodec libx264 -pix_fmt yuv420p -r 30 -f rtsp -rtsp_transport tcp rtsp://homeserver.local:8554/mystream
- Reads from
/dev/video2
- you will have to change it if your camera is mounted somewhere else - It use H.264 encoding for video
- Publishes to
rtsp://homeserver.local:8554/mystream
- In this case, I have not enabled publish or read - username and passwords. You can edit them in mediamtx.yml and enable it. In that case, you must use
rtsp://publish_user:[email protected]:8554/mystream
for publishing.
So now your camera is publishing to a central server. Any user on the network can go to rtsp://homeserver.local:8554/mystream
in VLC and watch the stream live.
WebRTC is another option to publish using the browser ( instead of ffmpeg). This can be useful; let's say you want to use your desktop during working hours to stream some camera. Hypothetical use-case, Say you are working at your home office, and your pet also is in the same room during working hours. You can stream it, so your partner can see it.
Home Assistant can pull the RTSP streams as a Generic camera. But Home Assistant also has RTSPtoWebRTC integration, which can take RTSP camera and convert it into WebRTC so you can watch them directly in the browser as part of your Home Assistant dashboard.
Additional Notes: If you have many cameras on the internet and local network. You want them integrated with Home Assistant. Then you can use go2rtc integration. It can pull various streams and make them available to Home Assistant. In a way, it's a universal viewer/client (including local USB Cam), and mediamtx is a server. They can also work together.
1 Response
[…] wrote a blog post detailing the inside-home video streaming from USB Cameras, reusing the existing USB cameras, and computing instead of buying new […]