开源中文网

您的位置: 首页 > 编程开发 > C语言 > 正文

FLV音频帧信息

来源:  作者:

#include <iostream>
#include <fstream>
using namespace std;

typedef struct _FLVHead
{
    unsigned char flv[3];
    unsigned char version;
    unsigned char FlagsVideo:1;
    unsigned char Flags:1;
    unsigned char FlagsAudio:1;
    unsigned char Flags1:5;
    unsigned int DataOffset;
}FLVHead;

typedef struct _AudioInfo
{
    unsigned char SoundType:1;
    unsigned char SoundSize:1;    
    unsigned char SoundRate:2;
    unsigned char SoundFormat:4;
}AudioInfo;

typedef struct _FLVTag
{
    unsigned int SizeOfLastTag;
    unsigned int Type:;
    unsigned int DataSize:24;
    unsigned int TimeStamp:24;
    unsigned int ExtenTimeStamp:8;
    unsigned int StreamID:24;
    unsigned int AVData:8;
}FLVTag;

const char* SoundFormat[] ={
"Linear PCM, platform endian",
"ADPCM",
"MP3",
"Linear PCM, little endian",
"Nellymoser 16-kHz mono",
"Nellymoser 8-kHz mono",
"Nellymoser",
"G.711 A-law logarithmic PCM",
"G.711 mu-law logarithmic PCM",
"reserved",
"AAC",
"MP3 8-Khz",
"Device-specific sound",
};
const char* SoundRate[] = {
"5.5-kHz",
"11-kHz",
"22-kHz",
"44-kHz",
};
const char* SoundSize[] = 
{
"snd8Bit",
"snd16Bit",
};
const char* SoundType[] = 
{
"sndMono",
"sndStereo",
};
unsigned int ReverseInt24(unsigned int B)
{
    unsigned char* A = (unsigned char* )&B;
    return A[0]<<16|
         A[1]<<8|
         A[2];
}
unsigned int ReverseInt32(unsigned int B)
{
    unsigned char* A = (unsigned char* )&B;
    return A[0]<<24|
         A[1]<<16|
         A[2]<<8|
         A[3];
}
void ShowAudioInfo(const AudioInfo * paudioInfo)
{
    cout<<" SoundFormat : "<<SoundFormat[paudioInfo->SoundFormat]<<endl;
    cout<<" SoundRate : "<<SoundRate[paudioInfo->SoundRate]<<endl;
    cout<<" SoundSize : "<<SoundSize[paudioInfo->SoundSize]<<endl;
    cout<<" SoundType : "<<SoundType[paudioInfo->SoundType]<<endl;
}
void ShowTagInfo(const FLVTag * pflvTag, const unsigned char* buf)
{
    if(pflvTag->Type == 8)
    {
        cout<<"Audio Tag"<<endl;
        ShowAudioInfo((AudioInfo *)buf + 15);
    }
    else if(pflvTag->Type == 18)
    {
        cout<<"Script Tag"<<endl;
    }
    else if(pflvTag->Type == 9)
    {
        cout<<"Video Tag"<<endl;
    }
}

int main()
{
    unsigned char buf[1024];
    char flvname[255] = "101.flv";

    ifstream flvfile; 
    flvfile.open(flvname,ifstream::binary|ifstream::in);
    cout<<flvname<<endl;
    if(flvfile.is_open())
    {    
        long seekpos = 0;
        flvfile.seekg(0, ios::end);
        long length = flvfile.tellg();
        flvfile.seekg (0, ios::beg);
        flvfile.read((char*)buf,20);
        FLVHead    * pflvhead = (FLVHead *)buf;
        unsigned int seekoffset = pflvhead->DataOffset;
        while(!flvfile.eof())
        {
            flvfile.seekg (seekoffset, ios::beg);
         flvfile.read((char*)buf,1024);
            FLVTag * pflvTag = (FLVTag *)buf;
            pflvTag->SizeOfLastTag = ReverseInt32(pflvTag->SizeOfLastTag);
            pflvTag->DataSize = ReverseInt24(pflvTag->DataSize);
            pflvTag->TimeStamp = ReverseInt24(pflvTag->TimeStamp);
            cout<<"StreamID: "<<std::dec<<pflvTag->StreamID<<endl;
            cout<<"AVData : "<<std::dec<<pflvTag->AVData<<endl;
            pflvTag->StreamID = ReverseInt24(pflvTag->StreamID);
            cout<<"Offset : 0x"<<std::hex<<seekoffset<<endl;
            cout<<"Size : "<<std::dec<<pflvTag->DataSize<<endl;
            ShowTagInfo(pflvTag, buf);
            seekoffset += pflvTag->DataSize + 15;
            
//system("pause");

        }
    }
    return 0;
}

Tags:音频 信息
关于开源中文网 - 联系我们 - 广告服务 - 网站地图 - 版权声明