javacv-ffmpeg(八)视频文件信息获取

说明

获取流信息应该没问题,暂时没尝试。有兴趣的同学可以试试
(后续更新的内容其实是补充,好长时间没写了,现在有时间就填填坑)

实体类代码

获取编码方式不太好用

import lombok.Data;

@Data
public class VideoInfo {
    /**
     * 总帧数
     * **/
    private int lengthInFrames;

    /**
     * 帧率
     * **/
    private double frameRate;

    /**
     * 时长
     * **/
    private double duration;
    
	/**
	 * 视频编码
	 */
	private String videoCode;
	/**
	 * 音频编码
	 */
	private String audioCode;

    private int width;
    private int height;
    private int audioChannel;
    private String md5;
	/**
	 * 音频采样率
	 */
	private Integer sampleRate;
}

工具类代码

/**
 * 获取视频信息
 * **/
@Log4j2
public class GetVideoInfo {
    public static VideoInfo getVideoInfoByFile(File file){
        VideoInfo info = new VideoInfo();
        FFmpegFrameGrabber grabber = null;
        try {
            grabber = new FFmpegFrameGrabber(file);
            grabber.start();

            int lengthInFrames = grabber.getLengthInVideoFrames();
            double frameRate = grabber.getVideoFrameRate();
            double duration = grabber.getLengthInTime() / 1000000.00;
            int width = grabber.getImageWidth();
            int height = grabber.getImageHeight();
            int audioChannel = grabber.getAudioChannels();
            String videoCode = grabber.getVideoCodecName();
            String audioCode = grabber.getAudioCodecName();
            String md5 = MD5Util.getMD5ByInputStream(new FileInputStream(file));
            int sampleRate = grabber.getSampleRate();

            info.setLengthInFrames(lengthInFrames);
            info.setFrameRate(frameRate);
            info.setDuration(duration);
            info.setWidth(width);
            info.setHeight(height);
            info.setAudioChannel(audioChannel);
            info.setMd5(md5);
            info.setVideoCode(videoCode);
            info.setAudioCode(audioCode);
            info.setSampleRate(sampleRate);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (grabber != null) {
                    grabber.stop();
                    grabber.release();
                }
            } catch (FFmpegFrameGrabber.Exception e) {
                log.error("getVideoInfo grabber.release failed");
            }
        }
        return info;
    }
}

你可能感兴趣的:(javacv,音视频,ffmpeg,java)