HTTP Live Streaming (HLS) mit nginx

Wir bauen hier einen speziellen nginx, mit einem rtmp Modul, welcher einen rtmp Stream empangen kann und daraus eine HLS Playlist und die Videos Chunks in einem Web Root ablegt.
Optional stellt er dieses auch direkt als Webserver bereit. Das Verzeichnis könnte aber von einem bestehenden Webservers ausgeliefert werden.

Installation

apt install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g zlib1g-dev

RTMP Modul clonen

git clone https://github.com/sergey-dryabzhinsky/nginx-rtmp-module.git

Aktuellen Build von nginx laden und entpacken

wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -xf nginx-1.16.1.tar.gz
cd nginx-1.16.1

nginx am besten in eigenes Verzeichnis kompilierern (über --prefix)

./configure --prefix=/usr/local/nginx-rtmp --with-http_ssl_module --add-module=../nginx-rtmp-module
make
make install

Ordner erstellen in dem der Stream zur Auslieferung gespeichert wird.

mkdir -p /var/www/hls/live
mkdir -p /var/www/videos
mkdir -p /var/www/html

nginx Konfiguration

#user nginx;
worker_processes auto;
events {
  worker_connections 1024;
}

# RTMP Konfiguration
rtmp {
  server {
  listen 1935;
  chunk_size 4096;
  ping 30s;
  notify_method get;
  allow play all;
  # rmtp handler our clients connect to for live streaming, it runs on port 1935. It converts the stream to HLS and stores it on our server
    application live {
      live on;
      hls on;
      hls_path /var/www/hls/live;
      hls_nested on;  # create a new folder for each stream
      record_notify on;
      record_path /var/www/videos;
      record all;
      record_unique on;
    }

    application vod {
      play /var/www/videos;
    }
 }
}

# HTTP Konfiguration
http {
  include   mime.types;
  default_type  application/octet-stream;
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 65;
  gzip on;
  server {
    listen 80;
    server_name _;

    autoindex on;

    location / {
      root /var/www/html;
      index index.html index.htm;
    }
    # the http end point our web based users connect to see the live stream
    location /live {
      types {
        application/vnd.apple.mpegurl m3u8;
      }
      alias /var/www/hls/live;
      add_header Cache-Control no-cache;
    }
  }
}

Web Player

/var/www/html/player.html

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>videojs-contrib-hls embed</title>
<link href="https://unpkg.com/video.js/dist/video-js.css" rel="stylesheet">
</head>
<body>
  <h1>Video.js Example Embed</h1>

  <video-js id="my_video_1" class="vjs-default-skin" controls preload="auto" width="640" height="480">
    <source src="http://localhost/live/STREAM-KEY/index.m3u8" type="application/x-mpegURL">
  </video-js>

  <script src="https://unpkg.com/video.js/dist/video.js"></script>
  <script src="https://unpkg.com/@videojs/http-streaming/dist/videojs-http-streaming.js"></script>

  <script>
var player = videojs('my_video_1',{
	liveui: true
});
  </script>
</body>
</html>

Streamen

STREAM-KEY ist ein freiwählbarer Name

Stream Ziel ist: rtmp://server_ip:1935/live/STREAM-KEY

Die Playlist des Stream dann unter http://server_ip/live/STREAM-KEY/index.m3u8 aufzurufen. Entweder über z.B. über VLC oder den Webplayer

ffmpeg

ffmpeg -i /path/to/video  -c:v h264 -c:a aac  -strict -2 -f flv rtmp://server_ip:1935/live/STREAM-KEY

OBS Studio

Stream-URL: rtmp://server_ip:1935/live
Stream-Key: frei wählbar z.B.: stream
Nach dem Stream Key richtet sich auch die playlist und clip Namen welche dann über den Webserver aufrufbar sind.

Quellen:
https://docs.peer5.com/guides/setting-up-hls-live-streaming-server-using-nginx/
https://obsproject.com/forum/resources/how-to-set-up-your-own-private-rtmp-server-using-nginx.50/
https://dev.to/samuyi/how-to-setup-nginx-for-hls-video-streaming-on-centos-7-3jb8

pub/http-live-streaming.txt · Zuletzt geändert: 2020/04/10 22:21 von Marco Krage