美文网首页
使用nginx实现rtmp服务器

使用nginx实现rtmp服务器

作者: 奶茶不要奶不要茶 | 来源:发表于2022-10-31 00:00 被阅读0次

本文使用的操作系统是Ubuntu20.04,nginx采用源码编译安装,最终实现效果是nginx提供rtmp服务。
安装nginx的依赖包,编译nginx依赖这些软件包,不然会出错。

apt update
apt install -y build-essential zlib1g-dev libpcre3 libpcre3-dev libssl-dev libxml2-dev libxslt-dev libgd-dev libjpeg-dev libpng-dev

下载nginx的源码包,我这里是nginx-1.20.2版本。

cd /usr/local/src
wget http://nginx.org/download/nginx-1.20.2.tar.gz
tar zxvf nginx-1.20.2.tar.gz
cd ./nginx-1.20.2

nginx未提供rtmp功能,rtmp由第三方模块提供,从github获取rtmp源码。

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

开始编译nginx,编译参数可以用我的,也可以自行删减。
另外注意rtmp模块的路径,我这里是拉取nginx-rtmp-module到nginx源码包目录中的。

./configure --prefix=/usr/local/nginx \
--with-debug \
--with-compat \
--with-threads \
--with-pcre \
--with-pcre-jit \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_auth_request_module \
--with-http_addition_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_image_filter_module \
--with-http_xslt_module \
--with-stream \
--add-module=./nginx-rtmp-module

configure没报错继续下一步。

make && make install

nginx安装成功后,打开nginx配置文件,在http{}范围外增加这些内容。

rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        
        application live {
            live on;
            record off;
        }
        application hls {
            live on;
            record off;
            hls on;
            hls_path /tmp;
        }
    }
}

启动nginx后使用ss -lnpt | grep 1935检查端口是否正常监听。

推流方式有很多种,我一般用的是ffmpeg和obs。
ffmpeg推流,-i指定数据来源,rtmp://是rtmp服务器。这里表示将mp4推流到localhost的rtmp服务器上。

ffmpeg -re -i demo.mp4 -c copy -f flv rtmp://localhost/live/demo-test

obs推流不做介绍了。拉流我一般用的是vlc和potplayer,比较喜欢potplayer吧,无边框播放真帅,yyds。

另外vlc和postplayer还支持播放h265的视频。

image.png image.png

相关文章

网友评论

      本文标题:使用nginx实现rtmp服务器

      本文链接:https://www.haomeiwen.com/subject/fudltdtx.html