Search code examples
c#audiostreambit-manipulationflv

FLV body length


I am making my own FLV audio downloader without using external libraries. I am following this documentation:

http://osflash.org/flv

In FLV tag type, there are three interesting values:

BodyLength, Timestamp, StreamId which are of uint24_be type. How to read them? I found the answer here:

Extract Audio from FLV stream in C#

However I don't understand few things:

If Timestamp and StreamId are both uint24_be(also what is uint24_be?) then why

reader.ReadInt32(); //skip timestamps 
ReadNext3Bytes(reader); // skip streamID

Also what exactly ReadNext3Bytes do? Why not to read 3 next bytes like this:

reader.ReadInt32()+reader.ReadInt32()+reader.ReadInt32();

Solution

  • You can't use the reader.ReadInt32()+reader.ReadInt32()+reader.ReadInt32() because, at first it is 12 bytes instead of 3 bytes, and at second it is not enough simple summarize these bytes - you should make an 24-bit value. Here is more readable version of ReadNext3Bytes function:

    int ReadNext3Bytes(System.IO.BinaryReader reader) {
        try {
            byte b0 = reader.ReadByte();
            byte b1 = reader.ReadByte();
            byte b2 = reader.ReadByte();
            return MakeInt(b0, b1, b2);
        }
        catch { return 0; }
    }
    int MakeInt(byte b0, byte b1, byte b2) {
        return ((b0 << 0x10) | (b1 << 0x08)) | b2;
    }