2024年1月3日发(作者:)
pNalu[2], // sps[2] profile_compat pNalu[3], // sps[3] AVCLevelIndication 3); // 4 bytes length before each NAL unit if (videoId == MP4_INVALID_TRACK_ID) { printf("Error:Can't add track.n"); return -1; }
MP4SetVideoProfileLevel(pHandle, 0x7F); addStream = 0; } MP4AddH264SequenceParameterSet(pHandle, videoId, pNalu, nNaluSize); return 0;} int addStream = 1;int WriteVideoFrame(MP4FileHandle pHandle, MP4TrackId& videoId, int& addStream, uint8_t *pBuf, int nDataSize){ int width = 640; //这3个参数要从SPS中解析出来 int height = 480; int frameRate = 25; int nSCPLen = 0; if(!is_frame_delimeter(pBuf, nSCPLen)) { printf("NALU error.n"); return -1; } assert(4 == nSCPLen); int nNaluSize = nDataSize - nSCPLen; unsigned char *pNalu = pBuf + nSCPLen; //pNalu = pBuf+4; unsigned char naluType = pNalu[0] & RL_NALU_TYPE_MASK; switch (naluType) { case 0x07: // SPS if(0 != WriteSPS(pHandle, pNalu, videoId, nNaluSize, timeScale, width, height, frameRate, addStream)) { MP4Close(pHandle, 0); return -1; } break;
case 0x08: // PPS //printf("pps(%d)n", nNaluSize); MP4AddH264PictureParameterSet(pHandle, videoId, pNalu, nNaluSize); break; default: //printf("slice(%d)n", nNaluSize); pBuf[0] = (nNaluSize>>24)&0xFF; pBuf[1] = (nNaluSize>>16)&0xFF; pBuf[2] = (nNaluSize>>8)&0xFF; pBuf[3] = (nNaluSize>>0)&0xFF; MP4WriteSample(pHandle, videoId, pBuf, nNaluSize + 4, MP4_INVALID_DURATION, 0, 1); break; }
if(!MP4SetTrackESConfiguration(pHandle, audioId, aEsConfig, sizeof(aEsConfig))) { printf("Fail to ES configure.n"); return -4; } } bool bRet = MP4WriteSample(pHandle, audioId, buf + nMetaLen,
nFrameSize - nMetaLen , MP4_INVALID_DURATION, 0, 1); if(!bRet) { printf("Fail to write audio frame.n");
return -3; } return nFrameSize;}int main() { MP4FileHandle pHandle = NULL; int ret = CreateMP4File(pHandle, argv[3]); if(0 != ret) { printf("Fail to create MP4 file.n"); return 1; } //... while(1) { nVideoRet = WriteVideoFrame(...); nAudioRet = WriteAudioFrame(...); } MP4Close(pHandle, 0); return 0;}


发布评论