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> 

그리고 저는 아주 잘 일하고 있어요

관련 정보