当前位置: 首页 > article >正文

使用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();
		}
	 }


}




http://www.kler.cn/a/574261.html

相关文章:

  • [BD青训项目]介绍
  • vue3中子组件获取父组件的name,父组件不做修改动作
  • 算法探秘:盛最多水的容器问题
  • Oracle数据导入导出小工具(主要用于导入导出小批量含大字段的数据)
  • 快速启动 vue 开发环境
  • 特斯拉FSD(全自动驾驶)功能概述
  • 迷你世界脚本文字板接口:Graphics
  • centos7服务器 Java和Hadoop安装教程,用VMware和finalshell
  • 【C++教程】C++中的基本数据类型
  • 【每日学点HarmonyOS Next知识】Web上传文件、监听上下左右区域连续点击、折叠悬停、字符串相关、播放沙盒视频
  • VsCode/Cursor workbench.desktop.main.js 的入口
  • VS2019,VCPKG - 为VS2019添加VCPKG
  • 【启发式算法】Dijkstra算法详细介绍(Python)
  • 高频 SQL 50 题(基础版)| 高级字符串函数 / 正则表达式 / 子句:1667. 修复表中的名字
  • Jenkins工具配置与运用指南:从零到持续交付
  • 网络安全 三高三弱治理
  • 【进程和线程】(面试高频考点)
  • HTML第四节
  • Android Studio快速配置国内镜像源和HTTP代理
  • python基础课程整理--元组的基础