使用nginx+nginx-rtmp-module搭建流媒体服务器

/ 0评 / 0

周末闲来无事,正好最近在玩直播,就想拿自己的闲置的树莓派做个直播服务器,做下来其实还是很简单的,开搞。
首先我给树莓派装了一个全新的系统,选的是kali,其实跟树莓派官方的系统用起来差不了太多,都是Debain系的,先更新软件源

sudo apt-get update && sudo apt-get upgrade

然后安装依赖软件

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

下载安装nginx和rtmp模块,编译安装

wget http://nginx.org/download/nginx-1.11.8.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
tar -zxvf nginx-1.11.8.tar.gz
unzip master.zip
cd nginx-1.11.8

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

我这边在make的时候遇到了 [-Werror=unused-but-set-variable] 这样的错误,解决办法是直接修改nginx安装包下/objs/Makefile文件中的-Werror,去掉 -Werror

CC =gcc
CFLAGS =  -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -D_LARGEFILE_SOURCE -DBUILDING_NGINX
CPP =
gcc -E
LINK =
$(CC)

这样就OK了,顺利编译安装通过,接下来配置 nginx

vim /usr/local/nginx/conf/nginx.conf 

添加下面的配置

rtmp {    
     server {    
         listen 1935;    
         application myapp {    
              live on;    
         }    
         application hls {    
              live on;    
              hls on;    
              hls_path /tmp/hls;    
         }    
     }    
} 

启动nginx服务器

#启动nginx
sudo /usr/local/nginx/sbin/nginx
#或者
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  

启动不报错,访问 http://ip 看到了nginx 的欢迎界面,安装成功!下面我们来推一次流,我这里使用了两种方法,第一种直接使用的 ffmpeg 来推的

ffmpeg -re -i ./test.mp4 -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://192.168.2.107:1935/myapp/test1

另一种方法使用的是使用将来会用到的LFLiveKit,自己把库克隆下来,修改demo里LFLivePreview.m中的链接

if (_self.startLiveButton.selected) {
        [_self.startLiveButton setTitle:@"结束直播" forState:UIControlStateNormal];
        LFLiveStreamInfo *stream = [LFLiveStreamInfo new];
        stream.url = @"rtmp://192.168.2.107:1935/myapp/test1";
        [_self.session startLive:stream];
} else {
        [_self.startLiveButton setTitle:@"开始直播" forState:UIControlStateNormal];
        [_self.session stopLive];
}

使用VLC直接可以看到直播,成了!

评论已关闭。