nginx-rtmp-docker icon indicating copy to clipboard operation
nginx-rtmp-docker copied to clipboard

HLS

Open wilhelm10243 opened this issue 5 years ago • 7 comments
trafficstars

Is it possible to open the HLS Feature in the RTMP module?

wilhelm10243 avatar Nov 01 '20 10:11 wilhelm10243

ffmpeg -re -i "https://"
-vcodec copy -acodec copy -vbsf h264_mp4toannexb -f flv
rtmp://192.168.88.11/live/test

che1974 avatar Nov 06 '20 13:11 che1974

In a Dockerfile I put:

FROM tiangolo/nginx-rtmp

WORKDIR /mnt/hls/

# I prefer to configure this in docker-compose.yml
# COPY nginx.conf /etc/nginx/nginx.conf

Then in your nginx.conf file:

worker_processes auto;
rtmp_auto_push on;
events {}
rtmp {
    server {
        listen 1935;
        listen [::]:1935 ipv6only=on;    

        application live {
            live on;
            record off;

            hls on;
            hls_path /mnt/hls/live;
            hls_fragment 2s;
            hls_playlist_length 4s;

        }
    }
}

http {
    # Disable server tokens
    server_tokens off;

    # Include MIME types
    include mime.types;

    # Set timeout limit
    keepalive_timeout 65;

    server {
        listen 1999;      # HTTP IPv4
        listen [::]:1999; # HTTP IPv6
        # server_name example.com www.example.com # Your domain (RECOMMENDED BUT OPTIONAL)
        
        location / {
            # Disable cache
            add_header Cache-Control no-cache;

            # Enable CORS
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length';

            # Allow CORS preflight requests
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Content-Length' 0;
                return 204;
            }

            # Specify file type to be served (.m3u8)
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t;
            }

            # File location
            # Set to the same hls_path specified in the rtmp application
            root /mnt/hls;
        }
    }
}

I'm still trying to tune the settings for more reliable loading via HLS clients. Suggestions / feedback welcome!

charlesbrandt avatar May 31 '21 21:05 charlesbrandt

@wilhelm10243 , Daily.co solved this problem here: https://github.com/daily-demos/nginx-rtmp

BenceUszkai avatar Jan 09 '22 11:01 BenceUszkai

@charlesbrandt did you ever manage to tune the settings for reliable loading? We're trying to minimize delay down to 5-10 seconds.

ndmgrphc avatar Feb 01 '22 18:02 ndmgrphc

Hey - accidentally I opened https://github.com/tiangolo/nginx-rtmp-docker/pull/34. I couldn't get the delay down too much either - it seems regardless of the 5s chunk setting, the minimum is around 7-8s. Maybe one could see the reason for that in the module source code if wants to dig in.

robinp avatar Feb 23 '22 07:02 robinp

Actually configuring

            hls_fragment 1s;
            hls_max_fragment 2s;
            hls_playlist_length 5s;

got the delay down to 5 sec. The hls_max_fragment is in the source code, but not documented on the wiki. It seems could lead to initial glitch. Also this works well on local network, but not sure if would over internet.

robinp avatar Feb 23 '22 08:02 robinp

The absolute only solution we found was to increase the frequency of key frames. It solved all problems...

application live {
  live on;
  record off;

  # on_publish http://nginx/rtmp/on_publish;
  # on_done http://nginx/rtmp/on_done;
  # ...
  # re-encode and forward to rtmp://.../hls
  exec /usr/bin/ffmpeg -i rtmp://localhost/live/$name -c:v libx264 -g 15 -keyint_min 15 -preset faster -b:v 512K -s 854x480 -f flv -c:a libfdk_aac -ac 1 -strict -2 -b:a 192k rtmp://localhost/hls/$name;
}      

Even increasing keyframe frequency in OBS solved it...

ndmgrphc avatar Feb 23 '22 22:02 ndmgrphc