Nginx
Nginx is a web server with interesting modules to extend its functionality.
The Real Time Messaging Protocol (RTMP) is used to send video (& audio) streams
over the internet. Nginx offers a RTMP media streaming module.
Many action cams and drones support sending their video streams via RTMP.
This page is focused on the configuration of RTMP on Nginx.
Contents
- Installation
- RTMP configuration example
- Publishing a stream with FFmpeg
- HTTP Live Streaming (HLS)
- External links
Installation
sudo apt install nginx libnginx-mod-rtmp
RTMP configuration example
/etc/nginx/nginx.conf
#... # For rtmp, worker_processes needs to be 1, which is the default. #worker_processes auto; #... rtmp { server { listen 1935; # Who is allowed to connect can be handled with # HTTP response status codes. #on_connect http://example.net/rtmp_auth; application live { live on; hls on; hls_path /tmp/hls; dash on; dash_path /tmp/dash; } } } http { #... server { listen 8080; location /hls { types { application/vnd.apple.mpegurl m3u8; } root /tmp; add_header Cache-Control no-cache; add_header Access-Control-Allow-Origin *; } location /dash { types { application/dash+xml mpd; } root /tmp; add_header Cache-Control no-cache; add_header Access-Control-Allow-Origin *; } } } #...
Reload the configuration:
sudo systemctl reload nginx
Publishing a stream with FFmpeg
The following command creates a stream named stream
in
the configured application live
:
ffmpeg -re -i input.mp4 \
-c:v libx264 -c:a aac \
-f flv rtmp://example.net/live/stream
Receive the stream from the RTMP server and show it on screen:
ffplay -fflags nobuffer rtmp://example.net/live/stream
HTTP Live Streaming (HLS)
hls.html
<!DOCTYPE html> <html lang="en"> <head> <title>HTTP Live Streaming</title> </head> <body> <script src="https://cdn.jsdelivr.net/npm/hls.js@1"></script> <video id="video" controls></video> <script> var video = document.getElementById('video'); var videoSrc = 'http://example.net:8080/hls/stream.m3u8'; if (Hls.isSupported()) { var hls = new Hls(); hls.loadSource(videoSrc); hls.on(Hls.Events.MEDIA_ATTACHED, function () { video.muted = true; video.play(); }); hls.attachMedia(video); } else if (video.canPlayType('application/vnd.apple.mpegurl')) { video.src = videoSrc; } </script> </body> </html>
HTTP Live Streaming (HLS) adds a delay / latency of approx. 10 to 60 seconds
to the live input stream.
External links
CONTENT.html | source | 2023-09-16 | 4.4 KB |