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);    
   

转载于:https://www.cnblogs.com/mingzhang/p/8515336.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值