ffmpeg rtsp转flv挂载到web端(Linux版)

发布时间 2023-07-27 10:40:56作者: striver-sc

ffmpeg rtsp转flv挂载到web端(Linux版)

  1. 下载ffmpeg

image-20230718173453821

image-20230718173541096

解压目录

image-20230718173616408

Linux安装nginx集成nginx-http-flv-module

Linux安装nginx运行环境

  • 安装gcc-c++编译器

    yum install gcc-c++
    yum install -y openssl openssl-devel
    
  • 安装pcre,zlib包

    yum install -y pcre pcre-devel
    yum install -y zlib zlib-devel
    

下载安装nginx和nginx-http-flv-module

  • 下载安装包nginx,nginx-http-flv-module

    Nginx

    nginx-http-flv-module

  • 新建目录/usr/local/mysoft,将下载好的压缩包放到mysoft目录下

  • 解压nginx

    tar -zxvf nginx-1.18.0.tar.gz
    cd nginx-1.18.0
    
  • 执行命令将nginx-http-flv-module模块添加到nginx执行安装

    ./configure --add-module=/usr/local/mysoft/nginx-http-flv-module --with-http_ssl_module
    make && make install
    
  • nginx会被安装到/usr/local/nginx目录下

  • 配置nginx.conf

    #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{
    	timeout             15s;
        drop_idle_publisher 15s;
    	server{
    		listen 1935; #接受推流的端口就
    		chunk_size 8192; #单一推流数据包的最大容量
    		application live {
              		 live on; #打开直播
               		 meta off; # 为了兼容网页前端的 flv.js,设置为 off 可以避免报错
               		 gop_cache on; # 支持GOP缓存,以减少首屏时间
               		 allow play all; # 允许来自任何 ip 的人拉流
           		 } 
    	}
    
    }
    
    
    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       7008;
            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;
    		add_header 'Access-Control-Allow-Origin' '*'; #允许跨域
    		add_header 'Access-Control-Allow-Credentials' 'true';
    		add_header 'Cache-Control' 'no-cache';
    	}
    
            #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;
        #    }
        #}
    
    }
    
    

    ffmpeg命令推送

    ffmpeg -rtsp_transport tcp -i  "rtsp://182.116.62.148:23702/07928543399589000101?DstCode=01&ServiceType=1&ClientType=1&StreamID=1&SrcTP=2&DstTP=2&SrcPP=1&DstPP=1&MediaTransMode=0&BroadcastType=0&SV=1&Token=BZ0peNzNqdPxEaUqs42wsgVo/6Ywlv7XF2XEwqd53No=&"(获取视频流地址) -c copy -f flv "rtmp://127.0.0.1:1935/live/10240"(要推送的地址 1935监控端口)
    

    生成的flv的url

    http://127.0.0.1:80/live?port=1935&app=live&stream=10240
    

    ffmpeg前端推流示例

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
    	<meta charset="UTF-8">
    	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	<title>video</title>
    </head>
    <script src="https://cdn.bootcss.com/flv.js/1.5.0/flv.js"></script>
    
    <body>
    	<div>
    		<video id="vVideo" width="600" height="500" controls />
    	</div>
    
    	<script>
    		//原生H5支持的媒体格式主要有MP4、OGG、WebM、M3U8
    		if (flvjs.isSupported()) {
    			var videoElement = document.getElementById('vVideo');
    			var flvPlayer = flvjs.createPlayer({
    				type: 'flv',
    				url: 'http://127.0.0.1:80/live?port=1935&app=live&stream=10240',
    				isLive: false,
                    hasAudio: false,
                    hasVideo: true,
                    enableStashBuffer: false,
                    changeOrigin: true
    			});
    			flvPlayer.attachMediaElement(videoElement);
    			flvPlayer.load();
    			flvPlayer.play();
    		}
    	</script>
    </body>
    
    </html>