2017-09-02 46 views
0

我想要java代碼來創建特定大小的視頻分區。 例如考慮一個尺寸爲20mb的視頻,我想要每個5mb的peices。所以我們得到4個部分。 我已經使用下面的代碼,但它只創建.MP4文件,它不創建視頻文件。如何使用android將視頻分解成片段?

public static void divideFile(File f) { 
    int partCounter = 1;//I like to name parts from 001, 002, 003, 
    //you can change it to 0 if you want 000, 001,003 

    int sizeOfFiles = 1024 * 1024;// 1MB 
    byte[] buffer = new byte[sizeOfFiles]; 

    String fileName = f.getName(); 

    //try-with-resources to ensure closing stream 
    try (FileInputStream fis = new FileInputStream(f); 
     BufferedInputStream bis = new BufferedInputStream(fis)) { 

     int bytesAmount = 0; 
     while ((bytesAmount = bis.read(buffer)) > 0) { 
      //write each chunk of data into separate file with different number in name 
      String filePartName = String.format("%s.%03d", fileName, partCounter++); 
      File newFile = new File(f.getParent(), filePartName); 
      try (FileOutputStream out = new FileOutputStream(newFile)) { 
       out.write(buffer, 0, bytesAmount); 
      } 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    } 

回答

0

我想你想的每個部分是一個可播放的視頻本身 - 在這種情況下,每個部分都需要正確的元數據標題,可讓玩家妥善處理。

只是按字節分割視頻將意味着元數據在任何塊中都不存在或不正確。

您能正確使用的ffmpeg做這個用以下命令(MP4):

的ffmpeg -I將videoPath -ss開始時間-t結束時間-c複製outputVideoChunk.mp4

有有幾種方法可以使用Android應用程序中的ffmpeg,但最簡單的一種是使用一個很好的支持包裝庫,如: