CentOS obs直播RTMP协议推流到Nginx流媒体服务器到VUE使用HLS直播

1. 发布打包前端管理平台(ant-design-vue)

2. 发布打包后端(.NET6-WebApi)

.NET6 webAPI

orm: free sql

数据库:mysql5.7

3. 发布打包H5端(uni)

播放器使用:

import Player from 'xgplayer'; // npm install xgplayer
import HlsJsPlayer from "xgplayer-hls.js"; // 直播流m3u8(hls) npm install xgplayer-hls.js

4. 服务器安装.NET6环境:https://learn.microsoft.com/zh-cn/dotnet/core/install/linux-centos

安装 .NET 之前,请运行以下命令,将 Microsoft 包签名密钥添加到受信任密钥列表,并添加 Microsoft 包存储库。 打开终端并运行以下命令:
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
安装 SDK
.NET SDK 使你可以通过 .NET 开发应用。 如果安装 .NET SDK,则无需安装相应的运行时。 若要安装 .NET SDK,请运行以下命令:
sudo yum install dotnet-sdk-6.0
安装运行时
通过 ASP.NET Core 运行时,可以运行使用 .NET 开发且未提供运行时的应用。 以下命令将安装 ASP.NET Core 运行时,这是与 .NET 最兼容的运行时。 在终端中,运行以下命令:
 
sudo yum install aspnetcore-runtime-6.0

 

5. 服务器安装宝塔:https://www.bt.cn/new/download.html

yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh ed8484bec

6. 服务器安装supervisor进程守护

supervisord是服务相关的命令
supervisorctl是客户端相关的命令

安装命令:

yum install -y supervisor

查看状态:

supervisord -v 

卸载:

yum -y remove supervisor

在/etc创建文件目录supervisor

在/etc/supervisor下创建文件目录supervisord.d

在/etc/supervisor下上传文件supervisord.conf,内容如下:

; Sample supervisor config file.

[unix_http_server]
file=/run/supervisor/supervisor.sock   ; (the path to the socket file)
;chmod=0700                 ; sockef file mode (default 0700)
;chown=nobody:nogroup       ; socket file uid:gid owner
;username=user              ; (default is no username (open server))
;password=123               ; (default is no password (open server))

[inet_http_server]         ; inet (TCP) server disabled by default
port=*:9001        ; (ip_address:port specifier, *:port for all iface)
;username=user              ; (default is no username (open server))
;password=123               ; (default is no password (open server))

[supervisord]
logfile=/www/log/supervisor/supervisord.log  ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (log level;default info; others: debug,warn,trace)
pidfile=/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false              ; (start in foreground if true;default false)
minfds=1024                 ; (min. avail startup file descriptors;default 1024)
minprocs=200                ; (min. avail process descriptors;default 200)
;umask=022                  ; (process file creation umask;default 022)
;user=chrism                 ; (default is current user, required if root)
;identifier=supervisor       ; (supervisord identifier, default is 'supervisor')
;directory=/tmp              ; (default is not to cd during start)
;nocleanup=true              ; (don't clean up tempfiles at start;default false)
;childlogdir=/tmp            ; ('AUTO' child log dir, default $TEMP)
;environment=KEY=value       ; (key value pairs to add to environment)
;strip_ansi=false            ; (strip ansi escape codes in logs; def. false)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///run/supervisor/supervisor.sock ; use a unix:// URL  for a unix socket
serverurl=http://*:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as http_username if set
;password=123                ; should be same as http_password if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; The below sample program section shows all possible program subsection values,
; create one or more 'real' program: sections to be able to control them under
; supervisor.

;[program:theprogramname]
;command=/bin/cat              ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)
;autostart=true                ; start at supervisord start (default: true)
;autorestart=true              ; retstart at unexpected quit (default: true)
;startsecs=10                  ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A=1,B=2           ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

; The below sample eventlistener section shows all possible
; eventlistener subsection values, create one or more 'real'
; eventlistener: sections to be able to handle event notifications
; sent by supervisor.

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; start at supervisord start (default: true)
;autorestart=unexpected        ; restart at unexpected quit (default: unexpected)
;startsecs=10                  ; number of secs prog must stay running (def. 1)
;startretries=3                ; max # of serial start failures (default 3)
;exitcodes=0,2                 ; 'expected' exit codes for process (default 0,2)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups        ; # of stderr logfile backups (default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;environment=A=1,B=2           ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The below sample group section shows all possible group values,
; create one or more 'real' group: sections to create "heterogeneous"
; process groups.

