获取视频的时长、大小及视频的读写

 

一些简单的东西,自己汇总了下,其他的都太杂了,如有侵权请告知。

文件视频的读写:

public static void main(String[] args) {
              BufferedInputStream reader;
              BufferedOutputStream writer;
              try{
                  reader=new BufferedInputStream(new FileInputStream("路径"));
                  writer=new BufferedOutputStream(new FileOutputStream("路径"));
                  int s;
                  while((s=reader.read())!=-1){
                      writer.write(s);
                      writer.flush();
                  }
                  writer.close();
                  reader.close();
              }catch(IOException e){
                  System.out.print("文件读写错误");
              }
        }

 

视频大小:

private static String ReadVideoSize(File source) {
            FileChannel fc= null;
            String size = "";
            try {
                @SuppressWarnings("resource")
                FileInputStream fis = new FileInputStream(source);
                fc= fis.getChannel();
                BigDecimal fileSize = new BigDecimal(fc.size());
                size = fileSize.divide(new BigDecimal(1048576), 2, RoundingMode.HALF_UP) + "MB";
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (null!=fc){
                        try{
                            fc.close();
                        }catch(IOException e){
                            e.printStackTrace();
                        }
                    }
                }
            return size;
        }

 

获取时长

private static String ReadVideoTime(File source) {
        Encoder encoder = new Encoder();
        String length = "";
        try {
                MultimediaInfo m = encoder.getInfo(source);
                long ls = m.getDuration()/1000;
                int hour = (int) (ls/3600);
                int minute = (int) (ls%3600)/60;
                int second = (int) (ls-hour*3600-minute*60);
                length = hour+"小时"+minute+"分钟"+second+"秒";
            } catch (Exception e) {
                e.printStackTrace();
            }
        return length;
    }

然后自己写个main方法就能运行了

 

public static void main(String[] args) {
        File a = new File("G:\\1.mp4");
        String size = ReadVideoSize(a);
        String time = ReadVideoTime(a);
        System.out.println(size);
        System.out.println(time);
    }

 

需要的jar

https://pan.baidu.com/s/1kUZ15yf  密码 ttpl

posted @ 2017-12-28 15:16  简单的深度  阅读(6121)  评论(0编辑  收藏  举报