引用自:利用nginx和rtmp在Ubuntu系统下搭建流服务器并测试推流拉流_RayChiu_Labloy的博客-CSDN博客_nginx rtmp ubuntu在一台Ubuntu服务器上搭建流服务器,然后在树莓派上利用ffmpeg推流,第三方PC端用VLC、ffplay拉流https://blog.csdn.net/RayChiu757374816/article/details/122606117
在中途遇到了一些原作者没有遇到过的错误
一、下载nginx-rtmp-module包
首先讲一下,流服务器会搭建在ubuntu18.04系统主机上,因此nginx-rtmp-module和下边的nginx都是在ubuntu18.04系统主机上的,因此从当前一到后边四的操作均在ubuntu18.04上
git clone https://github.com/arut/nginx-rtmp-module.git
二、安装nginx
wget http://nginx.org/download/nginx-1.21.5.tar.gz
tar -zvxf nginx-1.21.5.tar.gz
cd nginx-1.21.5
./configure --prefix=/usr/local/src/nginx --add-module=../nginx-rtmp-module --with-http_ssl_module
make -j8
make install
其中./configure的时候有可能会失败,报“..../configure: error: SSL modules require the OpenSSL library...”之类的错误,那么ubuntu这里的需要安装一个组件:
sudo apt-get install libssl-dev
报“...../configure: error: the HTTP rewrite module requires the PCRE library....”之类的错误需要安装nginx依赖:
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
sudo apt-get install openssl libssl-dev
还没有配环境变量,先启动测试一下nginx:
cd /usr/local/src/nginx/sbin
./nginx
ps -ef|grep nginx
在启动nginx提示:nginx: [alert] could not open error log file: open() “/usr/local/nginx/logs/error.log:
进入nginx目录
创建logs目录:mkdir logs
进入logs目录: cd logs
创建error.log和access.log文件:vi error.log vi access.log
ps:如果出现编辑上面两个文件的页面不知道怎么退出,可以依次按ESC->:wq->Enter退出
再次进入nginx/sbin目录执行启动nginx命令,成功了。
三、nginx配置环境变量
sudo vi ~/.bashrc
#然后写入下边这一行,注意对应自己的安装路径
export PATH=$PATH:/usr/local/src/nginx/sbin
source ~/.bashrc
四、在nginx.conf中配置rtmp:
rtmp {
server {
listen 1935;
application rtmplive {
live on;
max_connections 1024;
}
application zbcs {
live on;
record off;
}
application hls{
live on;
hls on;
hls_path /usr/hls; //保存ts文件地址,自己创建
hls_fragment 5s;
}
}
}
五、安装ffmpeg
1.安装yasm:
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar zxvf yasm-1.3.0.tar.gz
cd yasm-1.3.0/
./configure
make && sudo make install
2.安装ffmpeg:
wget https://ffmpeg.org/releases/ffmpeg-3.4.6.tar.gz
tar zxvf ffmpeg-3.4.6.tar.gz
cd ffmpeg-3.4.6/
./configure --prefix=/usr/local --enable-shared --enable-static
make && sudo make install
3.配置ld.so.conf增加动态链接库路径:
在文件 /etc/ld.so.conf 末尾加上 /usr/local/ffmpeg 根据你的安装路径来。
ldconfig #使配置生效的命令
4.配置环境变量:
vi ~/.bashrc
export PATH="/usr/local/ffmpeg/bin:$PATH"
source ~/.bashrc
5.验证ffmpeg:
ffmpeg -version
ffserver -version
六、启动nginx服务器后用ffmpeg推流
启动nginx:
nginx
在启动过程中可能会报错如下错误:nginx: [emerg] still could not bind()
这可能是端口被占用导致,nginx常用端口是80,解决方法如下:
# 查看端口使用情况
netstat -ntlp|grep 80
# kill掉占用80的端口
killall -9 nginx
# 在重新启动nginx
ffmpeg推流:
ffmpeg -re -i 123.mp4 -vcodec copy -codec copy -f flv rtmp://127.0.0.1:1935/hls/test
七、ffmpeg推流过程中用VLC拉流播放
接ffmpeg推流命令,这里我们使用了一个 mp4 视频文件;rtmp://127.0.0.1:1935/hls/mytest 表示将视频流通过 RTMP 推给流媒体服务器,这里因为我们的服务器和推流端都是开发板,所以这个 IP 地址127.0.0.1指的就是本机的流媒体服务器(即开发板IP地址)。
3、使用 VLC 软件进行拉流
安装VLC软件, 点击左上角“媒体” ---> “打开网络串流”:
测试ffmpeg调用usb摄像头推流
ffmpeg -f video4linux2 -i /dev/video0 -f flv rtmp://127.0.0.1:1935/hls/test
八、使用ffplay拉流
ffplay rtmp://192.168.1.210:1935/hls