Назад | Перейти на главную страницу

Как уменьшить задержку с помощью потокового сервера Nginx RTMP

Мой виртуальный сервер настроен с 3 ГБ памяти и 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;
    }
}    

это моя конфигурация, у меня есть сервер rtmp и на том же сервере nginx с видео библиотекой hls.js, у меня задержка 6 секунд с ffmpeg, у меня задержка до этого 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> 

и для меня работает очень хорошо