一、流媒体服务器介绍

        接下来的文章介绍下目前常用的流媒体服务器,并实际部署下部分服务器,和小伙伴们一起感受下流媒体服务器是如何工作的。

        市面上优秀的流媒体服务器解决方案有很多,比如SRS,Red5,EasyDarwin,nginx-rtmp,live555,mediasoup等等。

        这些服务器框架各有优缺点,但你需要明白的是,世上没有一款完美的流媒体服务器解决方案,在流媒体选型的时候,需要根据自己的业务选择适合的流媒体服务器。

二、nginx-rtmp 的部署

        本篇文章,我们详细介绍下使用nginx-rtmp来部署一套流媒体服务器,并用FFmpeg来推流,VLC来拉流。

        RTMP是Real Time Messaging Protocol(实时消息传输协议)的首字母缩写。该协议基于TCP,是一个协议族,包括RTMP基本协议及RTMPT/RTMPS/RTMPE等多种变种。RTMP是一种被设计用来进行实时数据通信的网络协议,主要用来在Flash/AIR平台和支持RTMP协议的流媒体/交互服务器之间进行音视频和数据通信。支持该协议的软件包括Adobe Media Server/Ultrant Media Server/red5等。RTMP与HTTP一样,都属于TCP/IP四层模型的应用层。

1.下载nginx 和 nginx-rtmp-module 

        浏览器输入地址,直接下载。

https://nginx.org/en/download.htmlhttps://github.com/arut/nginx-rtmp-module.git

 

 2.上传到linux服务器,然后解压压缩包

        在/opt 下新建文件夹存放压缩包。比如 mkdir  nginx-rtmp,然后将文件上传文件夹下解压缩。

$tar xvf nginx-1.20.1.tar.gz$unzip nginx-rtmp-module-master.zip

3.创建build目录

$cd nginx-1.20.1$mkdir build

4.config & make & make install

(1)./configure --prefix=/opt/nginx-rtmp/nginx-1.20.2/build --add-module=/opt/nginx-rtmp/nginx-rtmp-module-1.2.2/

(2)make(3)make install

补充:config过程中错误及错误处理

./configure: error: the HTTP rewrite module requires the PCRE library.
yum -y install pcre-devel

./configure: error: SSL modules require the OpenSSL library.
yum -y install openssl openssl-devel

./configure如果输出not found等不需要管,直接进行make;./configure是对nginx进行配置。

5.修改nginx配置文件

        进入/opt/nginx-rtmp/nginx-1.20.2/build/conf/nginx.conf,增加rtmp服务器配置


#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

rtmp {
    server {
        listen 1935;
        chunk_size 4096;

        # live on
        application rtmp_live {
            live on;
            #hls on; #这个参数把直播服务器改造成实时回放服务器。
            #wait_key on; #对视频切片进行保护,这样就不会产生马赛克了。
            #hls_path ./sbin/html; #切片视频文件存放位置。
            #hls_fragment 10s;     #每个视频切片的时长
            #hls_playlist_length 60s;  #总共可以回看的时间,这里设置的是1分钟。
            #hls_continuous on; #连续模式。
            #hls_cleanup on;    #对多余的切片进行删除。
            #hls_nested on;     #嵌套模式。
        }

        #play videos
        application rtmp_play{
            play ./videos;  #build directory
        }
    }
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

nginx报错:nginx: [emerg] unknown directive in /etc/nginx/conf.d/test.conf:4

解决: 第四行出现了 tab 空格 , 换成正常的空格即可

6.启动 nginx

cd /opt/nginx-rtmp/nginx-1.20.2/build

sudo ./sbin/nginx

7.ffmpeg推流

ffmpeg -stream_loop -1 -v verbose -re -i sample_yuancore_720p.mp4  -f flv rtmp://192.168.41.115:1935/rtmp_live/mystream

-stream_loop -1 无限循环推流
-v verbose 详细日志
-re 本来的码率推流
-c:a copy -c:v copy 省略 按照原来编码推流 

8.VLC拉流(直播、点播)

直播:rtmp://192.168.41.115:1935/rtmp_live/mystream
点播:rtmp://192.168.41.115:1935/rtmp_play/01.mp4
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