RTMP 实时推流到web页面

1 . 安装nginx
参考:https://blog.csdn.net/TH_NUM/article/details/103199376

2. 下载安装 ffmpeg
去ffpmpeg 下载官网的源代码:
http://ffmpeg.org/
安装ffmpeg

./configure
make
make install

RTMP 通过FFmpeg+nginx发布成rtmp和http-flv

编译nginx需要加入(nginx-http-flv-module)模块(参考:https://blog.csdn.net/TH_NUM/article/details/103199376),执行configure的时候要加入–add-module=path/lib/nginx-http-flv-module选项。
nginx的具体编译过程不详介绍。

./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/home/dlr/wuyongyu/nginx/pcre-8.43 \
--add-module=/home/dlr/wuyongyu/nginx/nginx-http-flv-module \
--with-openssl=/home/dlr/wuyongyu/nginx/openssl-1.0.1t 

重新编译nginx是为了集成nginx-http-flv-module这个模块,在发布rtmp的时候可以转化成支持HTTP方式的FLV。

配置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;
}


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

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #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;
        }

        location /live {
            flv_live on;
            chunked_transfer_encoding  on; #open 'Transfer-Encoding: chunked' response
	    add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
	    add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
	    add_header Access-Control-Allow-Headers X-Requested-With;
	    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
	    add_header 'Cache-Control' 'no-cache';
         }
        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 push & pull status

            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /var/www/rtmp; #specify in where stat.xsl located
        }

        location /control {
            rtmp_control all; #configuration of control module of rtmp
        }
        # 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; #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 localhost; #for suffix wildcard matching of virtual host name

        application live {
            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;
        }
    }

}

配置好nginx.conf,在cmd下启动nginx服务

第二步:编写网页端显示的代码
使用video-js

1.进入到 nginx/html 文件夹,可以看到有两个.html文件,index.html就是Welcome to nginx!这个页面
先把它备份一份,再自己写个index.html,用来显示视频流。内容如下:

需要从:https://download.csdn.net/download/TH_NUM/11990337 下载
video-js.swf文件从压缩包中解压出来放到html 文件夹下。

PS: video-js.css和video.js文件也在这个压缩包中,也可以把它们都解压出来放到html 文件夹下

index.html

<!DOCTYPE html>
<html>
    <head>
      <title>RTMP Sample Player Videojs</title>
      <!-- Chang URLs to wherever Video.js files will be hosted -->
      <link href="video-js.css" rel="stylesheet" type="text/css">
      <!-- video.js must be in the <head> for older IEs to work. -->
      <script src="video.js"></script>
      <script>
        videojs.options.flash.swf = "video-js.swf";
      </script>
    </head>
    <body>
        <div align = "center">
            <h1>RTMP Player Videojs</h1>
            <video id="example_video_1" class="video-js vjs-default-skin" controls preload="none" width="640" height="480" data-setup="{}">
                <source src="rtmp://192.168.1.106:1935/live/mystream" type="rtmp/flv"/>

                <p class="vjs-no-js">To view this video please enable JavaScript, and consider upgrading to a web browser that <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a></p>
            </video>
        </div>


    </body>
</html>

ffmpeg 推流:

ffmpeg -re -i ~/wuyongyu/video/demo.mp4  -vcodec copy -acodec copy -f flv rtmp://localhost:1935/live/mystream

或则推流摄像头:
ffmpeg -f v4l2 -video_size 640x480 -framerate 30 -i /dev/video0 -vcodec libx264 -preset veryfast -f flv rtmp://localhost:1935/live/mystream

其他:
ffmpeg 拍照:

ffmpeg -f video4linux2 -i /dev/video0 -vframes 1 /usr/local/nginx/html/test2.jpeg -y

在浏览器地址栏输入localhost:80 或者http://localhost:80/live?port=1935&app=live&stream=mystream

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝鲸123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值