Java获取视频文件各种信息

通过 JAVE 

JAVE:http://www.sauronsoftware.it/projects/jave/index.php

  • https://github.com/dadiyang/jave
  • https://blog.csdn.net/dadiyang/article/details/85003240

 官方介绍:

The JAVE (Java Audio Video Encoder) library is Java wrapper on the ffmpeg project. Developers can take take advantage of JAVE to transcode audio and video files from a format to another. In example you can transcode an AVI file to a MPEG one, you can change a DivX video stream into a (youtube like) Flash FLV one, you can convert a WAV audio file to a MP3 or a Ogg Vorbis one, you can separate and transcode audio and video tracks, you can resize videos, changing their sizes and proportions and so on. Many other formats, containers and operations are supported by JAVE.

只需要一个jar包— jave-1.0.2.jar 

下载:http://www.sauronsoftware.it/projects/jave/download.php 

gradle 项目:

// https://mvnrepository.com/artifact/com.github.dadiyang/jave
compile group: 'com.github.dadiyang', name: 'jave', version: '1.0.5'

 

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.MultimediaInfo;
import it.sauronsoftware.jave.EncoderException;
import org.junit.Test;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import java.io.File;
public class Main {
    /**
     * *.mp4,*.flv,*..3gp格式均可
     */
    @Test
    public void test() throws EncoderException {
        String str = "D:/BaiduNetdiskDownload/尚硅谷/大数据/尚硅谷大数据之Hadoop视频/4.视频/176_尚硅谷_MapReduce_开发总结.avi";

        File source = new File(str);
        Encoder encoder = new Encoder();
        MultimediaInfo m = encoder.getInfo(source);
        long millisecond = m.getDuration();//毫秒数
        System.out.println(millisecond);

        int hour = (int) millisecond / (60 * 60 * 1000);//小时
        int minute = (int) (millisecond % (60 * 60 * 1000)) / 60000;//分钟
        int second = (int) ((millisecond % (60 * 60 * 1000)) % 60000) / 1000;//秒
        System.out.println(hour + ":" + minute + ":" + second);
    }
}

 统计一个文件夹里面所有指定格式的视频文件

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.MultimediaInfo;
import org.junit.Test;
import java.io.File;
import java.io.IOException;

/**
 * 统计一个文件夹中指定格式视频文件的长度
 */
public class getMVLength {


/**
 * 视频个数
 */
private static int count = 0;
/**
 * 视频长度
 */
private static Long totalLen = 0L;

@Test
public void test() throws IOException, EncoderException {
    File file = new File("D:/BaiduNetdiskDownload/尚硅谷/尚硅谷SpringBoot核心技术篇");
    getAllCatalog(file);
    System.out.println("满足条件的文件个数:" + count);

    int hour = (int) (totalLen / (60 * 60 * 1000));//小时
    int minute = (int) (totalLen % (60 * 60 * 1000)) / 60000;//分钟
    int second = (int) ((totalLen % (60 * 60 * 1000)) % 60000) / 1000;//秒
    System.out.println("总时间:" + hour + ":" + minute + ":" + second);
}

/**
 * 递归遍历文件
 */
private static void getAllCatalog(File file) throws IOException, EncoderException {
    File[] arrFiles = file.listFiles();
    for (int i = 0; i < arrFiles.length; i++) {
        if (arrFiles[i].isDirectory()) { //是文件就递归
            getAllCatalog(arrFiles[i]);
        } else {
            File tempFile = arrFiles[i];
            if (tempFile.getName().endsWith(".avi")) {
                showMessage(tempFile);
                count++;
            }
        }
    }
}

private static void showMessage(File file) throws EncoderException {
    Encoder encoder = new Encoder();
    MultimediaInfo m = encoder.getInfo(file);
    long millisecond = m.getDuration();//毫秒数
    totalLen += millisecond;

    long fileSize = file.length();//字节
    Double MB = fileSize/(1024*1024*1.0);



    int hour = (int) millisecond / (60 * 60 * 1000);//小时
    int minute = (int) (millisecond % (60 * 60 * 1000)) / 60000;//分钟
    int second = (int) ((millisecond % (60 * 60 * 1000)) % 60000) / 1000;//秒
    System.out.print(file.getName() + " " + hour + ":" + minute + ":" + second+" ");
    System.out.printf("%.2f MB(%d字节)\n",MB,fileSize);
}


}

https://blog.csdn.net/lang_1991/article/details/80929926

你可能感兴趣的:(Java)