MP4v2 基本使用(二)

时间:2023-03-09 15:57:58
MP4v2 基本使用(二)

MP4转H264文件

本文最目标是基本的MP4v2接口的使用,并且实现一个简单的提取mp4文件里面264流的功能;

1.264文件基本知识

MP4v2 基本使用(二)

下看看H264文件的基本常识 
NAL 头 0x00 0x00 0x00 0x01 
sps :nal+0x67开头 
pps :nal+0x68开头

I帧 0x65 开头 
P帧… 
具体的参考别人的解析 
http://blog.****.net/jefry_xdz/article/details/8461343

2.mp4v2提取264文件的代码

 #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <sys/time.h>
#include <mp4v2/mp4v2.h> unsigned char sps[],pps[];
int spslen = ,ppslen = ; int get264stream(MP4FileHandle oMp4File,int VTrackId,int totalFrame)
{
if(!oMp4File) return -;
char NAL[] = {0x00,0x00,0x00,0x01};
unsigned char *pData = NULL;
unsigned int nSize = ;
MP4Timestamp pStartTime;
MP4Duration pDuration;
MP4Duration pRenderingOffset;
bool pIsSyncSample = ; int nReadIndex = ;
FILE *pFile = NULL;
pFile = fopen("out.h264","wb"); while(nReadIndex < totalFrame)
{
nReadIndex ++;
//printf("nReadIndex:%d\n",nReadIndex);
MP4ReadSample(oMp4File,VTrackId,nReadIndex,&pData,&nSize,&pStartTime,&pDuration,&pRenderingOffset,&pIsSyncSample); //IDR֡ 帧,写入sps pps先
if(pIsSyncSample)
{
fwrite(NAL,,,pFile);
fwrite(sps,spslen,,pFile); fwrite(NAL,,,pFile);
fwrite(pps,ppslen,,pFile); }
//264frame
if(pData && nSize > )
{
//标准的264帧,前面几个字节就是frame的长度.
//需要替换为标准的264 nal 头.
pData[] = 0x00;
pData[] = 0x00;
pData[] = 0x00;
pData[] = 0x01;
fwrite(pData,nSize,,pFile);
} //如果传入MP4ReadSample的视频pData是null
// 它内部就会new 一个内存
//如果传入的是已知的内存区域,
//则需要保证空间bigger then max frames size.
free(pData);
pData = NULL;
}
fflush(pFile);
fclose(pFile); return ;
}
int openmp4file(char *sMp4file)
{
MP4FileHandle oMp4File;
int i; //unsigned int oStreamDuration;
unsigned int oFrameCount; oMp4File = MP4Read(sMp4file);
int videoindex = -,audioindex = -;
uint32_t numSamples;
//uint32_t timescale;
//uint64_t duration; if (!oMp4File)
{
printf("Read error....%s\r\n",sMp4file);
return -;
} MP4TrackId trackId = MP4_INVALID_TRACK_ID;
uint32_t numTracks = MP4GetNumberOfTracks(oMp4File,NULL,);
printf("numTracks:%d\n",numTracks); for (i = ; i < numTracks; i++)
{
trackId = MP4FindTrackId(oMp4File, i,NULL,);
const char* trackType = MP4GetTrackType(oMp4File, trackId);
if (MP4_IS_VIDEO_TRACK_TYPE(trackType))
{
//printf("[%s %d] trackId:%d\r\n",__FUNCTION__,__LINE__,trackId);
videoindex= trackId; //duration = MP4GetTrackDuration(oMp4File, trackId );
numSamples = MP4GetTrackNumberOfSamples(oMp4File, trackId);
//timescale = MP4GetTrackTimeScale(oMp4File, trackId);
//oStreamDuration = duration/(timescale/1000);
oFrameCount = numSamples; // read sps/pps
uint8_t **seqheader;
uint8_t **pictheader;
uint32_t *pictheadersize;
uint32_t *seqheadersize;
uint32_t ix;
MP4GetTrackH264SeqPictHeaders(oMp4File, trackId, &seqheader, &seqheadersize, &pictheader, &pictheadersize); for (ix = ; seqheadersize[ix] != ; ix++)
{
memcpy(sps, seqheader[ix], seqheadersize[ix]);
spslen = seqheadersize[ix];
free(seqheader[ix]);
}
free(seqheader);
free(seqheadersize); for (ix = ; pictheadersize[ix] != ; ix++)
{
memcpy(pps, pictheader[ix], pictheadersize[ix]);
ppslen = pictheadersize[ix];
free(pictheader[ix]);
}
          free(pictheader);
free(pictheadersize);
}
         else if (MP4_IS_AUDIO_TRACK_TYPE(trackType))
{
audioindex = trackId;
printf("audioindex:%d\n",audioindex);
}
} //解析完了mp4,主要是为了获取sps pps 还有video的trackID
if(videoindex >= )
get264stream(oMp4File,videoindex,oFrameCount); //需要mp4close 否则在嵌入式设备打开mp4上多了会内存泄露挂掉.
MP4Close(oMp4File,);
return ;
} int main(void)
{
openmp4file("test.mp4");
return ;
}

3.测试文件ffproe的信息如下.

MP4v2 基本使用(二)

测试的mp4文件和生成的264文件信息如下,用播放器播放可以正常显示图像.