java實現視頻分段並且合并

來源:互聯網
上載者:User
import java.io.*;public class Study {private static int length = 1024;// 可以設定檔案在讀取時一次讀取檔案的大小 /* * 檔案的切割 String path 切割檔案的路徑 size      子檔案的大小 */public static void filesplit(String path, int size) throws Exception {if (path == null)throw new Exception("源檔案不可為空...");File file = new File(path);if (!file.exists())throw new Exception("源檔案不存在...");long num = file.length() % size == 0 ? file.length() / size : file.length() / size + 1;String list[] = new String[(int) num];// 用於存放分割後的結果FileInputStream reader = new FileInputStream(file);long beginIndex = 0, endIndex = 0;int readcount = 0;byte buffer[] = new byte[length];for (int i = 0; i < num; i++) {list[i] = "d:\\test\\bao" + ".depart" + i + ".flv";FileOutputStream writer = new FileOutputStream(list[i]);endIndex = (endIndex + size) > file.length() ? file.length(): endIndex + size;for (; beginIndex < endIndex;) {if (endIndex - beginIndex >= length) {readcount = reader.read(buffer);beginIndex += readcount;writer.write(buffer);} else {// 下面的就不能直接讀取1024個位元組了,就要一個一個位元組的讀取了for (; beginIndex < endIndex; beginIndex++) {writer.write(reader.read());}continue;}}writer.close();}}public static void union(String path, String newString) throws Exception {File file = new File(path);File list[] = file.listFiles();File newFile = new File(newString);byte buffer[] = new byte[1024];int readcount;if (!newFile.getParentFile().exists())throw new Exception("你合并的檔案夾的不存在...");FileOutputStream writer = new FileOutputStream(newString);for (File f : list) {FileInputStream reader = new FileInputStream(f);while ((readcount = reader.read(buffer)) != -1) {writer.write(buffer);}reader.close();}writer.close();}public static void main(String args[]) throws Exception {filesplit("d:\\bao.flv", 1024*1024);union("d:\\test\\", "d:\\test\\movie.flv");}}/* * 檔案的分割與合并 1.檔案的分割,原理是用輸入資料流去讀取檔案,將讀取規定大小的流再輸出支指定的檔案,直到整個把整個檔案讀取結束. * 2.檔案合并,檔案的合并原理與分割正好想反,就是把所有的檔案都讀取到一個輸入資料流中,然後再把輸入資料流中的東西全部輸出到   * 同一個檔案輸出資料流中,這樣就可以把分割的檔案合并到一個檔案中去了. 並且檔案的大小和原來也會一樣 .  3.上面的程式我試著分割一個600多M * 的電影,分割是成功了,但只是分割的第一個檔案可以播放,而後面的幾個檔案都不可以播放,我也不知道為什麼, * 可能是視頻檔案裡面有什麼自定的格式吧...不過分割後再把所有的檔案合并,合并後檔案大小和之前一樣,而且還可以插入,說明檔案沒有分割壞 * 至於單個的檔案為什麼不能插入,這個以後用到的時候再去研究..現在還是把Java的基礎搞懂再說.....  2011.10.21 9:43 * * */

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.