赞
踩
目前只用到了视频流。
主要步骤:
1、创建mp4文件
int wrap_create_h264_mp4_file(const char *filepath,const mp4_warp_h264_format *params,mp4_wrap_ret_handler *handler)
{
printf("open file :%s\n",filepath);
MP4FileHandle file = MP4CreateEx(filepath, 0, 1, 1, 0, 0, 0, 0);//创建mp4文件
if (file ==0)
{
printf("open file fialed.\n");
return -1;
}
handler->file_handler=file;
MP4SetTimeScale(file, params->timeScale);
// MP4TrackId MP4AddH264VideoTrack(MP4FileHandle hFile,
// uint32_t timeScale,
// MP4Duration sampleDuration,
// uint16_t width,
// uint16_t height,
// uint8_t AVCProfileIndication,
// uint8_t profile_compat,
// uint8_t AVCLevelIndication,
// uint8_t sampleLenFieldSizeMinusOne)
//添加h264 track
MP4TrackId video = MP4AddH264VideoTrack(file, params->timeScale, params->timeScale / params->fps, params->width, params->height,
0x4D, //sps[1] AVCProfileIndication
0x00, //sps[2] profile_compat
0x1f, //sps[3] AVCLevelIndication
3); // 4 bytes length before each NAL unit
if (video == MP4_INVALID_TRACK_ID)
{
printf("add video track failed.\n");
return -1;
}
MP4SetVideoProfileLevel(file, 0x7F);
handler->video_track=video;
/*
//添加aac音频
MP4TrackId audio = MP4AddAudioTrack(file, 48000, 1024, MP4_MPEG4_AUDIO_TYPE);
if (video == MP4_INVALID_TRACK_ID)
{
printf("add audio track failed.\n");
return;
}
MP4SetAudioProfileLevel(file, 0x2);
*/
handler->format=params;
return 0;
}
上面要注意那个AVCProfileIndication等的值,要与sps中一样。可以先保存一份起来供使用。
2、添加sps,pps信息
可以在前面创建文件后添加,也可以在循环获取视频流中发现有sps,pps就添加。
while(xxx)
...
if(packet->type.encH264Type==VIDENC_H264E_NALU_SPS){
//sps
//去掉start code
wrap_MP4AddH264SequenceParameterSet (mp4_handler.file_handler,mp4_handler.video_track,packet->pBuf+4,packet->length-4);
}
else if(packet->type.encH264Type==VIDENC_H264E_NALU_PPS){
//pps
//去掉start code
wrap_MP4AddH264PictureParameterSet (mp4_handler.file_handler,mp4_handler.video_track,
packet->pBuf+4,packet->length-4);
}
//这一步很重要,没有处理对的话,会造成无法播放
//参考http://www.cnblogs.com/chutianyao/archive/2012/04/13/2446140.html
uint32_t* p = packet->pBuf;
*p=htonl(packet->length -4);//大端,去掉头部四个字节
wrap_write_track_data(mp4_handler.file_handler,mp4_handler.video_track,packet->pBuf,packet->length,mp4_format.timeScale/mp4_format.fps);
file_size+=packet->length;
}//end while
3、添加sample信息
见第2步。
4、关闭文件
达到输出的要求后,如达到设定的大小或时间限制,则关闭文件流。
MP4Close(file,0);
问题:
265是否一样处理?
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。