使用mp4v2錄制mp4的Api還是挺少的,只要其中幾個(gè)概念理解了甚脉,寫起來很快,這個(gè)網(wǎng)址有API說明:https://linux.die.net/man/3/mp4setaudioprofilelevel
1将鸵、創(chuàng)建mp4文件
MP4Create(sFileName); //傳入要?jiǎng)?chuàng)建的mp4文件名
2.設(shè)置文件時(shí)間基
MP4SetTimeScale(m_hFile, 90000) //第一個(gè)參數(shù)為第一步創(chuàng)建的文件句柄勉盅,第二個(gè)參數(shù)為時(shí)間基,這里取視頻的采樣率90000
3.創(chuàng)建H264視頻track
MP4TrackId MP4AddH264VideoTrack( //返回track句柄
MP4FileHandle hFile, //創(chuàng)建的文件句柄
uint32_t timeScale, //該track的時(shí)間基顶掉,h264為90000
MP4Duration sampleDuration, //每幀的持續(xù)時(shí)間草娜,以時(shí)間基為基準(zhǔn),比如對于25fps痒筒,這里填90000/25=3600
uint16_t width, //視頻寬
uint16_t height, //視頻高
uint8_t AVCProfileIndication, //接下來3個(gè)參數(shù)代表h264編碼的profile-level-id,分別對應(yīng)sps第二宰闰、三、四個(gè)字節(jié),sps[1]
uint8_t profile_compat, //sps[2]
uint8_t AVCLevelIndication, //sps[3]
uint8_t sampleLenFieldSizeMinusOne ); //每個(gè)NALU單元前有幾個(gè)字節(jié)代表NALU的長度簿透,減去1就是這里要填的值移袍,這里我們填3.接下來我們寫h264數(shù)據(jù)的時(shí)候是要去掉NALU分割符
//0 0 0 1,然后在NALU前加4個(gè)字節(jié)代表NALU的長度(大端字節(jié)序)
void MP4SetVideoProfileLevel( MP4FileHandle hFile, uint8_t value ); //設(shè)置視頻遵循的協(xié)議老充,第一個(gè)參數(shù)為文件句柄葡盗,第二個(gè)參數(shù)我們設(shè)置為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( //設(shè)置sps
MP4FileHandle hFile, //文件句柄
MP4TrackId trackId, //視頻track句柄
const uint8_t* pSequence, //sps數(shù)據(jù)啡浊,注意不包括分隔符0 0 0 1
uint16_t sequenceLen ); //數(shù)據(jù)長度
void MP4AddH264PictureParameterSet( //設(shè)置pps
MP4FileHandle hFile, //文件句柄
MP4TrackId trackId, //視頻track句柄
const uint8_t* pPict, //pps數(shù)據(jù)觅够,注意不包括分隔符0 0 0 1
uint16_t pictLen ); //數(shù)據(jù)長度
4.創(chuàng)建AAC音頻track
MP4TrackId MP4AddAudioTrack( //創(chuàng)建音頻track胶背,返回track id
MP4FileHandle hFile, //MP4文件句柄
uint32_t timeScale, //音頻時(shí)間基,這里設(shè)置為采樣率8000
MP4Duration sampleDuration, //每幀時(shí)長喘先,以時(shí)間基為度量單位钳吟,對于AAC,每幀1024個(gè)采樣窘拯,所以這里設(shè)置為1024
uint8_t audioType DEFAULT(MP4_MPEG4_AUDIO_TYPE) ); //音頻type,這里設(shè)置為MP4_MPEG4_AUDIO_TYPE
void MP4SetAudioProfileLevel( MP4FileHandle hFile, uint8_t value ); //設(shè)置音頻遵從的協(xié)議红且,第一參數(shù)為mp4文件句柄,第二個(gè)我們設(shè)置為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( //設(shè)置音頻解碼配置參數(shù)
MP4FileHandle hFile, //mp4文件句柄
MP4TrackId trackId, //音頻track句柄
const uint8_t* pConfig, //AAC的audio-specific-config值直焙,兩個(gè)字節(jié),可以通過ADTS頭部計(jì)算出來
uint32_t configSize ); //長度
5.寫音視頻數(shù)據(jù)
bool MP4WriteSample( //寫音視頻數(shù)據(jù)
MP4FileHandle hFile, //MP4文件句柄
MP4TrackId trackId, //音頻或者視頻的track句柄
const uint8_t* pBytes, //音頻或者視頻數(shù)據(jù)砂轻。對于AAC,輸入純AAC數(shù)據(jù)斤吐,不帶adts頭搔涝;對于h264,去掉0 0 0 1分隔符和措,
//在NALU前面添加4字節(jié)表示NALU長度(大端字節(jié)序)
uint32_t numBytes, //數(shù)據(jù)長度
MP4Duration duration DEFAULT(MP4_INVALID_DURATION), //幀時(shí)長庄呈,以對應(yīng)的時(shí)間基為度量
MP4Duration renderingOffset DEFAULT(0), //默認(rèn)填0就好
bool isSyncSample DEFAULT(true) ); //對于h264,如果為IDR幀則為true,非IDR幀則填false
6.關(guān)閉mp4文件
void MP4Close( //關(guān)閉mp4文件
MP4FileHandle hFile, //文件句柄
uint32_t flags DEFAULT(0) ); //標(biāo)準(zhǔn)位派阱,我們填MP4_CLOSE_DO_NOT_COMPUTE_BITRATE诬留,這樣在關(guān)閉文件時(shí),不計(jì)算整個(gè)文件的大小贫母,這樣可以更快關(guān)閉文件
備注:之前項(xiàng)目上有用到文兑,我封裝了一個(gè)c++類來調(diào)用:
屏幕快照 2019-05-19 上午12.36.00.png