adsadasdddddddddddddddddddddd

package com.totp.service;

import lombok.extern.slf4j.Slf4j;
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.*;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.UUID;

@Slf4j
@Service
public class PushStreamService {

    private static String RTSP_URL  = "rtsp://admin:admin@120.79.51.23:8554/live/";

    private static final int FRAME_RATE = 15;



    @PostConstruct
    public void pushStream() throws Exception {
        System.out.println("启动中...");
        String uuid = UUID.randomUUID().toString().replace("-","");
        // 抓取参数a
        String grabberFormat = "gdigrab";
        String input = "desktop"; // 捕获整个桌面
        int frameRate = 30;
        int gop = frameRate * 2;

        // 推流参数
        String rtspServer = RTSP_URL + uuid;
        int width = 1920;
        int height = 1080;
        int bitrate = 2000000; // 2Mbps

        FFmpegFrameGrabber grabber = null;
        FFmpegFrameRecorder recorder = null;

        try {
            // 初始化屏幕抓取器
            grabber = new FFmpegFrameGrabber(input);
            grabber.setFormat(grabberFormat);
            grabber.setFrameRate(frameRate);
            grabber.setImageWidth(width);
            grabber.setImageHeight(height);
            grabber.start();

            // 初始化RTSP录制器
            recorder = new FFmpegFrameRecorder(rtspServer, width, height);
            recorder.setFormat("rtsp");
            recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
            recorder.setFrameRate(frameRate);
            recorder.setGopSize(gop);
            recorder.setVideoBitrate(bitrate);

            // 关键参数:降低延迟
            recorder.setVideoOption("tune", "zerolatency");
            recorder.setVideoOption("preset", "ultrafast");
            recorder.setVideoOption("crf", "23");
            recorder.setOption("timeout", "5000000");

            try {
                recorder.start();
            }catch (Exception e){
                log.info("连接超时...");
                return;
            }


            Long count=System.currentTimeMillis()+3000;
            Frame capturedFrame;
            while ((capturedFrame = grabber.grab()) != null) {
                recorder.record(capturedFrame);
                if (count-System.currentTimeMillis()<=0){
                    log.info("推流中,拉流地址:{}", RTSP_URL + uuid);
                    count=System.currentTimeMillis()+3000;
                }

            }
        } catch (FrameGrabber.Exception | FrameRecorder.Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (recorder != null) {
                    recorder.close();
                }
                if (grabber != null) {
                    grabber.close();
                }
            } catch (FrameRecorder.Exception | FrameGrabber.Exception e) {
                e.printStackTrace();
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值