H264与AAC ES打包成MP4
注意
设置图像的sps pps
MP4AddH264SequenceParameterSet(file,video,sps,sizeof(sps));
MP4AddH264PictureParameterSet(file,video,pps, sizeof(pps));
设置音频解码信息
MP4SetTrackESConfiguration(file,audio,pConfig,configSize);
MP4FileHandle file = MP4CreateEx("new.mp4");//创建mp4文件
if (file == MP4_INVALID_FILE_HANDLE){
printf("open file fialed.\n");
return -1;
}
MP4SetTimeScale(file, 1000);
//添加h264 track
MP4TrackId video = MP4AddH264VideoTrack(file, 12800, 12800 / 25, 1280, 720,
0x4d, //sps[1] AVCProfileIndication
0x40, //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, 0x01);
// write sps
unsigned char sps[] = {0x67,0x4D,0x40,0x1F,0xDA,0x01,0x40,0x16,0xEC,0x04,0x40,0x00,0x00,0x03,0x00,0x40,0x00,0x00,0x0C,0x83 ,0xC6 ,0x0C ,0xA8};
unsigned char pps[] = {0x68 ,0xEF ,0x3C ,0x80};
MP4AddH264SequenceParameterSet(file,video,sps,sizeof(sps));
// write pps
MP4AddH264PictureParameterSet(file,video,pps, sizeof(pps));
//添加aac音频
MP4TrackId audio = MP4AddAudioTrack(file, 48000, 1024, MP4_MPEG4_AUDIO_TYPE);
if (audio == MP4_INVALID_TRACK_ID){
printf("add audio track failed.\n");
return -1;
}
MP4SetAudioProfileLevel(file, 0x2);
unsigned char Buf[409600] = {0};
for(int i = 0; i < sampleTotalCount[0]; ++i){
fseek(fp, sampleList[0][i].offset, SEEK_SET);
fread(Buf, sampleList[0][i].size, 1, fp);
printf("\nsample_list[%d].size:%d#", i, sampleList[0][i].size);
for (int k = 0; k < 16; ++k){
printf("%02x ", Buf[k]);
}
MP4WriteSample(file, video, (uint8_t*)(Buf), sampleList[0][i].size, sampleList[0][i].delta, 0, 1);
}
for(int i = 0; i < sampleTotalCount[1]; ++i){
fseek(fp, sampleList[1][i].offset, SEEK_SET);
fread(Buf, sampleList[1][i].size, 1, fp);
MP4WriteSample(file, audio, (uint8_t*)Buf, sampleList[1][i].size , sampleList[1][i].delta, 0, 1);
}
MP4Close(file);