DebianLinux树莓派手把手教你搭建一个直播服务器(Nginx+Rtmp)

xingyun86 2021-4-1 903

手把手教你搭建一个直播服务器(Nginx+Rtmp)

常用工具:

                ffmpeg音视频编解码 

                ffplay音视频播放工具

搭建流媒体服务器

                准备流媒体服务器linux

                准备并安装Nginx服务

                配置RTMP并启动Nginx服务

1.linxu安装Nginx和rtmp 

1.先下载安装  nginx 和 nginx-rtmp 编译依赖工具

sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev libz-dev

2. 创建一个工作目录,并切换到工作目录

mkdir ~/working
cd ~/working

3. 下载 nginx 和 nginx-rtmp源码

wget https://nginx.org/download/nginx-1.19.9.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip -o nginx-rtmp-module-master.zip

4. 安装unzip工具,解压下载的安装包

sudo apt-get install unzip

5.解压 nginx 和 nginx-rtmp安装包

tar -xvf nginx-1.19.9.tar.gz
unzip nginx-rtmp-module-master.zip

6. 切换到 nginx-目录

cd nginx-1.19.9

7.添加 nginx-rtmp 模板编译到 nginx

./configure --with-http_ssl_module --add-module=../nginx-rtmp-module-master

8.编译安装 

make
sudo make install
如果遇到错误error: this statement may fall through [-Werror=implicit-fallthrough=],解决方法:
make CFLAGS='-Wno-implicit-fallthrough'

9. 安装nginx init 脚本

sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults

10. 启动和停止nginx 服务,生成配置文件

sudo service nginx start
sudo service nginx stop

11. 安装 FFmpeg

Debian/Ubuntu Linux下:
sudo apt-add-repository ppa:jon-severinsson/ffmpeg
sudo apt-get update
sudo apt-get install ffmpeg
或
windows下
https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-full-shared.7z

 12. 配置 nginx-rtmp 服务器

打开 /usr/local/nginx/conf/nginx.conf

rtmp {
    server {
        listen 1935;#监听的端口
        chunk_size 4000;
        application hls {#rtmp推流请求路径
            live on;
            hls on;
            hls_path /home/pps/working/pub/html/hls;
            hls_fragment 5s;
        }
    }
}
以下配置为可选项:
拷贝stat.xsl到 /home/pps/working/pub/html目录.
$cp /home/pps/working/nginx-rtmp-module-master/stat.xsl /home/pps/working/pub/html/ -f

然后在nginx.conf中添加如下配置.
   http {
    server {
        ...
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl {
            root  /home/pps/working/pub/html;
        }
        ...           
      }
   }

13. 保存上面配置文件,然后重新启动nginx服务

sudo service nginx restart

如果使用手动启动,则配置:

打开 /usr/local/nginx/conf/nginx.conf

error_log /home/pps/working/pub/logs/error.log;
pid /home/pps/working/pub/logs/nginx.pid;
创建目录:
mkdir html
mkdir logs

启动脚本:(指定运行路径,前台运行)

/home/pps/working/pub/nginx -c /home/pps/working/pub/nginx.conf  -g "daemon off;" -p /home/pps/working/pub

14. 如果你使用了防火墙,请允许端口 tcp 1935

    用netstat -an | grep 1935

出现以下信息代表则代表启动nginx/rtmp成功

        netstat -an| grep 1935

        tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN 

16: 使用 客户端,使用 rtmp协议进行视频实时采集

ffmpeg直播命令:

        推流:ffmpeg -re -i 1.mp4 -c copy -f flv rtmp://xxx.xxx.xxx.xxx:1935/hls/test

        拉流:ffmpeg -i  rtmp://xxx.xxx.xxx.xxx:1935/hls/test -c copy dump.flv

        播放音视频:

                ffplay rtmp://xxx.xxx.xxx.xxx:1935/hls/test

                ffplay http://xxx.xxx.xxx.xxx:8081/hls/test.m3u8

17.下载一个OBS(视频直播客户端)

  (1)配置url

    (2)推流            

    (3)  服务器播放

                ffplay rtmp://xxx.xxx.xxx.xxx:1935/hls/test

                ffplay http://xxx.xxx.xxx.xxx:8081/hls/test.m3u8

18: 关于 nginx-rtmp 更多配置

https://github.com/arut/nginx-rtmp-module/wiki/Directives

flashplay播放器地址:  http://bbs.chinaffmpeg.com/1.swf

×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回