;[group:thegroupname]
;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions
;priority=999                  ; the relative start priority (default 999)

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[include]
files = supervisord.d/*.conf ;这行默认是加载/etc/supervisord.d/目录内所有conf后缀的配置,喜欢用ini的可以改成ini
supervisord.conf

在/etc/supervisor/supervisord.d下上传文件xx.conf,内容如下:

[program:appname]
command=dotnet appname.dll --urls=http://*:5000   ;要执行的命令
directory=/www/api ;命令执行的目录
environment=ASPNETCORE__ENVIRONMENT=Production ;环境变量
user=root  ;进程执行的用户身份
stopsignal=INT
autostart=true ;是否自动启动
autorestart=true ;是否自动重启
startsecs=3 ;自动重启间隔
stderr_logfile=/www/log/appname.err.log ;标准错误日志
stdout_logfile=/www/log/appname.out.log ;标准输出日志
[supervisord]
[supervisorctl]

执行启动命令:

supervisord -c /etc/supervisor/supervisord.conf

启动成功后:执行下面命令查看,会出现一个

ps aux|grep supervisord
root     11589  0.0  0.0 112828   988 pts/0    S+   16:57   0:00 grep --color=auto supervisord
root     30510  0.0  0.4 245472 17220 ?        Ss   16:12   0:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisor/supervisord.conf

启动程序:

supervisorctl start  appname
supervisorctl stop  appname

查看状态:

supervisorctl status
appname    RUNNING   pid 20538, uptime 0:24:24

查看状态:浏览器输入:http://*:9001/

 

 查看端口以及appanme运行进程

lsof -i:5000
ps -f |grep appname
 配置Supervisor开机启动
systemctl enable supervisord.service
 检查是否开机启动
systemctl is-enabled supervisord

如果启动错误,查看日志

supervisorctl tail dcapi stdout

 

 log日志地址:/www/log/supervisor/supervisord.log

遇到错误:Unlinking stale socket /run/supervisor/supervisor.sock

解决办法:

执行命令:

nlink /run/supervisor/supervisor.sock

 

7. 宝塔安装Mysql5.7

 

8. 服务器安装Nginx(基于RTMP模式的)

没有git请输入以下命令

yum -y install git

开始下载nginx-rtmp,记住cd到一个盘符进行下载,建议cd ~,也可以直接从git下载了然后复制上去/www/server/nginx/nginx-rtmp-module

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

修改配置文件:添加内容:--add-module=/root/nginx-rtmp-module

vim /www/server/panel/install/nginx.sh

可以下载到本地修改,然后上传到服务器路径下:/www/server/panel/install/nginx.sh

修改位置:./configure --add-module=/root/nginx-rtmp-module --user=www ........................

 

 

使用sh命令安装

sh /www/server/panel/install/nginx.sh install1.22

9. Nginx配置 ,修改宝塔》软件管理》Nginx1.22管理》配置修改如下:

user  www www;
worker_processes auto;
error_log  /www/wwwlogs/nginx_error.log  crit;
pid        /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

stream {
    log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';
  
    access_log /www/wwwlogs/tcp-access.log tcp_format;
    error_log /www/wwwlogs/tcp-error.log;
    include /www/server/panel/vhost/nginx/tcp/*.conf;
}

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }
#RTMP服务,新增部分:
rtmp_auto_push on;
rtmp{
    server{
        listen 1935;        #服务端口
        chunk_size 4096;    #数据传输块的大小
        
        application vod{
            play ./vod;   #视频文件存放位置
        }
        application live{
            live on;                     #开启直播
            hls on;              
            hls_path /www/m3u8File;          
            hls_fragment 5s;             #每个视频切片的时长
            hls_playlist_length 16s;
            recorder myRecord {
                    record_path /rec;
            record all manual;
                    record_suffix -%Y-%m-%d-%H_%M_%S.flv;
            }
            hls_continuous on;          #连续模式
            hls_cleanup on;             #对多余的切片进行删除
            hls_nested on;              #嵌套模式
        }
    }
}
#RTMP服务,新增完毕


http
    {
        include       mime.types;
        #include luawaf.conf;

        include proxy.conf;

        default_type  application/octet-stream;

        server_names_hash_bucket_size 512;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_intercept_errors on;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        limit_conn_zone $binary_remote_addr zone=perip:10m;
        limit_conn_zone $server_name zone=perserver:10m;

        server_tokens off;
        access_log off;

server
    {
        listen 888;
        server_name phpmyadmin;
        index index.html index.htm index.php;
        root  /www/server/phpmyadmin;

        #error_page   404   /404.html;
        include enable-php.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /\.
        {
            deny all;
        } 
        access_log  /www/wwwlogs/access.log;
    }
include /www/server/panel/vhost/nginx/*.conf;
}
RTMP-Nginx配置

找到某个80端口的Nginx配置,修改:

server
{
    listen 80;
    server_name xxxxxxx;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/live;

    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    #SSL-END

    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END

    #PHP-INFO-START  PHP引用配置,可以注释或修改
    include enable-php-00.conf;
    #PHP-INFO-END
    try_files $uri $uri/ /index.html;
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/www.live.com.conf;
    #REWRITE-END

    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }

    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }

    #禁止在证书验证目录放入敏感文件
    if ( $uri ~ "^/\.well-known/.*\.(php|jsp|py|js|css|lua|ts|go|zip|tar\.gz|rar|7z|sql|bak)$" ) {
        return 403;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log /dev/null;
        access_log /dev/null;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log /dev/null;
        access_log /dev/null;
    }

#新增部分
    location /live{
            types{
                #m3u8 type设置
                application/vnd.apple.mpegurl m3u8;
                #ts分片文件设置
                video/mp2t ts;
            }
              #访问权限开启,否则访问这个地址会报403
                        autoindex on;
                        
            #指向访问m3u8文件目录
            alias /www/m3u8File;
            expires -1;
            add_header Cache-Control no-cache;
            #防止跨域问题
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';  
        }
         location /control{
            rtmp_control all;
        }
        
        location /stat{
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }
        location /stat.xsl{
            root ./nginx-rtmp-module;
        }
#新增完毕
    access_log  /www/wwwlogs/www.live.com.log;
    error_log  /www/wwwlogs/www.live.com.error.log;
}
拉流配置

在Nginx服务器位置创建一个目录:/www/server/panel/m3u8File

10. 开始直播及推流

OBS推流配置服务地址:rtmp://127.0.0.1:1935/live

串流密钥:001

然后开始推流,在服务器地址/www/server/panel/m3u8File下面会生成001的文件目录,然后001文件目录里面会出现很多*.ts文件,以及一个index.mu38文件

拉流地址: http://127.0.0.1/live/001/index.m3u8

拉流地址:rtmp://127.0.0.1:1935/live/001

拉流地址注意端口

本地测试可以使用VLC media player软件

 

posted @ 2022-09-26 17:24  彪悍的代码不需要注释  阅读(1191)  评论(2编辑  收藏  举报
39
0