Chinaunix首页 | 论坛 | 博客
  • 博客访问: 158923
  • 博文数量: 56
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 502
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-18 14:21
文章分类

全部博文(56)

文章存档

2010年(39)

2009年(17)

我的朋友

分类: C/C++

2010-03-05 10:58:26

#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:8 ;
    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;
}


阅读(966) | 评论(0) | 转发(0) |
0

上一篇:Base64源码

下一篇:今天做了个性格测试

给主人留下些什么吧!~~