使用mp4v2录制mp4的Api还是挺少的,只要其中几个概念理解了,写起来很快,这个网址有API说明:https://linux.die.net/man/3/mp4setaudioprofilelevel
1、创建mp4文件
MP4Create(sFileName); //传入要创建的mp4文件名
2.设置文件时间基
MP4SetTimeScale(m_hFile, 90000) //第一个参数为第一步创建的文件句柄,第二个参数为时间基,这里取视频的采样率90000
3.创建H264视频track
MP4TrackId MP4AddH264VideoTrack( //返回track句柄
MP4FileHandle hFile, //创建的文件句柄
uint32_t timeScale, //该track的时间基,h264为90000
MP4Duration sampleDuration, //每帧的持续时间,以时间基为基准,比如对于25fps,这里填90000/25=3600
uint16_t width, //视频宽
uint16_t height, //视频高
uint8_t AVCProfileIndication, //接下来3个参数代表h264编码的profile-level-id,分别对应sps第二、三、四个字节,sps[1]
uint8_t profile_compat, //sps[2]
uint8_t AVCLevelIndication, //sps[3]
uint8_t sampleLenFieldSizeMinusOne ); //每个NALU单元前有几个字节代表NALU的长度,减去1就是这里要填的值,这里我们填3.接下来我们写h264数据的时候是要去掉NALU分割符
//0 0 0 1,然后在NALU前加4个字节代表NALU的长度(大端字节序)
void MP4SetVideoProfileLevel( MP4FileHandle hFile, uint8_t value ); //设置视频遵循的协议,第一个参数为文件句柄,第二个参数我们设置为1,定义如下:
MP4SetVideoProfileLevel sets the minumum profile/level of MPEG-4 video support necessary to render the contents of the file.
ISO/IEC 14496-1:2001 MPEG-4 Systems defines the following values:
0x00 Reserved
0x01 Simple Profile @ Level 3
0x02 Simple Profile @ Level 2
0x03 Simple Profile @ Level 1
0x04 Simple Scalable Profile @ Level 2
0x05 Simple Scalable Profile @ Level 1
0x06 Core Profile @ Level 2
0x07 Core Profile @ Level 1
0x08 Main Profile @ Level 4
0x09 Main Profile @ Level 3
0x0A Main Profile @ Level 2
0x0B N-Bit Profile @ Level 2
0x0C Hybrid Profile @ Level 2
0x0D Hybrid Profile @ Level 1
0x0E Basic Animated Texture @ Level 2
0x0F Basic Animated Texture @ Level 1
0x10 Scalable Texture @ Level 3
0x11 Scalable Texture @ Level 2
0x12 Scalable Texture @ Level 1
0x13 Simple Face Animation @ Level 2
0x14 Simple Face Animation @ Level 1
0x15-0x7F Reserved
0x80-0xFD User private
0xFE No audio profile specified
0xFF No audio required
void MP4AddH264SequenceParameterSet( //设置sps
MP4FileHandle hFile, //文件句柄
MP4TrackId trackId, //视频track句柄
const uint8_t* pSequence, //sps数据,注意不包括分隔符0 0 0 1
uint16_t sequenceLen ); //数据长度
void MP4AddH264PictureParameterSet( //设置pps
MP4FileHandle hFile, //文件句柄
MP4TrackId trackId, //视频track句柄
const uint8_t* pPict, //pps数据,注意不包括分隔符0 0 0 1
uint16_t pictLen ); //数据长度
4.创建AAC音频track
MP4TrackId MP4AddAudioTrack( //创建音频track,返回track id
MP4FileHandle hFile, //MP4文件句柄
uint32_t timeScale, //音频时间基,这里设置为采样率8000
MP4Duration sampleDuration, //每帧时长,以时间基为度量单位,对于AAC,每帧1024个采样,所以这里设置为1024
uint8_t audioType DEFAULT(MP4_MPEG4_AUDIO_TYPE) ); //音频type,这里设置为MP4_MPEG4_AUDIO_TYPE
void MP4SetAudioProfileLevel( MP4FileHandle hFile, uint8_t value ); //设置音频遵从的协议,第一参数为mp4文件句柄,第二个我们设置为2,定义如下:
MP4SetAudioProfileLevel sets the minumum profile/level of MPEG-4 audio support necessary to render the contents of the file.
ISO/IEC 14496-1:2001 MPEG-4 Systems defines the following values:
0x00 Reserved
0x01 Main Profile @ Level 1
0x02 Main Profile @ Level 2
0x03 Main Profile @ Level 3
0x04 Main Profile @ Level 4
0x05 Scalable Profile @ Level 1
0x06 Scalable Profile @ Level 2
0x07 Scalable Profile @ Level 3
0x08 Scalable Profile @ Level 4
0x09 Speech Profile @ Level 1
0x0A Speech Profile @ Level 2
0x0B Synthesis Profile @ Level 1
0x0C Synthesis Profile @ Level 2
0x0D Synthesis Profile @ Level 3
0x0E-0x7F Reserved
0x80-0xFD User private
0xFE No audio profile specified
0xFF No audio required
bool MP4SetTrackESConfiguration( //设置音频解码配置参数
MP4FileHandle hFile, //mp4文件句柄
MP4TrackId trackId, //音频track句柄
const uint8_t* pConfig, //AAC的audio-specific-config值,两个字节,可以通过ADTS头部计算出来
uint32_t configSize ); //长度
5.写音视频数据
bool MP4WriteSample( //写音视频数据
MP4FileHandle hFile, //MP4文件句柄
MP4TrackId trackId, //音频或者视频的track句柄
const uint8_t* pBytes, //音频或者视频数据。对于AAC,输入纯AAC数据,不带adts头;对于h264,去掉0 0 0 1分隔符,
//在NALU前面添加4字节表示NALU长度(大端字节序)
uint32_t numBytes, //数据长度
MP4Duration duration DEFAULT(MP4_INVALID_DURATION), //帧时长,以对应的时间基为度量
MP4Duration renderingOffset DEFAULT(0), //默认填0就好
bool isSyncSample DEFAULT(true) ); //对于h264,如果为IDR帧则为true,非IDR帧则填false
6.关闭mp4文件
void MP4Close( //关闭mp4文件
MP4FileHandle hFile, //文件句柄
uint32_t flags DEFAULT(0) ); //标准位,我们填MP4_CLOSE_DO_NOT_COMPUTE_BITRATE,这样在关闭文件时,不计算整个文件的大小,这样可以更快关闭文件
备注:之前项目上有用到,我封装了一个c++类来调用: