获取视频信息的大小、时长等的工具类代码

    1. public class FlieLen {
    2. public static String ReadVideoTime(File source) {
    3. Encoder encoder = new Encoder();
    4. String length = "";
    5. try {
    6. MultimediaInfo m = encoder.getInfo(source);
    7. long ls = m.getDuration() / 1000;
    8. int hour = (int) (ls / 3600);
    9. int minute = (int) (ls % 3600) / 60;
    10. int second = (int) (ls - hour * 3600 - minute * 60);
    11. if (hour == 0 || hour < 10) {
    12. length += "0" + hour + ":";
    13. } else {
    14. length += hour + ":";
    15. }
    16. if (minute == 0 || minute < 10) {
    17. length += "0" + minute + ":";
    18. } else {
    19. length += minute + ":";
    20. }
    21. if (second == 0 || second < 10) {
    22. length += "0" + second;
    23. } else {
    24. length += second;
    25. }
    26. } catch (Exception e) {
    27. e.printStackTrace();
    28. }
    29. return length;
    30. }
    31. /**
    32. * 获取视频大小
    33. *
    34. * @param source
    35. * @return
    36. */
    37. public static String ReadVideoSize(File source) {
    38. FileChannel fc = null;
    39. String size = "";
    40. try {
    41. @SuppressWarnings("resource")
    42. FileInputStream fis = new FileInputStream(source);
    43. fc = fis.getChannel();
    44. BigDecimal fileSize = new BigDecimal(fc.size());
    45. size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";
    46. } catch (FileNotFoundException e) {
    47. e.printStackTrace();
    48. } catch (IOException e) {
    49. e.printStackTrace();
    50. } finally {
    51. if (null != fc) {
    52. try {
    53. fc.close();
    54. } catch (IOException e) {
    55. e.printStackTrace();
    56. }
    57. }
    58. }
    59. return size;
    60. }
    61. public static void main(String[] args) {
    62. File file = new File("E://123456.mp4");
    63. ReadVideoTime(file);
    64. ReadVideoSize(file);
    65. }

    所需依赖

    1. <dependency>
    2. <groupId>jack.jave</groupId>
    3. <artifactId>jave</artifactId>
    4. <version>1.0.2</version>
    5. <scope>system</scope>
    6. <systemPath>${basedir}/src/main/resources/lib/jave-1.0.2.jar</systemPath>
    7. </dependency>