首先得跳过 9字节的FLV文件头,然后在分析tag头 (11 字节)确定是否为视频 tag. 09:视频 08:音频
00 00 2D: tag data size:(并不全是h264裸流,还有些一下其他信息)
蓝色部分即为 tag data 区:
第一个字节分析 : 17 --视频格式信息
c=fgetc(myopen);
video_type=(c&0xf0);
video_type =video_type>>4;
switch(video_type){
case 1:strcat(str,"key frame ");break;
case 2:strcat(str,"inter frame");break;
case 3:strcat(str,"disposable inter frame");break;
case 4:strcat(str,"generated keyframe");break;
case 5:strcat(str,"video info/command frame");break;
default:strcat(str,"UNKNOWN");break;
}
video_type=(c&0x0f);
strcat(str,"| ");
switch (video_type)
{
case 1:strcat(str,"JPEG (currently unused)");break;
case 2:strcat(str,"Sorenson H.263");break;
case 3:strcat(str,"Screen video");break;
case 4:strcat(str,"On2 VP6");break;
case 5:strcat(str,"On2 VP6 with alpha channel");break;
case 6:strcat(str,"Screen video version 2");break;
case 7:strcat(str,"AVC");break;
default:strcat(str,"UNKNOWN");break;
}
提取 h264代码:
void H264JIEXI(int datelength)//关键
{
int info = 0;
Read8(info, flvfile); // 17
int avctype = 0;
Read8(avctype, flvfile); //第一个视频关键帧 tag :00
FSEEK(3, flvfile); // 跳过之后 3字节
int templength = 0;
char*tempbuff = NULL;
if (avctype == 0) // 提取 sps pps
{
FSEEK(6, flvfile); // 后移6
Read16(templength, flvfile); // 读取 sps 长度 --2字节
printf("sssize:%d\n", templength);
tempbuff = (char*)malloc(templength);
fread(tempbuff, 1, templength, flvfile); //读sps
fwrite(&h264space, 1, 4, h264file); //nal 头 00 00 00 01
fwrite(tempbuff, 1, templength, h264file);
free(tempbuff);
Read8(templength, flvfile);//ppsnum // 读pps 个数
Read16(templength, flvfile);//ppssize // 读pps 长度 ----2字节
printf("ppsize:%d\n", templength);
tempbuff = (char*)malloc(templength);
fread(tempbuff, 1, templength, flvfile);
fwrite(&h264space, 1, 4, h264file);
fwrite(tempbuff, 1, templength, h264file);
free(tempbuff);
}
else
{
// Read32(templength,flvfile);
// tempbuff=(char*)malloc(templength);
// fread(tempbuff,1,templength,flvfile);
// fwrite(&h264space,1,4,h264file);
// fwrite(tempbuff,1,templength,h264file);
// free(tempbuff);
//可能存在多个nal,需全部读取
int countsize = 2 + 3;
static int gg = 0;
while (countsize < datelength)
{
Read32(templength, flvfile); // 四个字节长度
tempbuff = (char*)malloc(templength);
printf("templength =%d\n", templength);
fread(tempbuff, 1, templength, flvfile);
fwrite(&h264space, 1, 4, h264file);
fwrite(tempbuff, 1, templength, h264file);
free(tempbuff);
countsize += (templength + 4);
}
}
}