Nginx whith rtmp module

Материал из Webko Wiki
Перейти к навигации Перейти к поиску

Необходимые пакеты

Ubuntu/Debian

sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev wget

Archlinux

sudo pacman -S base-devel wget

Необходимые исходники

  1. Исходник nginx, последняя версия на данный момент
wget http://nginx.org/download/nginx-1.9.6.tar.gz

Проверят обновления здесь

  1. Исходник RTMP модуля, последняя версия
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

Порядок сборки

$ cd
$ wget http://nginx.org/download/nginx-1.9.6.tar.gz
$ wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
$ tar -zxvf nginx-1.9.6.tar.gz
$ unzip master.zip
$ cd nginx-1.9.6
$ ./configure --add-module=../nginx-rtmp-module-master --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log\
 --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp\
 --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp\
 --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module\
 --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail\
 --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
$ make
$ make install

Примеры конфигурации

Страница статистики

http {               #добавлят в секцию http
server {
        listen       ваш_ip/домен:порт;

        location /stat {
             rtmp_stat all;
             rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
             root /srv/http/;
        }
}

В итоге получим

Ретрансляция rtmp потока

rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp {                # секция rtmp в конфиге nginx
    server {
    listen ваш_ip/домен:1935;    #1935 стандартный порт rtmp
        
        application live {
        allow publish ip_с_которого_можна_принимать_поток;
        deny publish all;
        access_log /var/log/nginx/live_access.log;
        play_restart off;
        drop_idle_publisher 15s;
        
        live on;                    
        hls on;                    # включаем hls для воспроизведения на apple подобных
        hls_path /tmp/hls;
        hls_fragment 10s;
     }
}

Для просотра потока можна применить VLC "Открыть сетевой поток"

rtmp://ваш_ip/live/НАЗВАНИЕ_ПОТОКА

Ретрансляция rtmp потока с транскодировкой и кодированием в udp unicast

rtmp_auto_push on;
rtmp_auto_push_reconnect 1s;
rtmp {                # секция rtmp в конфиге nginx
    server {
    listen ваш_ip/домен:1935;    #1935 стандартный порт rtmp
        
        application live {
        allow publish ip_с_которого_можна_принимать_поток;
        deny publish all;
        access_log /var/log/nginx/live_access.log;
        play_restart off;
        drop_idle_publisher 15s;
        
        live on;                    
        hls on;                    # включаем hls для воспроизведения на apple подобных
        hls_path /tmp/hls;
        hls_fragment 10s;

        exec_static ffmpeg -i rtmp://ваш_ip/live/НАЗВАНИЕ_ПОТОКА -vcodec copy -acodec copy -threads 2 -f mpegts -vbsf h264_mp4toannexb\
 udp://адресат:порт_адресата?pkt_size=564;
        exec_static ffmpeg -i rtmp://ваш_ip/live/НАЗВАНИЕ_ПОТОКА -vcodec copy -acodec libmp3lame -ar 44100 -ab 128k -ac 2 -threads 2 -f mpegts -vbsf h264_mp4toannexb\
 udp://адресат:порт_адресата?pkt_size=564;  # тоже самое тоько попутно кодируем звук в mp3
     }
}