Java 视频解析相关工具类 (codeing真的是细节决定成败!)

项目中用到了视频解析的工具,简单的说一下,目前jave这个包,只能去下载下来后,手动上传到私服地址进行使用,pom.xml文件中写好配置是无法下载到jar包的。

        <dependency>
            <groupId>jack.javegroupId>
            <artifactId>javeartifactId>
            <version>1.0.2version>
        dependency>

楼主,只用了一个小功能,获取当前视频文件的时长,由于解析出来的是一个long 类型的值,并非我们日常中想用的那种xx:xx:xx 格式,就有的一下的代码:

/**
 * @ClassName VideoUtil
 * @Description TODO 视频解析相关工具类
 * @Author Lucky
 * @Version 1.0
 */

public class VideoUtil {

    public static String readVideoTime(File file2) {
        Encoder encoder = new Encoder();
        String length = "";
        String strHour = "";
        String strMinute = "";
        String strSecond = "";
        try {
            MultimediaInfo m = encoder.getInfo(file2);
            long ls = m.getDuration()/1000;
            int hour = (int) (ls/3600);
            System.out.println("hour"+hour);
            if(hour < 10){
                strHour = "0" + String.valueOf(hour);
            }else{
                strHour = String.valueOf(hour);
            }
            int minute = (int) (ls%3600)/60;
            System.out.println("minute"+minute);
            if(minute < 10){
                strMinute = "0" + String.valueOf(minute);
            }else{
                strMinute = String.valueOf(minute);
            }
            int second = (int) (ls-hour*3600-minute*60);
            System.out.println("second"+second);
            if(second < 10){
                strSecond = "0" + String.valueOf(second);
            }else{
                strSecond = String.valueOf(second);
            }
            System.out.println("strHour--->>>"+strHour);
            System.out.println("strMinute--->>>"+strMinute);
            System.out.println("strSecond--->>>"+strSecond);
            System.out.println(strHour+":"+strMinute+":"+strSecond);
            length = strHour+":"+strMinute+":"+strSecond;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return length;
    }

但是代码写的很low,想简单的优化一下,如下:

/**
 * @ClassName VideoUtil
 * @Description TODO 视频解析相关工具类
 * @Author Lucky
 * @Version 1.0
 */

public class VideoUtil {

    public static String readVideoTime(File file2) {
        Encoder encoder = new Encoder();
        String length = "";
        String strHour = "";
        String strMinute = "";
        String strSecond = "";
        try {
            MultimediaInfo m = encoder.getInfo(file2);
            long ls = m.getDuration()/1000;

            int hour = (int) (ls/3600);
            strHour = hour > 10 ? strHour : "0" + String.valueOf(hour);

            int minute = (int) (ls%3600)/60;
            strMinute = minute > 10 ? strMinute : "0" + String.valueOf(minute);

            int second = (int) (ls-hour*3600-minute*60);
            strSecond = second > 10 ? strSecond : "0" + String.valueOf(second)

            System.out.println("strHour--->>>"+strHour);
            System.out.println("strMinute--->>>"+strMinute);
            System.out.println("strSecond--->>>"+strSecond);
            System.out.println(strHour+":"+strMinute+":"+strSecond);
            length = strHour+":"+strMinute+":"+strSecond;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return length;
    }

}

最终在好友的帮助下,完成了最优化版的工具类 ,如下:

/**
 * @ClassName VideoUtil
 * @Description TODO 视频解析相关工具类
 * @Author Lucky
 * @Version 1.0
 */

public class VideoUtil {

    public static String readVideoTime(File file2) {
        Encoder encoder = new Encoder();
        String length = "";
        try {
            MultimediaInfo m = encoder.getInfo(file2);
            long ls = m.getDuration()/1000;
            LocalTime time = LocalTime.ofSecondOfDay(ls);
            length = time.toString();
            System.out.println("length--->>>"+length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return length;
    }

}

此过程充分的说明了,java很成熟,有很多我们日常中会用到的方法,其实他们都已经写好了。我们自己去实现,很容易出现很多我们自己想不到的问题,要习惯用jdk源生的方法,要站在巨人的肩膀上看世界。


我爱你
即使你满身污秽如刺猬
也有我双手拥抱不忌讳

你可能感兴趣的:(工具类)