0


部署RTMP(nginx)视频流服务

RTMP 服务端配置

使用 Root 用户进行配置

: 切换 root 用户

     sudo(superuser do或 switch user do,用于提权)
     格式:sudo -u USERNAME COMMAND
 sudo -i
     ​su​(switch user 或 substitute,用于切换用户)
     格式:su -l USERNAME(-l为login,即登陆的简写)
    -l 可以将l省略掉,所以此命令常写为su - USERNAME
    三种方式切换到root的命令:su,su -和su - root
su -l

切换到 /etc 目录,创建并切换 rtmpServer 文件夹

    cd /etc
    mkdir rtmpServer
    chmod 777 rtmpServer  #(修改文件夹权限)
    cd rtmpServer

将nginx-rtmp-module和nginx下载到该文件夹下。
在这里插入图片描述

Nginx

Nginx HTTP服务器,是用于 Web 服务、反向代理、内容缓存、负载均衡、媒体流传输等场景的开源软件。 这个流媒体服务器

安装 Nginx 1.8.1

下载地址 Nginx
使用 wget 下载

wget http://nginx.org/download/nginx-1.8.1.tar.gz

解压 nginx

tar -zxvf nginx-1.8.1.tar.gz

安装nginx的依赖库(可能存在环境变量版本等问题)

    sudo apt-get update
    sudo apt-get install libpcre3 libpcre3-dev
    sudo apt-get install openssl libssl-dev

Nginx-rtmp-module

nginx-rtmp-module Nginx 一个nginx的流媒体服务器模块,可以支持RTMP和HLS(Live Http Stream)。
下载 nginx-rtmp-module

git clone https://github.com/arut/nginx-rtmp-module.git

在这里插入图片描述

配置并编译nginx

进入到nginx-1.8.1安装目录, 进入nginx源码目录,修改configure配置文件
添加nginx的rtmp模块。 add-module为下载的nginx-rtmp-module文件路径。

cd nginx-1.8.1
*[注释]:
    修改配置 
        ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=../nginx-rtmp-module

*[注释]
    默认配置
        ./configure --add-module=../nginx-rtmp-module
make    # 环境版本 问题影响安装
sudo make install

在这里插入图片描述

运行测试nginx

进入安装目录/usr/local/nginx,运行命令./sbin/nginx

    cd /usr/local/nginx
    ./sbin/nginx

打开浏览器在地址栏输入:localhost。如果

配置 RTMP 视频点播

打开配置文件nginx.conf,添加RTMP的配置。

worker_processes  1;
events {
   worker_connections  1024;
}
rtmp {                #RTMP服务 ** 添加即可 **
   server {
       listen 1935;  #//服务端口
   chunk_size 4096;   #//数据传输块的大小

   application vod {
       play /opt/video/vod; #//视频文件存放位置。
   }
   }
}
http {
   include       mime.types;
   default_type  application/octet-stream;
   sendfile        on;
   keepalive_timeout  65;
   server {
       listen       80;
       server_name  localhost;
       location / {
           root   html;
           index  index.html index.htm;
       }
       
       location /stat {    #第二处添加的location字段。
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl { #第二处添加的location字段。
            root /etc/rtmpServer/nginx-rtmp-module/;
        }

       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
           root   html;
       }
   }
}

rtmp 浏览器不能直接播放 可转为 hls/

#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_auto_push off;
rtmp_auto_push_reconnect 1s;
rtmp_socket_dir /tmp;

rtmp {                #RTMP服务
    server {
        listen 1935;  #//服务端口
        drop_idle_publisher 30s; #// 缓存时长
        chunk_size 4096;   #//数据传输块的大小

        application live { #创建一个叫live的应用
             live on;#开启live的应用
             allow publish 127.0.0.1;#
             allow play all;
             # exec_pull ffmpeg -i rtsp://admin:[email protected]/axis-media/media.amp -threads 2 -f flv -r 25 -s 1280x720 -an rtmp://localhost:1935/cam1/stream 2>>/var/log/nginx/ffmpeg.log;
            interleave off;
            #exec_options on;
            #exec_push ffmpeg -i rtmp://localhost/live/test -c copy -f flv rtmp://localhost/live/test;
            hls on; #开启hls
            hls_path /etc/rtmpServer/nginx-1.8.1/html/hls;
            #hls的ts切片存放路径
            hls_fragment 2s;
            #本地切片长度
            hls_playlist_length 6s; 
        }
        application hls {
            live on;
            hls off;
            #gop_cache off;
            #gop_max_frame_count 256;
            #gop_max_video_count 128;
            #gop_max_audio_count 128;
            #wait_key on;
            #hls_path www/hls;
            #hls_fragment 2s;
            #hls_playlist_length 10s;
        }
        application vod {
            play /opt/video/vod; #//视频文件存放位置。
        }
    
    }
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    #flv_live on;
    chunked_transfer_encoding on;
    add_header Cache-Control no-cache;
    add_header Access-Control-Allow-Origin * always;
    add_header Access-Control-Allow-Headers X-Requested-With;
    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

    access_log off;
    #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;
            
            add_header Access-Control-Allow-Origin '*' always;
            add_header Access-Control-Allow-Headers '*';
            add_header Access-Control-Allow-Methods '*';
            add_header Access-Control-Allow-Credentials 'true';
        }
        location /stat {    #第二处添加的location字段。
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl { #第二处添加的location字段。
            root /etc/rtmpServer/nginx-rtmp-module/;
        }

        #配置hls点播
        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            alias /etc/rtmpServer/nginx-1.8.1/html/hls;
            add_header Cache-Control no-cache;
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Allow-Credentials' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, HEAD, OPTIONS';
            add_header 'Access-Control-Expose-Headers' 'Server,range,Content-Length,Content-Range';
        }

        #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;
    #    }
    #}

}

无人机连接 使用 rtmp 开启直播

无人机配置
请添加图片描述


标签: nginx 运维

本文转载自: https://blog.csdn.net/qq_46551351/article/details/140429154
版权归原作者 小白∪ 所有, 如有侵权,请联系我们删除。

“部署RTMP(nginx)视频流服务”的评论:

还没有评论