视频数据使用h264进行编码的,想把编码后的数据写入MP4文件中,但是需要写文件头,具体怎么实现。
看了相关资料说涉及到 Ftyp box、Moov box、Mdat box,其中这三个box的大小是怎么得到的?希望各位指点,谢谢!
------解决方案--------------------------------------------------------
你可以查一下 mpeg4ip方面的的东西,如果 你用它的话,可以根据mpeg4ip\server\mp4live这个目录下的
file_mp4_recorder.cpp文件写一个录制MP4的程序。
------解决方案--------------------------------------------------------
我用的mp4v2-1.9.1写的,非常好用简单
创建文件并生成流信息如下(我生成的是h264+aac的mp4文件)
m_Mp4File = MP4Create(fileName, 9);
if(m_Mp4File == MP4_INVALID_FILE_HANDLE)
{
hr = E_FAIL;
return hr;
}
if(!MP4SetTimeScale(m_Mp4File, 90000))
{
MP4Close(m_Mp4File);
m_Mp4File = MP4_INVALID_FILE_HANDLE;
hr = E_FAIL;
return hr;
}
MP4SetVideoProfileLevel(m_Mp4File, 0x0F);
MP4SetAudioProfileLevel(m_Mp4File, 0x0F);
m_Mp4VideoId = MP4AddH264VideoTrack(m_Mp4File, 90000, MP4_INVALID_DURATION, mEncodeX264Width, mEncodeX264Height, 0, 0, 0, 0);
if(m_Mp4VideoId == MP4_INVALID_TRACK_ID )
{
MP4Close(m_Mp4File);
m_Mp4File = MP4_INVALID_FILE_HANDLE;
hr = E_FAIL;
return hr;
}
m_Mp4AudioId = MP4AddAudioTrack(m_Mp4File, USE_SAMPLE_RATE, MP4_INVALID_DURATION);
if(m_Mp4AudioId == MP4_INVALID_TRACK_ID )
{
MP4Close(m_Mp4File);
m_Mp4File = MP4_INVALID_FILE_HANDLE;
hr = E_FAIL;
return hr;
}
unsigned char* faacDecoderInfo = NULL;
unsigned long faacDecoderInfoSize = 0;
if( faacEncGetDecoderSpecificInfo(mMyOutPut.hEncoder, &faacDecoderInfo, &faacDecoderInfoSize ))
{
MP4Close(m_Mp4File);
m_Mp4File = MP4_INVALID_FILE_HANDLE;
hr = E_FAIL;
return hr;
}
if(!MP4SetTrackESConfiguration(m_Mp4File, m_Mp4AudioId, faacDecoderInfo, faacDecoderInfoSize ))
{
free(faacDecoderInfo);
MP4Close(m_Mp4File);
m_Mp4File = MP4_INVALID_FILE_HANDLE;
hr = E_FAIL;
return hr;
}
free( faacDecoderInfo );
写数据就是:
MP4WriteSample(m_Mp4File, m_Mp4VideoId, &mux_buffer[0], pos, dur * 90000 / 1E7, 0, true);
MP4WriteSample(m_Mp4File, m_Mp4AudioId, mMyOutPut.bitbuf + totalbytesWritten, bytesWritten, dur * USE_SAMPLE_RATE / 1E7, 0, true);