nginx+ffmpeg+rtsp+rtmp/http-flv 流媒体服务搭建【完整版】

nginx+ffmpeg+rtsp+rtmp/http-flv 流媒体服务搭建【完整版】

安装nginx及其依赖

  • gcc安装

    yum -y install gcc gcc-c++
    
  • pcre安装

    #若拉不下来,可直接把链接复制到浏览器下载。我是直接在浏览器下载成功的,以下所有链接均同理。
    #若链接失效,可自行到网上下载
    wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
    tar -zxvf pcre-8.35.tar.gz
    cd pcre-8.35
    ./configure
    make
    sudo make install
    
  • zlib安装

    wget http://zlib.net/zlib-1.2.13.tar.gz
    tar -zxf zlib-1.2.13.tar.gz
    cd zlib-1.2.13
    ./configure
    make
    sudo make install
    
  • openssl安装

    #1
    wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz
    tar -zxvf openssl-fips-2.0.10.tar.gz
    cd openssl-fips-2.0.10
    ./config && make && make install
    #2
    yum -y install openssl openssl-devel
    
  • 下载nginx 以及 nginx-http-flv-module

    nginx-http-flv-module

    #nginx-http-flv-module: https://github.com/winshining/nginx-http-flv-module
    git clone https://github.com/winshining/nginx-http-flv-module.git
    #下面这个我没成功克隆下来,我是直接开科学,去浏览器下载的。解压后放进服务器
    #网址:https://github.com/winshining/nginx-http-flv-module.git
    

    nginx

    wget http://nginx.org/download/nginx-1.10.2.tar.gz
    tar zxvf nginx-1.10.2.tar.gz
    cd nginx-1.10.2
    # 编译时指定nginx-http-flv-module的目录
    ./configure --add-module=/usr/local/nginx-http-flv-module-master
    make
    sudo make install
    
  • 修改nginx.conf

    vi /usr/local/nginx/conf/nginx.conf
    cd /usr/local/nginx/sbin
    #启动nginx
    cd /usr/local/nginx/sbin
    ./nginx
    #重新加载配置文件
    ./nginx -s reload
    

    配置文件如下,直接复制即可(只需要修改server_name为自己的服务器IP地址):

    worker_processes  1; #should be 1 for Windows, for it doesn't support Unix domain socket
    #worker_processes  auto; #from versions 1.3.8 and 1.2.5
    
    #worker_cpu_affinity  0001 0010 0100 1000; #only available on FreeBSD and Linux
    #worker_cpu_affinity  auto; #from version 1.9.10
    
    error_log logs/error.log error;
    
    #if the module is compiled as a dynamic module and features relevant
    #to RTMP are needed, the command below MUST be specified and MUST be
    #located before events directive, otherwise the module won't be loaded
    #or will be loaded unsuccessfully when NGINX is started
    
    #load_module modules/ngx_http_flv_live_module.so;
    
    events {
        worker_connections  4096;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        keepalive_timeout  65;
    
        server {
            listen       80;
    
            location / {
                root   /var/www;
                index  index.html index.htm;
            }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
            location /live {
                flv_live on; #open flv live streaming (subscribe)
                chunked_transfer_encoding  on; #open 'Transfer-Encoding: chunked' response
    
                add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
                add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
            }
    
            location /hls {
                types {
                    application/vnd.apple.mpegurl m3u8;
                    video/mp2t ts;
                }
    
                root /tmp;
                add_header 'Cache-Control' 'no-cache';
            }
    
            location /dash {
                root /tmp;
                add_header 'Cache-Control' 'no-cache';
            }
    
            location /stat {
                #configuration of streaming & recording statistics
    
                rtmp_stat all;
                rtmp_stat_stylesheet stat.xsl;
            }
    
            location /stat.xsl {
                root /var/www/rtmp; #specify in where stat.xsl located
            }
    
            #if JSON style stat needed, no need to specify
            #stat.xsl but a new directive rtmp_stat_format
    
            #location /stat {
            #    rtmp_stat all;
            #    rtmp_stat_format json;
            #}
    
            location /control {
                rtmp_control all; #configuration of control module of rtmp
            }
        }
    }
    
    rtmp_auto_push on;
    rtmp_auto_push_reconnect 1s;
    rtmp_socket_dir /tmp;
    
    rtmp {
        out_queue           4096;
        out_cork            8;
        max_streams         128;
        timeout             15s;
        drop_idle_publisher 15s;
    
        log_interval 5s; #interval used by log module to log in access.log, it is very useful for debug
        log_size     1m; #buffer size used by log module to log in access.log
    
        server {
            listen 1935;
            server_name www.test.*; #for suffix wildcard matching of virtual host name
    
            application myapp {
                live on;
                gop_cache on; #open GOP cache for reducing the wating time for the first picture of video
            }
    
            application hls {
                live on;
                hls on;
                hls_path /tmp/hls;
            }
    
            application dash {
                live on;
                dash on;
                dash_path /tmp/dash;
            }
        }
    
        server {
            listen 1935;
            server_name *.test.com; #for prefix wildcard matching of virtual host name
    
            application myapp {
                live on;
                gop_cache on; #open GOP cache for reducing the wating time for the first picture of video
            }
        }
    
        server {
            listen 1935;
            server_name www.test.com; #for completely matching of virtual host name
    
            application myapp {
                live on;
                gop_cache on; #open GOP cache for reducing the wating time for the first picture of video
            }
        }
    }
    

