使用ffmpeg获取视频流后如何封装存储成mp4文件

perl 发布于 2012/07/03 20:03
阅读 9K+
收藏 1

int main(int argc,char *argv[])
{
 AVFormatContext *pFormatCtx;
 int i,videoStream;
 AVCodecContext *pCodecCtx;
 AVPacket packet;
 //H264Context *h;
 int type=0; 
 //Open video file
 char rtsp[100]="";
 char filename[150]="";
 char *p=NULL;
 char camid[6]="";
 char log[100]="";
 char new_file[200]="";
 char command[200]="";
 char file_jpg[200]="";

 if(argc!=3)
 {
  exit(1);
 }
 
 strcpy(rtsp,argv[1]);
 strcpy(filename,argv[2]);
 p=strstr(filename,"cam");
 memcpy(camid,p,5);

 signal(SIGTERM,recv_signal);
 signal(SIGUSR1,recv_signal);
 signal(SIGHUP,recv_signal);
 signal(SIGSEGV,recv_signal);
 strcpy(File_Name,filename);

 //Register all formats and codecs
 av_register_all();
 avformat_network_init();

 if(av_open_input_file(&pFormatCtx,rtsp,NULL,0,NULL)!=0)
 {
  sprintf(log,"%s [%s] %s","The ",rtsp," stream open error\n");
  write_log(camid,log);
  exit(1);
 }

 //Retrieve stream information
 if(av_find_stream_info(pFormatCtx)<0)
 {
  sprintf(log,"%s","Video Flow information check error\n");
  write_log(camid,log);
  return -1;
 }

 //Dump information about file onto standard error
 av_dump_format(pFormatCtx,0,rtsp,0);

 //Find the first video stream
 videoStream=-1;
 for(i=0;i<pFormatCtx->nb_streams;i++)
 {
  if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
  {
   videoStream=i;
   break;
  }
 }

 if(videoStream==-1)
 {
  sprintf(log,"%s","Didn't find a video stram\n");
  write_log(camid,log);
  return -1;
 }

 pCodecCtx=pFormatCtx->streams[videoStream]->codec;
 //open file
 FILE *fp=NULL;
 //char *filename="/home/MEDIA/project/linux/test.mp4";

 if((fp=fopen(filename,"wb"))==NULL)
 {
  sprintf(log,"%s%s",filename," open failed\n");
  write_log(camid,log);
  exit(1);
 }
/*这里打开了一个test.mp4的文件,下面怎么封装存储就不知道了,请各位大哥指导!帮助指点下封装成mp4文件的主要函数部分*/

加载中
返回顶部
顶部