獲取視頻的時長、大小及視頻的讀寫


 

一些簡單的東西,自己匯總了下,其他的都太雜了,如有侵權請告知。

文件視頻的讀寫:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM