首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何减少Nginx RTMP流服务器的延迟

如何减少Nginx RTMP流服务器的延迟
EN

Server Fault用户
提问于 2018-08-23 05:03:03
回答 1查看 16.5K关注 0票数 3

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

我正在通过我的NGINX服务器(作为mp4 )播放下面的示例MP4视频文件文件。我遇到了延迟问题。

这是我的nginx.conf

代码语言:javascript
运行
复制
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;
        }
    }
}    
EN

回答 1

Server Fault用户

发布于 2019-06-10 21:49:42

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

我的配置

代码语言:javascript
运行
复制
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的代码

代码语言:javascript
运行
复制
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

以及带有代码的库

代码语言:javascript
运行
复制
    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();
      });
    }

对我来说工作得很好

票数 0
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/927613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档