ffmpeg及其依赖安装

  • 安装yasm

    wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
    tar zxvf yasm-1.3.0.tar.gz
    cd yasm-1.3.0
    ./configure
    make
    sudo make install
    
  • 安装 nasm(x264依赖)

    wget https://www.nasm.us/pub/nasm/releasebuilds/2.14/nasm-2.14.tar.gz --no-check-certificate
    tar -zxvf  nasm-2.14.tar.gz 
    cd nasm-2.14
    ./configure
    make && make install 
    
    #添加PATH  至/etc/profile
    export PATH=$PATH:/usr/local/bin
    source /etc/profile
    
    
  • 安装x264(要有这个,才能采用h264编码)

    yum install git #如果没有git 先安装git
    git clone https://code.videolan.org/videolan/x264.git
    cd x264
    #以下命令运行,我记得好像有个报错,如果有,查一查就能解决,解决之后重新运行这个命令
    ./configure --enable-shared 
    ## --enable-shared 参数需要带上,不然只有安装x264命令而没有生成相关lib 库文件
    make && make install
    
  • ffmpeg安装

    wget http://www.ffmpeg.org/releases/ffmpeg-5.0.1.tar.gz
    tar -zxvf   ffmpeg-5.0.1.tar.gz
    cd  ffmpeg-5.0.1
    ./configure --enable-shared --enable-swscale --enable-gpl --enable-nonfree --enable-pic --prefix=/usr/local/ffmpeg  --enable-postproc --enable-pthreads --enable-static --enable-libx264
    
    make && make install
    
  • 添加环境变量及编辑/etc/ld.so.conf 添加相关库文件的目录

    vi /etc/profile
    # 在最后PATH添加环境变量:
    export PATH=$PATH:/usr/local/ffmpeg/bin
    # 保存退出
    source /etc/profile
    # 将ffmepg的lib目录链接到系统库中
    vi /etc/ld.so.conf
    # 在文档后追加内容: 
    #/usr/local/ffmpeg/lib/(ffmpeg 的编译目录) /usr/local/lib/(x264的编译生成的库文件)
    #添加完后执行ldconfig,使配置生效
    ldconfig
    # 查看版本,测试是否生效
    ffmpeg --version
    

到此ffmpeg和nginx都已经安装完成了,接下只需要执行ffmepg相关的命令开始推流进行转换操作,之后配合nginx,访问转换后的http,就可以在页面上实现实时预览了。

ffmpeg推流命令

以下是我亲测的清晰且不延迟参数命令,你们只需要将rtsp替换为自己的,以及将rtmp替换为本机即可。

ffmpeg -rtsp_transport tcp -probesize 32 -analyzeduration 5000000 -i rtsp://admin:Hik123456@192.168.0.241:554/h264/ch1/main/av_stream -an -c:v libx264 -tune zerolatency -preset ultrafast -b:v 1000k -r 30 -g 15 -c:a aac -f flv rtmp://127.0.0.1:1935/cr/one

后台运行

nohup ffmpeg -rtsp_transport tcp -probesize 32 -analyzeduration 5000000 -i rtsp://admin:Hik123456@192.168.0.241:554/h264/ch1/main/av_stream -an -c:v libx264 -tune zerolatency -preset ultrafast -b:v 1000k -r 30 -g 15 -c:a aac -f flv rtmp://127.0.0.1:1935/cr/one &

要推多个,将rtmp链接最后的one修改为其他的即可。cr是在nginx.conf中配置好的application名称。

  • 显示包含"ffmpeg"的所有进程

    #查看有哪些url在运行
    ps aux | grep ffmpeg
    

如果有问题欢迎联系我,最近一段时间我在搞视频流以及海康相关的东西,想跟各位大佬多多交流。

以上我的这篇文章是根据下面这篇,实践后出现新的问题和安装内容,整合的完整版。
参考:https://blog.csdn.net/weixin_55775322/article/details/132338524?spm=1001.2014.3001.5506
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值