文章
问答
冒泡
基于Nginx搭建rtmp流媒体服务器(nginx-rtmp-module)

前言

众所周知,Nginx是一个高性能的Http和反向代理服务器,它支持大量的第三方模块,可以在编译时加入来扩展功能。本文通过nginx-rtmp-module模块来扩展nginx支持RTMP流媒体的传输。

实现步骤

编译环境

  • ubuntu 22.04
  • nginx 1.18.0
  • nginx-rtmp-module v1.2.2

安装nginx和nginx-rtmp-module

# 安装依赖
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev

# 下载nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz

# 下载nginx-rtmp-module
git clone https://github.com/arut/nginx-rtmp-module.git

# 编译nginx
cd nginx-1.18.0
sudo ./configure --add-module=../nginx-rtmp-module
  # 忽视deprecated的方法
sudo make CFLAGS="-Wno-error=deprecated-declarations"
sudo make install

配置nginx

安装完后nginx目录在/usr/local/nginx,配置文件nginx.conf在conf目录下,执行文件nginx在sbin目录下

具体配置可以参考 https://github.com/arut/nginx-rtmp-module

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
  worker_connections  1024;
}

# rtmp config
rtmp  {
  server  {
    listen 1935;
    ping 30s;
    chunk_size 4096;
    record all;

    # TV mode: one publisher, many subscribers
    application live {
      live on;
    }
  }
}

http {
  include       mime.types;
  default_type  application/octet-stream;

  #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  #                  '$status $body_bytes_sent "$http_referer" '
  #                  '"$http_user_agent" "$http_x_forwarded_for"';

  #access_log  logs/access.log  main;

  sendfile        on;
  #tcp_nopush     on;

  #keepalive_timeout  0;
  keepalive_timeout  65;

  #gzip  on;

  server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
      root   html;
      index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   html;
    }

    # rtmp stat
    location /stat {
      rtmp_stat all;
      rtmp_stat_stylesheet stat.xsl;
    }

    location /stat.xsl {
      root /opt/develop/nginx-rtmp-module/;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
  }


  # another virtual host using mix of IP-, name-, and port-based configuration
  #
  #server {
  #    listen       8000;
  #    listen       somename:8080;
  #    server_name  somename  alias  another.alias;

  #    location / {
  #        root   html;
  #        index  index.html index.htm;
  #    }
  #}


  # HTTPS server
  #
  #server {
  #    listen       443 ssl;
  #    server_name  localhost;

  #    ssl_certificate      cert.pem;
  #    ssl_certificate_key  cert.key;

  #    ssl_session_cache    shared:SSL:1m;
  #    ssl_session_timeout  5m;

  #    ssl_ciphers  HIGH:!aNULL:!MD5;
  #    ssl_prefer_server_ciphers  on;

  #    location / {
  #        root   html;
  #        index  index.html index.htm;
  #    }
  #}

}

推流并测试

通过ffmpeg指令推流

ffmpeg -f gdigrab -framerate 30 -i desktop -vf "crop=1920:1080:0:0" -c:v libx264 -bf 0 -pix_fmt yuv420p -f flv rtmp://192.168.10.174/live/stream1

用vlc测试效果如下图

参考文章

https://cloud.tencent.com/developer/article/2388600

nginx

关于作者

TimothyC
天不造人上之人,亦不造人下之人
获得点赞
文章被阅读