如何使用 Nginx RTMP 流媒体服务器减少延迟

如何使用 Nginx RTMP 流媒体服务器减少延迟

我的虚拟服务器配置了3GB内存和1个核心。

我正在播放以下 mp4 文件示例 MP4 视频文件通过我的 NGINX RTMP 服务器,因为small.mp4。我遇到了延迟问题。

这是我的nginx.conf

rtmp {
    server {
        listen 1935;
        chunk_size 4000;

        # video on demand for flv files
        application live {
            play /usr/local/nginx/html;
        }

        # video on demand for mp4 files
        application live360 {
            play /usr/local/nginx/html;
        }
    }
}

# HTTP can be used for accessing RTMP stats
http {
    access_log /var/log/nginx/access-streaming.log;
    error_log /var/log/nginx/error-streaming.log;

    server {
        # in case we have another web server on port 80
        listen 8080;

        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            # XML stylesheet to view RTMP stats.
            # Copy stat.xsl wherever you want
            # and put the full directory path here
            root /usr/local/nginx/html;
        }

        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /tmp/app;
            expires -1;
        }
    }
}    

答案1

这是我的配置,我有一个 rtmp 服务器,在同一个服务器中有一个带有视频 hls.js 库的 nginx,使用 ffmpeg 时我的延迟为 6 秒,在此之前的延迟为 15 秒

我的配置

worker_processes  1;


rtmp {
    server {
    listen 1935;

        hls on;
        hls_path /tmp/hls;

        # Use HLS encryption
        #hls_keys on;

        # Store auto-generated keys in this location rather than hls_path
        #hls_key_path /tmp/keys;

        # Prepend key url with this value
        #hls_key_url https://example.com/keys/;

        # Change HLS key every 2 fragments
        #hls_fragments_per_key 2;
        application live {
            live on;
            # Turn on HLS
            hls on;
            hls_path /tmp/live/;
        # Use HLS encryption
        #hls_keys on;

        # Store auto-generated keys in this location rather than hls_path
        #hls_key_path /tmp/keys;

        # Prepend key url with this value
        #hls_key_url https://example.com/keys/;

        # Change HLS key every 2 fragments
        #hls_fragments_per_key 2;
        application live {
            live on;
            # Turn on HLS
            hls on;
            hls_path /tmp/live/;
            hls_fragment 1;
            hls_playlist_length 10;
            # disable consuming the stream from nginx as rtmp
            deny play all;
            hls_continuous on;
        }
    }
}
http {
    server {
    listen 8080;

        location / {
            # Disable cache
            add_header 'Cache-Control' 'no-cache';

            # CORS setup
            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;
            }

            types {
                application/dash+xml mpd;
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            root /tmp/live/;
        }
    }
}

这是 ffmpeg 的代码

ffmpeg -re -i video.mp4 -c:v libx264 -preset veryfast -maxrate 3000k -bufsize 6000k -pix_fmt yuv420p -g 50 -c:a aac -b:a 160k -ac 2 -ar 44100 -f flv -maxrate 1.6M rtmp://192.168.1.27/live/key

以及带有代码的库

<script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
  <div class="content">
    <div class="content-logo_ca"></div>
    <div class="content-video">
      <video autoplay="true" controls id="videohls"></video>

    </div>
  </div>
  <script>
    var video = document.getElementById('videohls');
    if(Hls.isSupported()) {
      var hls = new Hls({liveSyncDuration:3});
      hls.loadSource('http://192.168.1.27:8080/key.m3u8');
      hls.attachMedia(video);       
      hls.on(Hls.Events.MANIFEST_PARSED,function() {
       hls.startLoad();
       video.play();
      });
    }
    else if (video.canPlayType('application/vnd.apple.mpegurl')) {
      video.src = 'http://192.168.1.27:8080/key.m3u8';
      video.addEventListener('loadedmetadata',function() {
    hls.autoLevelEnabled = false;
    hls.loadLevel = 3;
        video.play();
      });
    }
    </script> 

对我来说效果很好

相关内容