一个ps解复用H264和H264打包ps的动态库及demo,支持Video和Audio。
下载地址:
百度网盘:https://pan.baidu.com/s/1dFB7oOh 密码:rm8r
CSDN:http://download.csdn.net/download/aflyeaglenku/10168479(csdn下载现在不能0积分下载,所以笔者只能选择最低的2积分下载。没有积分的朋友可以从百度网盘下载。)
笔者使用的编译器是vs2008+sp1。读者需要将代码和库都下载下来自行编译后运行。
示例代码:
#include <stdio.h>
#include <windows.h>
#include "demuxer.h"
#include "muxer.h"
FILE *m_input_ps = NULL;
FILE *m_output_es = NULL;
FILE *m_output_ps = NULL;
void *m_pDemux = NULL;
void *m_pMuxer = NULL;
int m_input_ps_len=0;
int m_output_es_len=0;
int m_output_ps_len=0;
/*
ps-demux 解复用输出es
*/
static void PsDeMuxerCB(unsigned char streamType, void * data, int size, long long pts, bool key, void* param)
{
m_output_es_len += size;
//printf("got es! streamType = %x\r\n",streamType);
/*
在回调函数中,将h264 的data保存写入文件中。
*/
//printf("got es! stream_id = %d,pts=%lld,iskey=%d,size=%d\r\n",streamid,pts,key,size);
//PINFO <<"got es! stream_id = "<<int(streamid)<<",pts="<<pts<<",iskey="<<key<<",size="<<size;
fwrite(data,1,size,m_output_es);
/*
在回调函数中,将h264 的data保存写入打包器中。
*/
EsInfo stPesInfo; memset(&stPesInfo,0,sizeof(EsInfo));
if(streamType == VIDEO_TYPE_H264)
stPesInfo.estype = key ? MUXSER_ESTYPE_I_SLICE:MUXSER_ESTYPE_P_SLICE;
else if(streamType == AUDIO_TYPE_ALL)
stPesInfo.estype = MUXSER_ESTYPE_AUDIO;
stPesInfo.timestamp = pts;
InputEsData(m_pMuxer, (char*)data, size, &stPesInfo);
}
/*
ps-muxer 输出
*/
static void PsMuxerCB(char* pDataBuf, unsigned long nDataLen, PEsInfo pstPesInfo,void* pUserData)
{
m_output_ps_len += nDataLen;
fwrite(pDataBuf,1,nDataLen,m_output_ps);
}
int main(int argc, char **argv)
{
int trytimes=100;
while(trytimes-->0)
{
int buflen = 1024*16;
unsigned char *buffer = new unsigned char[buflen];
m_input_ps = fopen("2017_10_22T19_48_49.295.ps","rb");
m_output_es = fopen("2017_10_22T19_48_49.295.h264","wb+");
m_output_ps = fopen("2017_10_22T19_48_49.295.ps.ps","wb+");
m_pDemux = CreatePsDeMuxer(PsDeMuxerCB, 0);
m_pMuxer = CreatePsMuxer(PsMuxerCB, 0, MUXSER_VIDEO_TYPE_GNER_H264 + MUXSER_AUDIO_TYPE_GNER_G711);
while(true)
{
int thisread = fread(buffer,1,buflen,m_input_ps);
if(thisread == 0)
break;
m_input_ps_len += thisread;
InputPsData(m_pDemux,(char *)buffer,thisread);
if(thisread < buflen)
break;
}
ReleasePsDeMuxer(m_pDemux);
ReleasePsMuxer(m_pMuxer);
fclose(m_input_ps);
fclose(m_output_es);
fclose(m_output_ps);
delete []buffer;buffer = NULL;
}
//printf("m_input_ps_len=%d,=%d,=%d\r\n",m_input_ps_len,m_output_es_len,m_output_ps_len);
Sleep(2000);
system("pause");
return 0;
}