linux_手把手教ubuntu搭建rtmp视频推送服务
RTMP是流媒体协议。 它是Adobe的私有协议,未完全公开。 一般传输的是flv,f4v格式流。在TCP1个通道上传输命令和数据。
1,安装conda,ffmpeg,nginx,nginx-rtmp-module
(建议先修改主机pip,conda的源)
安装conda,创建环境::conda create -n rstp python=3.7
报错
top可看出内存不足,换个大内存机器安装ffmpeg
.0sudo add-apt-repository ppa:jonathonf/ffmpeg-4
sudo apt-get update
sudo apt-get install ffmpeg
如果使用了:sudo apt-get install ffmpeg(默认2.8),先卸载掉,避免干扰下载Nginx源码
http://nginx.org/download/nginx-1.17.10.tar.gz下载nginx-rtmp-modulegithub.com/arut/nginx-rtmp-module/archive/master.zip
在nginx源码路径下执行
./configure —add-module=/home/john/nginx-rtmp-module-master/(注意后面的路径是解压后的rtmp源码路径)报错:./configure: error: C compiler cc is not found
sudo apt-get install gcc
sudo apt-get install build-essential报错:./configure: error: the HTTP rewrite module requires the PCRE library.
sudo apt-get install libpcre3 libpcre3-dev报错:./configure: error: SSL modules require the OpenSSL library.
sudo apt-get install openssl libssl-dev报错:./configure: error: the HTTP gzip module requires the zlib library.
sudo apt install zlib1g-dev sudo make
sudo make install
验证:
成功后重启Nginx
restart nginx,输入机器外网ip,查看是否nginx默认页面检查是否成功:
/usr/local/nginx/sbin/nginx -V
是否包含:类似 --add-module=nginx-rtmp-module 访问nginx的welcome,确保nginx安装无问题
2,nginx配置修改
外层:
rtmp
{
server
{
listen 1935;
chunk_size 4096;
application live
{
live on;
}
}
}http的内层添加:
server {
listen 8080;
location /stat{
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl{
root /home/cjc/安装包/nginx/nginx-rtmp-module-master;
}
}报错:nginx: [emerg] unknown directive “rtmp” in /etc/nginx/nginx.conf
换nginx版本,本人采用了1.7,同时确保nginx使用的源码方式安装,如果apt-get安装,卸载干净重新安装此时nginx的配置如下:
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
rtmp
{
server
{
listen 1935;
chunk_size 4096;
application live
{
live on;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
location /stat{
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl{
root /home/john/nginx-rtmp-module-master;
}
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}3,推流和拉流
记得开放虚拟机端口,否则容易悲剧(telnet测试下远程机端口)
终端(循环推送):ffmpeg -stream_loop -1 -i 1.mp4 -f flv rtmp://localhost:1935/live/test1
vlc:rtmp://118.25.10.138:1935/live/test1奇怪的是
,端口1935去掉.终端(循环推送):ffmpeg -stream_loop -1 -i 1.mp4 -f flv rtmp://localhost/live/test1
vlc:rtmp://118.25.10.138/live/test1查了下rtmp默认端口就是1935,所以不加端口,实际就是从1935取得的,故确保远程主机放开端口1935
如何实现多路?
启动多个ffmpeg即可. 需要留意cpu和网络占用情况
rtmp://localhost/live/test2,rtmp://localhost/live/test3等等 如何实现帧率控制?
参数-r控制帧率
nohup ffmpeg -stream_loop -1 -r 1 -i 3.mp4 -f flv rtmp://localhost/live/3 &推送视频时,需要添加-re参数
FFmpeg推流遇到错误 Failed to update header with correct duration:https://blog.csdn.net/u013521296/article/details/98486941
并且最好推送自己的文件(文件夹有写入权限)虽解释不了原因,但曾遇到同一个文件,推送源是别人共享路径下的(别人共享的自己无写入权限),不行,将文件复制到自己目录下,就可以的情况。
4,docker
查资料时发现,已经有人制作了dofacker,可以直接使用.
https://hub.docker.com/r/datarhei/nginx-rtmp/
参考
https://blog.csdn.net/u014552102/article/details/86599289
https://blog.csdn.net/u014552102/article/details/86606465