使用jcodec库,访问网络视频提取封面图片上传至oss

  注释部分为FFmpeg(确实方便但依赖太大,不想用)

 
package com.zuodou.upload;


import com.aliyun.oss.OSS;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.zuodou.oss.OssProperties;
//import org.bytedeco.javacv.FFmpegFrameGrabber;
//import org.bytedeco.javacv.Frame;
//import org.bytedeco.javacv.Java2DFrameConverter;
import org.jcodec.api.FrameGrab;
import org.jcodec.common.io.FileChannelWrapper;
import org.jcodec.common.io.NIOUtils;
import org.jcodec.common.io.SeekableByteChannel;
import org.jcodec.common.model.Picture;
import org.jcodec.scale.AWTUtil;


import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.FileChannel;


/**
* 截取视频图片
* @ClassName:  FrameGrabberKit   
*/
public class FrameVideoUtlis {


    /**
     * 获取指定视频的帧并保存为图片至指定目录
     * @param videofile  源视频文件路径
     * @param framefile  截取帧的图片存放路径 例:F:\hfkjrecorder\target\4.jpg
     * @throws Exception
     */
	public  void getVedioImg(String videofile, String framefile,OSS ossClient){
	    //截取封面图
		try {
            URL url = new URL(videofile);
            URLConnection connection = url.openConnection();
            InputStream inputStream = connection.getInputStream();

            // 创建临时文件
            File tempFile = File.createTempFile("video", ".mp4");

            // 将 InputStream 内容写入临时文件
            try (RandomAccessFile tempFileStream = new RandomAccessFile(tempFile, "rw")) {
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    tempFileStream.write(buffer, 0, bytesRead);
                }
            }

            // 将临时文件转换为 SeekableByteChannel
            SeekableByteChannel channel = NIOUtils.readableChannel(tempFile);

            // 从视频流中抓取第一帧
            Picture grab = FrameGrab.getFrameFromChannel(channel,1);
            // 将视频帧转换为BufferedImage
            BufferedImage image = AWTUtil.toBufferedImage(grab);
            // 生成的预览图
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", outputStream);


//            FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videofile);
//            grabber.start();
//            // 提取封面图像
//            Java2DFrameConverter converter = new Java2DFrameConverter();
//            Frame frame = grabber.grabImage();
//            BufferedImage image = converter.convert(frame);
//            // 不再需要保存为本地文件,直接上传到阿里云 OSS
//            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//            ImageIO.write(image, "jpg", outputStream);

            // 保存封面图像
            byte[] imageBytes = outputStream.toByteArray();
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(imageBytes.length);
            PutObjectRequest putObjectRequest = new PutObjectRequest(OssProperties.BUCKET_NAME, framefile.substring(1), new ByteArrayInputStream(imageBytes), metadata);

            // 创建PutObject请求。
            ossClient.putObject(putObjectRequest);
//            grabber.stop(); // 确保在代码块结束时释放资源
            tempFile.delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值