linux_手把手教ubuntu搭建rtmp视频推送服务

hello886

原创; linux_手把手教ubuntu搭建rtmp视频推送服务

1,安装conda,ffmpeg,nginx,nginx-rtmp-module

(建议先修改主机pip,conda的源)
安装conda,创建环境::conda create -n rstp python=3.7
报错:Solving environment: | failed

top可看出内存不足,换个大内存机器

安装ffmpeg:4.0

sudo add-apt-repository ppa:jonathonf/ffmpeg-4
sudo apt-get update
sudo apt-get install ffmpeg
如果使用了:sudo apt-get install ffmpeg(默认2.8),先卸载掉,避免干扰

下载Nginx源码:wget http://nginx.org/download/ngi...
下载nginx-rtmp-module:wget github.com/arut/nginx-rtmp-module/archive/master.zip

在nginx源码路径下执行:sudo ./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
验证:
image

成功后重启Nginx:systemctl 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:12

换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 &

4,docker

查资料时发现,已经有人制作了dofacker,可以直接使用.
https://hub.docker.com/r/data...

参考

https://blog.csdn.net/u014552...
https://blog.csdn.net/u014552...

阅读 128
1 声望
0 粉丝
0 条评论
你知道吗?

1 声望
0 粉丝
宣传栏