梦见你的名字 发表于 2024-8-28 11:33:55

部署RTMP(nginx)视频流服务

利用 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下载到该文件夹下。
https://i-blog.csdnimg.cn/direct/dd4f78557c4c4aefac13f6b720e4cd16.png
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
https://i-blog.csdnimg.cn/direct/c0e90e2ac96a41d4b17afa096b9a3d90.png
配置并编译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
https://i-blog.csdnimg.cn/direct/8ab84baf20314bda81a12ee965ffdf14.png
运行测试nginx

进入安装目次/usr/local/nginx,运行下令./sbin/nginx
        cd /usr/local/nginx
        ./sbin/nginx
打开欣赏器在地址栏输入:localhost。假如
配置 RTMP 视频点播

打开配置文件nginx.conf,添加RTMP的配置。
worker_processes1;
events {
   worker_connections1024;
}
rtmp {                #RTMP服务 ** 添加即可 **
   server {
       listen 1935;#//服务端口
   chunk_size 4096;   #//数据传输块的大小


   application vod {
       play /opt/video/vod; #//视频文件存放位置。
   }
   }
}
http {
   include       mime.types;
   default_typeapplication/octet-stream;
   sendfile      on;
   keepalive_timeout65;
   server {
       listen       80;
       server_namelocalhost;
       location / {
         root   html;
         indexindex.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/

#usernobody;
worker_processes1;

#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;

#pid      logs/nginx.pid;


events {
    worker_connections1024;
}

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:admin@10.10.10.11/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_typeapplication/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_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_loglogs/access.logmain;

    sendfile      on;
    #tcp_nopush   on;

    #keepalive_timeout0;
    keepalive_timeout65;

    #gzipon;

    server {
      listen       80;
      server_namelocalhost;

      #charset koi8-r;

      #access_loglogs/host.access.logmain;

      location / {
            root   html;
            indexindex.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_page404            /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_indexindex.php;
      #    fastcgi_paramSCRIPT_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 {
      #    denyall;
      #}
    }


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

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


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_namelocalhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_keycert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout5m;

    #    ssl_ciphersHIGH:!aNULL:!MD5;
    #    ssl_prefer_server_cipherson;

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

}

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

无人机配置
https://i-blog.csdnimg.cn/direct/1b43405ef9bf471897a13b49efb0a1d3.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 部署RTMP(nginx)视频流服务