FFmpeg+nginx流媒体服务框架搭建

442 阅读2分钟

一、.介绍

在这里插入图片描述

摄像头提供视频流=》内网服务器解码=》外网服务器提供访问

  • 外网服务器可以使用nginx将指定的请求拦截转发到内网服务器
  • 内网服务器使用ffmpeg把摄像头的视频流拉过来进行转码(例如h264转flv)然后推流到nginx中让nginx提供外网服务器调用

二、框架搭建

2.1 支持flv视频流的nginx

下载flv模块

 https://github.com/winshining/nginx-http-flv-module

下载nginx源码

http://nginx.org/en/download.html

进入到nginx目录中进行配置编译

./configure --prefix=/usr/local/nginx编译完成存储路径 - -with-http_ssl_module --add-module=/nginx-http-flv-module模块路径

然后进行编译

make
make&install

编译完成后就可以将编译好的nginx拷贝到服务器使用
nginx配置:nginx接收rtmp流1935端口,对外提供访问是9001/live

user rooty;
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  4096;
}


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       9001;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        location /live {
            flv_live on; #打开 HTTP 播放 FLV 直播流功能
            chunked_transfer_encoding on; #支持 'Transfer-Encoding: chunked' 方式回复

            add_header 'Access-Control-Allow-Origin' '*'; #添加额外的 HTTP 头
            add_header 'Access-Control-Allow-Credentials' 'true'; #添加额外的 HTTP 头
        }

        location /control {
            rtmp_control all; #rtmp 控制模块的配置
        }

        #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_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; #log 模块在 access.log 中记录日志的间隔时间,对调试非常有用
    log_size     1m; #log 模块用来记录日志的缓冲区大小

    server {
        listen 1935;

        application myapp {
            live on;
            gop_cache on; #打开 GOP 缓存,减少首屏等待时间
        }
    }
}

推流路径

rtmp://127.0.0.1:1935/nginx配置的app名称/流自定义名称

对外提供的访问路径

http://nginx地址:9001/live?port=1935&app=nginx配置的app名称&stream=流自定义名称

2.2 安装yasm和FFmpeg

注:yasm用于编译ffmpeg
ffmpeg官网下载: ffmpeg.org/download.ht…
安装yasm官网下载:yasm.tortall.net/Download.ht…

2.2.1 安装yasm

解压:

tar -zxvf yasm-1.3.0.tar.gz

在这里插入图片描述
检查:

cd yasm-1.3.0
./configure

在这里插入图片描述
安装:

make 
sudo make install

在这里插入图片描述

2.2.1 安装FFmpeg

解压:

tar -xvzf ffmpeg-4.3.2.tar.gz
cd ffmpeg-4.3.2

检查:

./configure --enable-gpl --enable-libx264 --prefix=/opt/ffmpeg

在这里插入图片描述
安装:

make #巨慢
sudo make install # 会把ffmpeg相关执行程序、头文件、lib库安装在/opt/ffmpeg/下

配置lib路径:

  • 创建环境变量
    在这里插入图片描述
    注:Ld不是id
  • 配置环境变量在这里插入图片描述
  • 更新环境变量
sudo ldconfig
  • 验证是否成功
    在这里插入图片描述
    配置软连接类似于快捷方式:
ln -s /opt/ffmpeg/bin/ffmpeg /usr/local/bin/ffmpeg

在这里插入图片描述

三、测试

推流转码

ffmpeg -re -i rtmp://58.200.131.2:1935/livetv/hunantv -c copy -f flv rtmp://127.0.0.1:1935/nginxapp名称/流名称

访问路径

http://nginx地址:9001/live?port=1935&app=nginx配置的app名称&stream=流自定义名称