2

ビデオをバイトに変換したいのですが、結果が得られますが、異なるビデオでテストして同じ結果が得られるため、結果は正しくないと思いますので、ビデオをバイトに変換する方法を教えてください。

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

助けてください?

4

3 に答える 3

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

これは、ファイルの内容ではなく、ファイル名をバイトに変換することです

ファイルの内容の読み方については、Java チュートリアルのBasic I/Oのレッスンを参照してください。

于 2012-05-16T08:12:31.463 に答える
2

同じコンテナ形式のビデオは、同じバイトで始まります。使用されるコーデックによって、実際のビデオ ファイルが決まります。

ビデオ アプリケーションの開発を計画している場合は、最初にコンテナ ファイル形式とコーデックについて詳しく読むことをお勧めします。

しかし、別の問題があります。Andrew Thompson が正しく指摘したように、ファイル名 stringのバイトを取得しています。

正しいアプローチは次のようになります。

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;
    }
于 2012-05-16T08:16:31.953 に答える