---
tags: notes, rtmp, video streaming, nginx, ffmpeg
---
# Video Streaming Server (nginx, rtmp)
With this note, you will have an nginx service as rtmp server. You can push/pull video streaming to/from the server. In addition, you can also push your camera or video with `ffmpeg` to the rtmp server.
Distro tested: ubuntu 18.04, ubuntu-mate 18.04, raspberry-os
## Build envrioment
```bash
# first thing first
sudo apt-get update
# install building tools
sudo apt-get install -y build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev git
cd /usr/local/src
git clone https://github.com/arut/nginx-rtmp-module
wget http://nginx.org/download/nginx-1.17.2.tar.gz
tar zxvf nginx-1.17.2.tar.gz
mv nginx-1.17.2 nginx
rm -f nginx-1.17.2.tar.gz
cd nginx
./configure --with-http_ssl_module --add-module=/usr/local/src/nginx-rtmp-module
# if make goes wrong, try: make CFLAGS='-Wno-implicit-fallthrough'
make
make install
```
Verify if nginx setup properly:
```bash
# you should nginx echo its version in th console
/usr/local/nginx/sbin/nginx -v
```
## Control `nginx` via `systemctl`
Create service config file:
```
vim /etc/systemd/system/nginx.service
```
And then type:
```cmake=
[Unit]
Description=The nginx server
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /usr/local/nginx/logs/nginx.pid
TimeoutStopSec=5
KillMode=mixed
[Install]
WantedBy=multi-user.target
```
Apply the config file:
```
systemctl daemon-reload
systemctl start nginx.service
systemctl enable nginx.service # auto execution on boot
systemctl status nginx.service
```
Type `localhost` or corresponding ip in the broswer, you should see this:

## Config nginx as RTMP
Modify the existing configuration file:
```bash
vim /usr/local/nginx/conf/nginx.conf
```
insert the following settings:
```json
rtmp {
server {
listen 1935; # port rtmp used
chunk_size 4096; # block size each pack
application vod {
play /opt/video; # stream local video file from the path
}
application live {
live on; # allow streaming push
allow publish all; # control access privilege
allow play all; # control access privilege
}
}
}
```
Check for config file syntax:
```
/usr/local/nginx/sbin/nginx -tc /usr/local/nginx/conf/nginx.conf
```
Check if anything using the port 1935:
```
netstat -nalp | grep 1935
```
You can access the `application` above (live and vod) with url like `rtmp://localhost/live` or `rtmp://localhost/vod`. You can also use any other word for an application.
You can play video file under the path `/opt/video/`. For example, for the video file `/opt/videdo/the_video.mp4`, you can play it with the url: `rtmp://localhost/vod/the_video.mp4`. ( just copy and paste in the VLC player and it plays the video if nothing goes wrong.)
## Statistics page
`nginx-rtmp-module` has default status page, showing basic statistics counts.
Edit the config file:
```
vim /usr/local/nginx/conf/nginx.conf
```
And insert the block with comment in the specific section:
```
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /stat { ### RTMP statistic page
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl { ### RTMP statistic table
root /usr/local/src/nginx-rtmp-module/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
```
Reload nginx service to update changes
```bash
systemctl reload nginx
```
Go to the web page `http://localhost/stat`, and you should see:

## Push something to the RTMP server
`ffmpeg` is a powerful tool for converting video between different format and pushing video streams.
```bash
apt-get install ffmpeg
```
You can pick one of the following to push to the RTMP server:
- Webcam (video0)
```bash
# software decoding
ffmpeg -f video4linux2 -r 24 -i /dev/video0 -f flv rtmp://localhost:1935/live
# hardware decoding
apt-get install gstreamer1.0-tools
gst-launch-1.0 -v v4l2src ! 'video/x-raw, width=640, height=480, framerate=30/1' ! queue ! videoconvert ! omxh264enc ! h264parse ! flvmux ! rtmpsink location='rtmp://localhost:1935/live live=1'
# if above fails:
apt-get install libomxil-bellagio0 libomxil-bellagio-bin
gst-launch-1.0 -v v4l2src ! 'video/x-raw, width=640, height=480, framerate=30/1' ! queue ! videoconvert ! avenc_h264_omx ! h264parse ! flvmux ! rtmpsink location='rtmp://localhost:1935/live live=1'
```
- Video file
`
ffmpeg -re -i ./the_video.mp4 -c copy -f flv rtmp://localhost:1935/live
`
- OBS

Again, you can pull(aka play) the video stream with the url `rtmp://localhost:1935/live`. (copy and paste in the VLC player should do the trick.)
## Reference
- [樹梅派架設RTMP串流伺服器](https://blog.gtwang.org/iot/raspberry-pi-nginx-rtmp-server-live-streaming/)
- [RTMP/HLS Webcam Live Streaming](https://kaanlabs.com/rtmp-hls-webcam-live-streaming-with-hardware-accelerated-h264-on-a-raspberry-pi/)