java MP4 解析 第一步 解析ftyp box

ftyp是MP4文件的第一个Box,包含了视频文件使用的编码格式、标准等,下面是一段定义和MP4文件头的解析

int len;
byte[] ftype = new byte[4];
byte[]  majorBrand = new byte[4];
int fversion;
byte[] compatibleBrands = new byte[12];

初步解析实现:

	public static void main(String[] args) throws Exception {
		int len;
		byte[] ftype = new byte[4];
		byte[]  majorBrand = new byte[4];
		int fversion;
		byte[] compatibleBrands = new byte[12];
		
		InputStream in = new FileInputStream("F:\\测试文件\\video\\testKongBu.mp4");
		//len
		byte[] b = new byte[4];
		in.read(b);
		len = Array.bytesToInt(b);//byte[]转int  下同
		
		//ftype
		in.read(ftype);
		//majorBrand
		in.read(majorBrand);
		//fversion
		in.read(b);
		fversion = Array.bytesToInt(b);
		//compatibleBrands
		in.read(compatibleBrands);
		
		System.out.println("len\t\t=\t"+len);
		System.out.println("ftyp\t\t=\t"+new String(ftype));
		System.out.println("majorBrand\t=\t"+new String(majorBrand));
		System.out.println("fversion\t=\t"+fversion);
		System.out.println("compatibleBrands=\t"+new String(compatibleBrands));
		
		in.close();
    }

解析结果:

附:

byte[]转int代码    MP4默认是网络字节序(高字节序)

测试文件:

链接:https://pan.baidu.com/s/1lFrIJqI5TzQAhLH1SOGMDQ 
提取码:u98c 

发布了44 篇原创文章 · 获赞 31 · 访问量 3万+
展开阅读全文

关于h264+aac合成MP4问题

04-20

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后播放正常,求原因啊!!!!!! 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览