【Springboot】录屏功能实现

3 篇文章 0 订阅

参考链接:

录屏功能实现主要参考

https://gitee.com/devwangrui/java-screen-capture
这里主要贴上修改后的几个代码,其他的就是用上面这位老哥的代码

1.RecorderController.java

package com.ht.recode.Controller;

import com.ht.recode.Model.AO.RecordeAO;
import com.ht.recode.fxbase.RecorderUtil;
import com.ht.recode.fxbase.VideoRecorder;
import com.ht.recode.utils.CutPicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author
 * @description: TODO
 * @date 2021/1/14  9:19
 */
@Slf4j
@RequestMapping("/recorder/")
@RestController
public class RecorderController {

    @Value("${video.recorder.recorderTime}")
    private Long recorderTime;

    @Value("${video.recorder.img-path}")
    private String imgPath;

    @Value("${video.recorder.file-name}")
    private String videoPath;

    @Autowired
    private RecorderUtil recorderUtil;

//    private RecordeAO recordeAO


    /* @Descreption:功能描述:
     * 〈〉
     * @Param RecordeAO:对象参数里两个属性->1,CameraName 生成的mp4文件文件名称,前面加了一段时间戳 。
     * 2,recordeTime:录制时长,如果不填 默认十秒
     * @return: 0代表失败 1代表成功
     * @Author: 
     * @Date: 2021/1/14 10:33
     */
//    @PostMapping("recorder")
    @RequestMapping(value = "/recorder")
//    public int recorder(@RequestBody RecordeAO recordeAO){
    public int recorder(RecordeAO recordeAO){
        int code = 0;
        long l = System.currentTimeMillis();
        //视屏名称 时间戳+名字
        String name = l+"-"+recordeAO.getCameraName();
        try {
            VideoRecorder videoRecorder = recorderUtil.getVideoRecorderInstance(name);
//            log.info("开始录屏---------------"+new Date());
            System.out.println("开始录屏---------------"+new Date());
            //开始============================================================================================
            videoRecorder.start();

            Long recordeTime = recordeAO.getRecordeTime();
            //定义需要录制的时间长度
            Long reTime = this.recorderTime;
            if (recordeTime!=null){
                //如果传过来的时间不是空,并且大于1秒,就用传过来的时间,不然就用默认录制时长来做时间
                reTime = recordeTime;
                if (recordeTime<1000){
                    //如果传过来的时间不是空,并且小于1秒,就只录1秒
                    reTime = 1000L;
                }
            }

//            log.info("这个录屏要录:"+reTime/1000+"秒");
            System.out.println("这个录屏要录:"+reTime/1000+"秒");
            //线程休眠多少秒   录屏就录多少秒
            Thread.sleep(reTime);

            //截图    需要图片的用这个方法取图
           CutPicUtil.cutPic(name,imgPath,"png",recordUtil.getRectangle());
//            log.info("截图保存在这个文件夹下====》:"+imgPath);

            //结束============================================================================================
            videoRecorder.stop();
//            log.info("录屏结束------------------------------"+new Date());
//            log.info("录屏保存在这个文件夹下:"+videoPath);
            System.out.println("录屏结束------------------------------"+new Date());
            System.out.println("录屏保存在这个文件夹下:"+videoPath);

        }catch (Exception e){
            code=1;
        }
        return code;
    }
}

2.RecordeAO

package com.ht.recode.Model.AO;

import lombok.Data;

/**
 * @author 
 * @description: TODO
 * @date 2021/1/14  10:12
 */
@Data
public class RecordeAO {

    private String cameraName;

    private Long recordeTime;

    public String getCameraName() {
        return cameraName;
    }

    public void setCameraName(String cameraName) {
        this.cameraName = cameraName;
    }

    public Long getRecordeTime() {
        return recordeTime;
    }

    public void setRecordeTime(Long recordeTime) {
        this.recordeTime = recordeTime;
    }

    @Override
    public String toString() {
        return "RecordeAO{" +
                "cameraName='" + cameraName + '\'' +
                ", recordeTime=" + recordeTime +
                '}';
    }
}

3.RecorderUtil

package com.threed.util;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.io.File;

@Component
@Data
@ConfigurationProperties(prefix = "video.recorder")
public class RecordUtil {
    /* 这4个参数用来定位要录制的屏幕区域,由一个点和一个矩形来定位
     * startx起点x坐标, starty起点y坐标,width录屏的宽度,height录屏的高度;
     * fileName是视屏录制之后存储的位置,这些写在配置文件中
     * */
    private int startx;
    private int starty;
    private int width;
    private int height;
    private String fileName;

    private Rectangle rectangle;

    public VideoRecorder getVideoRecorderInstance(String videoName){
        //这4个参数用来定位要录制的屏幕区域  由一个点和一个Rectangle矩形来定位
        rectangle = new Rectangle(startx, starty, width, height);
        File file = new File(fileName);
        if (!file.isDirectory()){
            //如果保存路径不存在,就创建一个
            file.mkdir();
        }
        String fileHoldName = fileName + videoName;
        //调用录屏API的对象  这个对象就可以开启或者关闭录屏
        VideoRecorder videoRecorder = new VideoRecorder(fileHoldName, true, rectangle);
        return videoRecorder;
    }

    public Rectangle getRectangle(){
        //截图时获取rectangle对象
        return new Rectangle(startx, starty, width, height);
    }

}

这个链接没仔细看:H5+springboot(集成ffmpeg)实现前端视频录制以及webm格式转mp4

遇到的问题:

1.org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
参考:http://t.zoukankan.com/hahayixiao-p-13251796.html
解决:将@POSTMapping改为@RequestMapping

2.@RequestBody对象为空,异常Required request body is missing
参考博客:https://www.cnblogs.com/dennyzhangdd/p/7780221.html
3.虽然可以录屏,但是会有异常,而且录制完播放较快,后面再研究下

[aac @ 0x7efe442179c0] Qavg: 120.000
org.bytedeco.javacv.FrameRecorder$Exception: No video output stream (Is imageWidth > 0 && imageHeight > 0 and has start() been called?)
	at org.bytedeco.javacv.FFmpegFrameRecorder.recordImage(FFmpegFrameRecorder.java:887)
	at org.bytedeco.javacv.FFmpegFrameRecorder.record(FFmpegFrameRecorder.java:876)
	at org.bytedeco.javacv.FFmpegFrameRecorder.record(FFmpegFrameRecorder.java:869)
	at com.threed.util.VideoRecorder$2.run(VideoRecorder.java:123)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值