tiangolo / nginx-rtmp-docker

Docker image with Nginx using the nginx-rtmp-module module for live multimedia (video) streaming.
MIT License
1.1k stars 365 forks source link

HLS #18

Closed wilhelm10243 closed 1 month ago

wilhelm10243 commented 3 years ago

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

che1974 commented 3 years ago

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

charlesbrandt commented 3 years ago

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!

BenceUszkai commented 2 years ago

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

ndmgrphc commented 2 years ago

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

robinp commented 2 years ago

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 commented 2 years ago

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.

ndmgrphc commented 2 years ago

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...