Javacv初体验-获取视频时长·转码·截取图片

官方:https://github.com/bytedeco/javacv

引入依赖:

<dependency>

<groupId>org.bytedeco</groupId>

<artifactId>javacv</artifactId>

<version>1.4.3</version>

</dependency>

<dependency>

<groupId>org.bytedeco.javacpp-presets</groupId>

<artifactId>ffmpeg-platform</artifactId>

<version>4.0.2-1.4.3</version>

</dependency>

 

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import org.bytedeco.javacpp.avcodec;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.Java2DFrameConverter;

public class VideoUtil {
	/**
	 * 获取时长
	 */
	public static long getDuration(File file) throws Exception {
		FFmpegFrameGrabber ff = new FFmpegFrameGrabber(file);
		ff.start();
		long duration = ff.getLengthInTime() / (1000 * 1000);
		ff.stop();
		return duration;
	}

	/**
	 * 转MP4
	 */
	public static String convertToMp4(File file) throws Exception {
		FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(file);
		Frame captured_frame = null;
		frameGrabber.start();
		String fileName = file.getAbsolutePath() + "ToMp4.mp4";
		FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(fileName, frameGrabber.getImageWidth(),
				frameGrabber.getImageHeight(), frameGrabber.getAudioChannels());
		recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264); // avcodec.AV_CODEC_ID_H264 //AV_CODEC_ID_MPEG4
		recorder.setFormat("mp4");
		recorder.setFrameRate(frameGrabber.getFrameRate());
		recorder.setSampleRate(frameGrabber.getSampleRate());
		recorder.setAudioChannels(frameGrabber.getAudioChannels());
		recorder.setFrameRate(frameGrabber.getFrameRate());
		recorder.start();
		while ((captured_frame = frameGrabber.grabFrame()) != null) {
			recorder.setTimestamp(frameGrabber.getTimestamp());
			recorder.record(captured_frame);
		}
		recorder.stop();
		recorder.release();
		frameGrabber.stop();
		return fileName;
	}

	/**
	 * @title 获取视频帧转存图片
	 * @param file 源视频文件
	 * @param imgDir 图片存放路径文件夹
	 * @param step 取帧间隔,每隔多少帧截取一张
	 * @throws Exception
	 */
	public static void getFramePics(File file, String imgDir, int step) throws Exception {

		FFmpegFrameGrabber ff = new FFmpegFrameGrabber(file); // 获取视频文件
		ff.start();
		int len = ff.getLengthInVideoFrames(); // 视频帧长度
		int index = 0; // 帧数
		int currIndex = 0;// 图片计数
		Frame frame = null;
		while (index <= len) {
			frame = ff.grabImage(); // 获取该帧图片流
			if (frame != null && frame.image != null) {
				getImage(frame, imgDir, currIndex, step); // 生成帧图片
				currIndex++;
			}
			index++;
		}
		ff.stop();
	}

	private static void getImage(Frame frame, String imgDir, int currIndex, int step) {
		if (currIndex % step != 0) {
			return;
		}
		File dir = new File(imgDir);
		if (!dir.isDirectory() || !dir.exists()) {
			dir.mkdirs();
		}
		File targetFile = new File(imgDir + currIndex + ".jpg");
		String imgSuffix = "jpg";

		Java2DFrameConverter converter = new Java2DFrameConverter();
		BufferedImage srcBi = converter.getBufferedImage(frame);
		int owidth = srcBi.getWidth();
		int oheight = srcBi.getHeight();
		// 对截取的帧进行等比例缩放
		int width = 800;
		int height = (int) (((double) width / owidth) * oheight);
		BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
		bi.getGraphics().drawImage(srcBi.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
		try {
			ImageIO.write(bi, imgSuffix, targetFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

学习:https://blog.csdn.net/jl19861101/article/details/94555851

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值