2 c ollapse C_ollapse 于 2016.04.20 11:14 提问

关于h264+aac合成MP4问题

bool Mp4Mutex::init(int& nTrackId, int nSampleRate, int nChannal, int bitsPerSample,char* pMp4File )
{
//m_Mp4File = pMp4File;
m_nSampleRate = nSampleRate;
m_nAudioChannal = nChannal;
m_nBitsPerSample = bitsPerSample;

// init faac
m_hEncoder = faacEncOpen( nSampleRate, nChannal, &m_nInputSamples, &m_nMaxOutputBytes);

m_nMaxInputBytes=m_nInputSamples*bitsPerSample/8;   

m_pbPCMBuffer = new BYTE [m_nMaxInputBytes];
m_pOutAACBuffer = new BYTE [m_nMaxOutputBytes];
m_pTempBuffer = new BYTE [TEMP_BUFFER_SIZE];
memset(m_pTempBuffer, 0 , TEMP_BUFFER_SIZE);

// Get current encoding configuration
faacEncConfigurationPtr pConfiguration = faacEncGetCurrentConfiguration(m_hEncoder);
if( !pConfiguration )
{
    printf("GetCurrentConfiguration error!\n");
    return false;
}

//设置版本,录制MP4文件时要用MPEG4
pConfiguration->version = MPEG4 ;
pConfiguration->aacObjectType = LOW; //LC编码

//输入数据类型
pConfiguration->inputFormat = FAAC_INPUT_16BIT;

// outputFormat (0 = Raw; 1 = ADTS)
// 录制MP4文件时,要用raw流。检验编码是否正确时可设置为 adts传输流,
pConfiguration->outputFormat= 0;


//瞬时噪声定形(temporal noise shaping,TNS)滤波器
pConfiguration->shortctl = SHORTCTL_NORMAL;

pConfiguration->useTns=true;
//pConfiguration->useLfe=false;
pConfiguration->quantqual=100;
pConfiguration->bandWidth=0;
pConfiguration->bitRate=0;

//  Set encoding configuration
faacEncSetConfiguration( m_hEncoder, pConfiguration);


// 2.1 create mp4 file
//char tmp[255] = "E:\\test1.mp4";
m_Mp4File = MP4Create(pMp4File ,  0);
if ( m_Mp4File == MP4_INVALID_FILE_HANDLE)
{
    printf("open file fialed.\n");
    return false;
}

// 2.2 add audio track
m_AudioTrackId  = MP4AddAudioTrack( m_Mp4File, nSampleRate, m_nInputSamples , MP4_MPEG4_AUDIO_TYPE );
if (m_AudioTrackId == MP4_INVALID_TRACK_ID)
{
    //MP4Close(m_Mp4File);
    ///free(faacDecoderInfo);
    return false;
}
nTrackId = m_AudioTrackId;
// 2.3 set audio level  LC
MP4SetAudioProfileLevel(m_Mp4File, 0x2 ); //  

// 2.4 get decoder info
unsigned char* faacDecoderInfo = NULL;    
unsigned long  faacDecoderInfoSize = 0;    
if( faacEncGetDecoderSpecificInfo( m_hEncoder, &faacDecoderInfo, &faacDecoderInfoSize )) 
{        
    //MP4Close(m_Mp4File);
    free(faacDecoderInfo);
    return false ;   
} 

// 2.5 set encoder info [16bit-8000hz-1channal->{ 0x15, 0x88 } ]
bool bOk = MP4SetTrackESConfiguration(m_Mp4File, m_AudioTrackId, faacDecoderInfo, faacDecoderInfoSize );
if( !bOk )
{
    free(faacDecoderInfo);
    //MP4Close(m_Mp4File);
    return false;   
}
free( faacDecoderInfo );

return true;

}

void Mp4Mutex::StartWriteMp4(const unsigned char const pBufferG711,int nG711Len)
{
static FILE
fp = fopen("E:\3.g711a","wb");
fwrite(pBufferG711,nG711Len,1,fp);
fflush(fp);
if ( G711_BUFFER_SIZE < nG711Len )
{
}
BYTE szPcmBuff[PCM_BUFFER_SIZE] = {0};//1024
int nPcmLen = g711a_decode( (short*)szPcmBuff, pBufferG711, nG711Len );

memcpy(m_pTempBuffer+m_nTempPos, szPcmBuff ,nPcmLen ) ;
m_nTempPos += nPcmLen;

if ( m_nTempPos < m_nMaxInputBytes )
{
    return ;
}

memcpy(m_pbPCMBuffer, m_pTempBuffer ,m_nMaxInputBytes ) ;

char szTemp[2048]={0};
int nLeft = m_nTempPos-m_nMaxInputBytes;
memcpy( szTemp, (m_pTempBuffer+m_nMaxInputBytes), nLeft );
memset(m_pTempBuffer, 0, TEMP_BUFFER_SIZE );
memcpy( m_pTempBuffer, szTemp, nLeft );
m_nTempPos -= m_nMaxInputBytes ;

int nRet = faacEncEncode(m_hEncoder, (int*)m_pbPCMBuffer, m_nInputSamples, m_pOutAACBuffer, m_nMaxOutputBytes );
if ( nRet <= 0 )
{
    return ;
}
MP4WriteSample( m_Mp4File, m_AudioTrackId, (BYTE*)&m_pOutAACBuffer[0], nRet-0 , 1024, 0, true);

}

代码如上,直接从摄像机组播取H264+G711a的标准流,合成完MP4后没有音频,从收流处直接将g711a保存成文件单独使用如上代码dome转化为MP4后播放正常,求原因啊!!!!!!

2个回答

CSDNXIAON
CSDNXIAON   2016.04.20 11:22

关于ffmpeg封装h264为mp4的问题
使用mp4v2将H264+AAC合成mp4文件
使用mp4v2将H264+AAC合成mp4文件
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

C_ollapse
C_ollapse   2016.04.26 11:54

查到一个问题编码完的音频数据编码显示为 ftypmp42 mp42avc1 n頼dat? libfaac 1.28,avc1应该是视频编码吧。。。,但是找不到哪里的问题导致的啊。。。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!