An extremely lightweight hardware-accelerated MJPEG streaming server for Raspberry Pi + Camera Module with Flask & libcamera API.
There is no security in this code to prevent any kind of attack. Consider implementing basic authentication with HTTPS if possible!
- Update your Raspbery Pi to latest official OS. Use Lite for lowest resource usage!
- Update packages with :
sudo apt updatesudo apt upgrade -ysudo apt dist-upgrade -ysudo apt install git
- Install Python packages with :
sudo apt install python3-pip -ysudo apt install python3-picamera2 --no-install-recommends
- Enable
I2Cviaraspi-config git clone https://github.com/kingkingyyk/Pi-MJPEG-Server.gitcd Pi-MJPEG-Server- Update
mjpeg_server.pyif needed. There are some configurations available such as resolution. - Move python script to somewhere safe:
sudo cp mjpeg_server.py /usr/local/bin - Install required python packages:
sudo python3 -m pip install --break-system-packages -r requirements.txt - Create an auto start service with:
sudo cp mjpeg_server.service /etc/systemd/systemsudo systemctl daemon-reloadsudo systemctl start mjpeg_serversudo systemctl enable mjpeg_server
- Verify the server is running fine with :
sudo systemctl status mjpeg_server- Open your browser and visit address
http://<Pi IP>:<8764/Port>/
This code is tested on work with motioneye. Just add this server url as network camera.
The code is tested to work on :
- Pi 5
SW Encode+ Camera Module v3 (~85% CPU usage for single connection) - Pi 4
HW Encode+ Camera Module v3 (~4% CPU usage for single connection, 1% more for every connection) - Pi Zero 2 W
HW Encode+ Camera Module v1 (~35% CPU usage for single connection) - Pi Zero W
HW Encode+ Camera Module v1 (~60% CPU usage for single connection)