使用mp4v2产生mp4文件

目前只用到了视频流。

主要步骤:
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是否一样处理?

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值