FLV文件格式解析部分代码
转自:http://www.lougoo.com/blog/article.asp?id=856
前面在FLV文件格式大揭秘中介绍了FLV文件格式,其中也谈到了曾经编写的代码解析flv文件。应网友要求,现在把代码贴出来,仅供大家参考。因为这代码还是在2006年写的,现在看来有些地方还不够准确,解析的也不够完全,所以仅供参考。
flvheadpacket.h
- #include "packet.h"
- class CFlvHeadPacket : public CPacket
- {
- public:
- CFlvHeadPacket(void);
- CFlvHeadPacket(UInt8* pContent, UInt32 nLength);
- ~CFlvHeadPacket(void);
- };
flvheadpacket.cpp
- #include "stdafx.h"
- #include "flvheadpacket.h"
- CFlvHeadPacket::CFlvHeadPacket(void)
- {
- m_nType = FLV_HEAD;
- }
- CFlvHeadPacket::CFlvHeadPacket(UInt8* pContent, UInt32 nLength)
- {
- m_nType = FLV_HEAD;
- m_pContent = pContent;
- m_nLength = nLength;
- }
- CFlvHeadPacket::~CFlvHeadPacket(void)
- {
- }
flvmetapacket.h
- #include "packet.h"
- class CFlvMetaPacket : public CPacket
- {
- public:
- CFlvMetaPacket(void);
- CFlvMetaPacket(UInt8* pContent, UInt32 nLength);
- ~CFlvMetaPacket(void);
- };
flvmetapacket.cpp
- #include "stdafx.h"
- #include "flvmetapacket.h"
- CFlvMetaPacket::CFlvMetaPacket(void)
- {
- m_nType = FLV_META;
- }
- CFlvMetaPacket::CFlvMetaPacket(UInt8* pContent, UInt32 nLength)
- {
- m_nType = FLV_META;
- m_pContent = pContent;
- m_nLength = nLength;
- }
- CFlvMetaPacket::~CFlvMetaPacket(void)
- {
- }
flvdatapacket.h
- #include "packet.h"
- enum{
- FLV_UNKNOWN_FRAME,
- FLV_VIDEO_FRAME,
- FLV_AUDIO_FRAME
- };
- class CFlvDataPacket : public CPacket
- {
- public:
- CFlvDataPacket(void);
- CFlvDataPacket(UInt8* pContent, UInt32 nLength);
- ~CFlvDataPacket(void);
- void Print();
- UInt8 m_nDataType;
- UInt32 m_nTime;
- UInt8 m_nFlag;
- };
flvdatapacket.cpp
- #include "stdafx.h"
- #include "flvdatapacket.h"
- CFlvDataPacket::CFlvDataPacket(void)
- {
- m_nType = FLV_DATA;
- m_nDataType = FLV_UNKNOWN_FRAME;
- }
- CFlvDataPacket::CFlvDataPacket(UInt8* pContent, UInt32 nLength)
- {
- m_nType = FLV_DATA;
- m_pContent = pContent;
- m_nLength = nLength;
- if ( m_pContent[0] == 8 )
- m_nDataType = FLV_AUDIO_FRAME;
- else if ( m_pContent[0] == 9 )
- m_nDataType = FLV_VIDEO_FRAME;
- else
- m_nDataType = FLV_UNKNOWN_FRAME;
- m_nTime = FOUR_CHARS_TO_INT(0,m_pContent[4],m_pContent[5],m_pContent[6]);
- m_nFlag = m_pContent[11];
- }
- CFlvDataPacket::~CFlvDataPacket(void)
- {
- }
- void CFlvDataPacket::Print()
- {
- printf("Type:%d,DataType=%d,Time=%d,Flag=%d,Length=%d/n",m_nType,m_nDataType,m_nTime,m_nFlag,m_nLength);
- }
flvreader.h
- #ifndef __FLVREADER_H__
- #define __FLVREADER_H__
- #define FLV_HEAD_LENGTH 13
- #define FLV_DATA_HEAD_LENGTH 11
- class CFlvDataPacket;
- class CFlvHeadPacket;
- class CFlvMetaPacket;
- class CFlvReader
- {
- public:
- CFlvReader(void);
- ~CFlvReader(void);
- public:
- SInt8* m_sFile;
- FILE* m_pFile;
- UInt32 m_nOffset;
- BOOL SetFile(SInt8* sFile);
- FILESTAT64 GetFileStat64();
- FILESTAT GetFileStat();
- CFlvHeadPacket* ReadHeader();
- CFlvMetaPacket* ReadMeta();
- CFlvDataPacket* GetNextDataPacket();
- };
- #endif
flvreader.cpp
- #include "stdafx.h"
- #include "flvreader.h"
- #include "flvdatapacket.h"
- #include "flvheadpacket.h"
- #include "flvmetapacket.h"
- CFlvReader::CFlvReader(void)
- {
- m_sFile = NULL;
- m_pFile = NULL;
- m_nOffset = 0;
- }
- CFlvReader::~CFlvReader(void)
- {
- if ( m_pFile != NULL )
- {
- fclose( m_pFile );
- m_pFile = NULL;
- }
- }
- BOOL CFlvReader::SetFile(SInt8* sFile)
- {
- if ( m_pFile != NULL )
- {
- fclose( m_pFile );
- m_pFile = NULL;
- }
- m_sFile = sFile;
- m_pFile = fopen( m_sFile, "rb" );
- return m_pFile != NULL;
- }
- CFlvHeadPacket* CFlvReader::ReadHeader()
- {
- UInt8* pContent = new UInt8[FLV_HEAD_LENGTH];
- UInt32 nRead = fread( pContent, sizeof(SInt8), FLV_HEAD_LENGTH, m_pFile );
- if ( nRead == FLV_HEAD_LENGTH )
- {
- return new CFlvHeadPacket(pContent, FLV_HEAD_LENGTH);
- }
- else
- {
- delete pContent;
- return NULL;
- }
- }
- CFlvMetaPacket* CFlvReader::ReadMeta()
- {
- UInt8 buf[FLV_DATA_HEAD_LENGTH];
- UInt32 nRead = fread( buf, sizeof(UInt8), FLV_DATA_HEAD_LENGTH, m_pFile );
- if ( nRead == FLV_DATA_HEAD_LENGTH )
- {
- if ( buf[0] == 18 )//meta
- {
- UInt32 nSize = FOUR_CHARS_TO_INT(0,buf[1],buf[2],buf[3]);
- UInt8* pContent = new UInt8[nSize+FLV_DATA_HEAD_LENGTH+4];
- memcpy(pContent,buf,FLV_DATA_HEAD_LENGTH);
- nRead = fread(&pContent[FLV_DATA_HEAD_LENGTH],sizeof(UInt8), nSize+4, m_pFile);
- if ( nRead = (nSize+4) )
- {
- return new CFlvMetaPacket(pContent,nSize+FLV_DATA_HEAD_LENGTH+4);
- }
- else
- {
- delete pContent;
- return NULL;
- }
- }
- }
- return NULL;
- }
- CFlvDataPacket* CFlvReader::GetNextDataPacket()
- {
- UInt8 buf[FLV_DATA_HEAD_LENGTH];
- UInt32 nRead = fread( buf, sizeof(UInt8), FLV_DATA_HEAD_LENGTH, m_pFile );
- if ( nRead == FLV_DATA_HEAD_LENGTH )
- {
- if ( buf[0] == 8 || buf[0] == 9 )
- {
- UInt32 nSize = FOUR_CHARS_TO_INT(0,buf[1],buf[2],buf[3]);
- UInt8* pContent = new UInt8[nSize+FLV_DATA_HEAD_LENGTH+4];
- memcpy(pContent,buf,FLV_DATA_HEAD_LENGTH);
- nRead = fread(&pContent[FLV_DATA_HEAD_LENGTH],sizeof(UInt8), nSize+4,m_pFile);
- if ( nRead = (nSize+4) )
- {
- return new CFlvDataPacket(pContent,nSize+FLV_DATA_HEAD_LENGTH+4);
- }
- else
- {
- delete pContent;
- return NULL;
- }
- }
- }
- return NULL;
- }
- FILESTAT CFlvReader::GetFileStat()
- {
- FILESTAT fileStat;
- memset(&fileStat,0,sizeof(FILESTAT));
- if ( m_pFile != NULL )
- {
- if ( _stat(m_sFile,&fileStat) != 0 )
- {
- printf( "/nGetFileStat Error!/n" );
- }
- }
- return fileStat;
- }
- FILESTAT64 CFlvReader::GetFileStat64()
- {
- FILESTAT64 fileStat;
- memset(&fileStat,0,sizeof(FILESTAT64));
- if ( m_pFile != NULL )
- {
- if ( _stat64(m_sFile,&fileStat) != 0 )
- {
- printf( "/nGetFileStat64 Error!/n" );
- }
- }
- return fileStat;
- }