环境

流媒体服务器 ubuntu2004+nginx+nginx-http-flv-module

推流 win10+javacv

拉流 win10 + flv.js

nginx 编译

安装依赖

libpcre3

sudo apt install libpcre3-dev

zlib

下载源码
http://www.zlib.net/zlib-1.2.11.tar.gz

tar zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make 
make install

nginx-http-flv-module

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

nginx

下载源码
http://nginx.org/download/nginx-1.19.6.tar.gz

tar zxvf nginx-1.19.6.tar.gz
cd nginx-1.19.6

./configure --prefix=/home/wangcj/nginx --add-module=/home/wagncj/nginx-http-flv-module
make 

make install

nginx 流媒体服务器配置

worker_processes  1; #should be 1 for Windows, for it doesn't support Unix domain socket
error_log logs/error.log error;
events {
    worker_connections  4096;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  65;
    server {
        listen       80;

        location / {
            root   html;
            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
        }

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

javacv 推流

引入依赖

        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv</artifactId>
            <version>1.5.4</version>
        </dependency>

        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv-platform</artifactId>
            <version>1.5.4</version>
        </dependency>

推流代码

        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber("desktop");
        grabber.setImageWidth(1920);
        grabber.setImageHeight(1080);
        grabber.setFormat("gdigrab");
        grabber.setFrameRate(15);


        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("rtmp://192.168.226.128:1935/myapp/123",grabber.getImageWidth(),grabber.getImageHeight());
        recorder.setFormat("flv");
        recorder.setFrameRate(grabber.getFrameRate());
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);


        try {
            recorder.start();
            grabber.start();

            while (!Thread.interrupted()) {
                long start = System.currentTimeMillis();

                Frame frame = grabber.grabImage();
                recorder.record(frame);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

启动推流

upload successful

upload successful

flv.js 拉流

前端代码

upload successful

<script src="flv.min.js"></script>
<video id="videoElement" muted></video>
<script>
    if (flvjs.isSupported()) {
        var videoElement = document.getElementById('videoElement');
        var flvPlayer = flvjs.createPlayer({
            type: 'flv',
            url: 'http://192.168.226.128/live?app=myapp&stream=123'
        });
        flvPlayer.attachMediaElement(videoElement);
        flvPlayer.load();
        flvPlayer.play();
    }
</script>

直播效果

upload successful

后续

视频感觉有几秒延时。之前按照videojs一直没成功,并且现在flash逐渐退出,果断放弃videojs



JAVA      javacv flv.js 直播 屏幕录制

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!