视频流下载 截图视频流下载 截图视频流下载 截图视频流下载 截图

该代码示例展示了如何利用JavaCV库中的FFmpegFrameGrabber从视频流地址抓取帧,并进行视频录制和截图操作。程序设定了一定的录制时长,并将视频保存为MP4格式,同时可选择是否录制音频。在指定时间间隔内,程序还会保存视频帧为JPEG图片。
摘要由CSDN通过智能技术生成

视频流下载 截图视频流下载 截图视频流下载 截图视频流下载 截图

package 
import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacv.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @ClassName testUrl
 * @Author dell-pc
 * @create 2023/3/9 16:28
 */
public class videoUtil {



    private static String streamUrl ;  //视频地址
    private static long timesSec = 2L;//停止录制时长 0为不限制时长
    static Boolean screenshot = true;

    private static String outFilePath;//视频文件的输出路径
    private static String imagePath;//图片存放地址
    public static  String filename;//文件名
    private static String filenameExtension = "mp4";//录制的视频文件格式(文件后缀名)
    private static boolean hasAudio = false; //是否录制音频
    public static boolean boo = true; //返回值



    public static void main(String[] args) throws InterruptedException {
//        testUrl testUrl = new testUrl();
//        testUrl.outFilePath = "C:\\Users\\dell-pc\\Desktop\\aaaaaaaaaaaa\\test10.mp4";
//        testUrl.imagePath =  "C:\\Users\\dell-pc\\Desktop\\aaaaaaaaaaaa\\";
//        //最好设置结束时长 如直接停止程序会造成输出文件的损坏无法正常播放
//        testUrl.timesSec = 1L;
//        testUrl.hasAudio = true;
//        new Thread(testUrl).start();
        String a= "C:\\Users\\dell-pc\\Desktop\\aaaaaaaaaaaa\\";
        Boolean qwe123 = getVideo(a, "流地址", "qwe123");
        System.out.println(qwe123);
    }


    public static Boolean getVideo(String die,String urlAddress,String aid) throws InterruptedException {
        System.out.println("流地址"+urlAddress);
         int flag = 0;
         int i=0;
        try{
            videoUtil testUrl = new videoUtil();
            testUrl.outFilePath = die+aid+"0.mp4";
            testUrl.imagePath =  die;
            testUrl.filename =  aid;
            testUrl.streamUrl =  urlAddress;
            testUrl.timesSec = 12L;  //最好设置结束时长 如直接停止程序会造成输出文件的损坏无法正常播放
            testUrl.hasAudio = true;
            if(outFilePath == null || outFilePath.length() == 0){
                System.out.println("文件输出路径不能为空。");
                return false;
            }
            //根据直播链接实例FFmpeg抓帧器
            FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(streamUrl);
            // 设置采集器构造超时时间(单位微秒,1秒=1000000微秒)
            // 设置读取的最大数据,单位字节
            grabber.setOption("probesize", "10000");
            // 设置分析的最长时间,单位微秒
            grabber.setOption("analyzeduration", "10000");
            grabber.setOption("rtsp_transport", "tcp");
            FFmpegFrameRecorder recorder = null;
            try {
                grabber.start();//这个容易被阻塞,一直没返回值

                System.out.println("开始录制了");
                Frame frame = grabber.grabFrame();
                if (frame != null) {
                    //保存到本地的文件
                    File outFile = new File(outFilePath);
                    //文件不存在 || 文件不是一个普通文件
                    if(!outFile.exists() || !outFile.isFile()){
                        if(!outFile.createNewFile()){
                            System.out.println("文件创建失败");
                            return false;
                        }
                    }
                    // 视频输出地址,视频宽分辨率(宽,高),是否录制音频(0:不录制/1:录制)
                    recorder = new FFmpegFrameRecorder(outFilePath, frame.imageWidth, frame.imageHeight, hasAudio ? 1 : 0);
                    //直播流格式
                    recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
                    //录制的视频格式
                    recorder.setFormat(filenameExtension);
                    //视频帧数
                    recorder.setFrameRate(30);
                    //开始录制
                    recorder.start();
                    // 计算结束时间
                    long endTime = System.currentTimeMillis() + timesSec * 10000;
                    // 如果没有到录制结束时间并且获取到了下一帧则继续录制
                    while ((System.currentTimeMillis() < endTime) && (frame != null)) {
                        //录制
                        recorder.record(frame);
                        //获取下一帧
                        frame = grabber.grabFrame();


                        //视频快照
                        if(screenshot) {

                            if (i<5) {

                                if(flag%100==0){

                                    //文件储存对象
                                    String fileName = imagePath+ videoUtil.filename +i+".jpg";
                                    File outPut = new File(fileName);
                                    ImageIO.write(FrameToBufferedImage(frame), "jpg", outPut);
                                    if (flag == 5){
                                        //结束视频快照
                                        screenshot = false;
                                    }
                                    i++;
                                }

                            }
                        }
                        flag++;
                    }
                    recorder.record(frame);
                }
                System.out.println("录制完成。");
            } catch (IOException e) {
                System.out.println("录制出错。");
                e.printStackTrace();
                return false;
            } finally {
                //停止录制
                try {
                    grabber.stop();
                    System.out.println("视频停止1");
                } catch (FrameGrabber.Exception e) {
                    e.printStackTrace();
                    return false;
                }
                if (recorder != null) {
                    try {
                        recorder.stop();
                        System.out.println("视频停止2");
                    } catch (FrameRecorder.Exception e) {
                        e.printStackTrace();
                        return false;
                    }
                }
            }
        }catch (Exception e){
            return false;
        }

        return boo;
    }



    public static BufferedImage FrameToBufferedImage(Frame frame) {
        //创建BufferedImage对象
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bufferedImage = converter.getBufferedImage(frame);
        return bufferedImage;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值