2012-05-16 39 views
2

我想要的視頻字節它給我轉換結果,但我認爲結果是不正確的,因爲我測試了不同的視頻和給了我同樣的結果,從而 任何一個可以幫助請給如何將視頻轉換爲字節劃分的視頻字節

String filename = "D:/try.avi"; 
byte[] myByteArray = filename.getBytes(); 
for(int i = 0; i<myByteArray.length;i ++) 
{ 
    System.out.println(myByteArray[i]); 
} 

任何幫助請嗎?

+0

你到底想幹什麼?將視頻內容轉換爲字節或文件名爲字節? – Drona

+0

對視頻文件進行逐字節比較有什麼意義?我可以逐幀理解,但逐字節沒有多大意義。 –

+0

我想將視頻內容轉換爲字節 – lama

回答

4
String filename = "D:/try.avi"; 
byte[] myByteArray = filename.getBytes(); 

即文件轉換爲字節,而不是文件內容

至於閱讀文件的內容,請參閱Java教程的Basic I/O課程。

+0

然後你知道如何轉換文件的內容? – lama

+0

這次再次大聲一點。 **爲什麼?**您打算爲用戶提供哪些功能? –

+0

我只是想找到服務質量爲轉移的視頻在網狀網絡則繪製的結果,所以我想將視頻轉換成數據字節做統計 – lama

2

相同容器格式的視頻以相同的字節開頭。使用的編解碼器確定實際的視頻文件。

我建議你閱讀更多關於容器文件格式和編解碼器第一,如果你打算開發視頻應用。

但你有一個不同的問題。正如Andrew Thompson正確指出的那樣,您將獲得文件名字符串的字節。

正確的做法是:

private static File fl=new File("D:\video.avi"); 
byte[] myByteArray = getBytesFromFile(fl); 

也請記住,終端通常有固定的緩衝區大小(在Windows上,它是幾行),所以輸出數據的很大一部分將僅顯示過去幾年它的線。

編輯:下面是getBytesFromFile的實現;一位java專家可能會提供更多的標準方法。

public static byte[] getBytesFromFile(File file) throws IOException { 
     InputStream is = openFile(file.getPath()); 

     // Get the size of the file 
     long length = file.length(); 

     if (length > Integer.MAX_VALUE) { 
      // File is too large 
      Assert.assertExp(false); 
      logger.warn(file.getPath()+" is too big"); 
     } 

     // Create the byte array to hold the data 
     byte[] bytes = new byte[(int)length]; 

     // debug - init array 
     for (int i = 0; i < length; i++){ 
      bytes[i] = 0x0; 
     } 

     // Read in the bytes 
     int offset = 0; 
     int numRead = 0; 
     while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { 
      offset += numRead; 
     } 

     // Ensure all the bytes have been read in 
     if (offset < bytes.length) { 
      throw new IOException("Could not completely read file "+file.getName()); 
     } 

     // Close the input stream and return bytes 
     is.close(); 
     return bytes; 
    } 
+0

'System.IO.File.ReadAllBytes(filename);'呃..從來沒有聽說過IO包或那個方法。你能鏈接到JavaDocs嗎? –

+0

那是java嗎?對不起,我沒看到標籤:)))當我們有兩種如此相似的語言時,就是這樣。 –

+0

*「是那個java嗎?」*它和它開始的一樣。 'Java',而不是'java'。 –