织梦CMS - 轻松建站从此开始!

罗索

ffmpeg实现转码一个普通视频文件为视频mpeg4,音频mp3的功能的程

jackyhwei 发布于 2010-09-21 14:46 点击:次 
本程序实现转码一个普通视频文件为视频mpeg4,音频mp3的功能
TAG:

本程序实现转码一个普通视频文件为视频mpeg4,音频mp3的功能

  1. #include <avcodec.h> 
  2. #include <avformat.h> 
  3. #include <stdio.h> 
  4. #include <avutil.h> 
  5. #include <stdio.h> 
  6. #include <stdlib.h> 
  7. #include <string.h> 
  8.  
  9. main(int argc,char **argv) 
  10. {    
  11.   const char *input_file_name="/root/movies/ddh1.mpg"
  12.   av_register_all();//注册库中所有可用的文件格式和编码器 
  13.   AVFormatContext *ic; 
  14.   //输入文件处理部分 
  15.   ic=av_alloc_format_context(); 
  16.   if(av_open_input_file(&ic,input_file_name,NULL,0,NULL)!=0) 
  17.   { 
  18.      printf("can't open the file %s\n",input_file_name); 
  19.      exit(1); 
  20.   }//打开输入文件 
  21.   if(av_find_stream_info(ic)<0) 
  22.   { 
  23.      printf("can't find suitable codec parameters\n"); 
  24.      exit(1); 
  25.   }//取出流信息 
  26.   dump_format(ic,0,input_file_name,0);//列出输入文件的相关流信息 
  27.   int i; 
  28.   int videoindex=-1;int audioindex=-1; 
  29.   for(i=0;i<ic->nb_streams;i++) 
  30.   {     
  31. if(ic->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) 
  32. videoindex=i; 
  33. else if(ic->streams[i]->codec->codec_type==CODEC_TYPE_AUDIO) 
  34. {
  35. audioindex=i; 
  36.   } 
  37.    if(videoindex==-1) 
  38.    { 
  39.           printf("can't find video stream\n"); 
  40.           exit(1); 
  41.    }//没有找到视频流 
  42.   AVCodecContext *vCodecCtx; 
  43.   vCodecCtx=ic->streams[videoindex]->codec;//取得视频流编码上下文指针 
  44.   AVCodec *vCodec; 
  45.   vCodec=avcodec_find_decoder(vCodecCtx->codec_id); 
  46.   if(vCodec==NULL) 
  47.   { 
  48.      printf("can't find suitable video decoder\n"); 
  49.      exit(1); 
  50.   }//找到合适的视频解码器 
  51.   if(avcodec_open(vCodecCtx,vCodec)<0) 
  52.   { 
  53.      printf("can't open the video decoder\n"); 
  54.      exit(1); 
  55.   }//打开该视频解码器 
  56.    if(audioindex==-1) 
  57.      { 
  58.         printf("can't find audio stream\n"); 
  59.         exit(1); 
  60.      }//没有找到音频流 
  61.   AVCodecContext *aCodecCtx; 
  62.   aCodecCtx=ic->streams[audioindex]->codec; 
  63.   AVCodec *aCodec; 
  64.   aCodec=avcodec_find_decoder(aCodecCtx->codec_id); 
  65.   if(aCodec==NULL) 
  66.   { 
  67.      printf("can't find suitable audio decoder\n"); 
  68.      exit(1); 
  69.   }//找到合适的音频解码器 
  70.   if(avcodec_open(aCodecCtx,aCodec)<0) 
  71.   { 
  72.      printf("can't open the audio decoder\n"); 
  73.      exit(1); 
  74.   }//打开该音频解码器 
  75. //下面为输出文件处理部分 
  76.     const char *output_file_name="/root/123.avi"
  77.     AVOutputFormat *fmt; 
  78.     AVFormatContext *oc; 
  79.     AVCodecContext *oVcc,*oAcc; 
  80.     AVCodec *oVc,*oAc; 
  81.     AVStream *video_st,*audio_st; 
  82.     AVFrame *oVFrame,*oAFrame; 
  83.     double video_pts; 
  84.     oVFrame=avcodec_alloc_frame(); 
  85.     fmt=guess_format(NULL,output_file_name,NULL); 
  86.     if(!fmt) 
  87.     { 
  88. printf("could not deduce output format from outfile extension\n"); 
  89.            exit(0); 
  90.     }//判断是否可以判断输出文件的编码格式 
  91.     oc=av_alloc_format_context(); 
  92.     if(!oc) 
  93.     { 
  94. printf("Memory error\n"); 
  95.            exit(0); 
  96.     } 
  97.     oc->oformat=fmt; 
  98.     pstrcpy(oc->filename,sizeof(oc->filename),output_file_name); 
  99.     video_st=av_new_stream(oc,0); 
  100.     if(!video_st) 
  101.     { 
  102. printf("could not alloc video stream\n"); 
  103.           exit(0); 
  104.     } 
  105.     oVcc=avcodec_alloc_context(); 
  106.     oVcc=video_st->codec; 
  107.     oVcc->codec_id=CODEC_ID_MPEG4; 
  108.     oVcc->codec_type=CODEC_TYPE_VIDEO; 
  109.     oVcc->bit_rate=2500000; 
  110.     oVcc->width=704; 
  111.     oVcc->height=480; 
  112.     oVcc->time_base=vCodecCtx->time_base; 
  113.     oVcc->gop_size=vCodecCtx->gop_size; 
  114.     //oVcc->pix_fmt=vCodecCtx->pix_fmt; 
  115.     oVcc->pix_fmt=vCodecCtx->pix_fmt; 
  116.     oVcc->max_b_frames=vCodecCtx->max_b_frames; 
  117.     video_st->r_frame_rate=ic->streams[videoindex]->r_frame_rate; 
  118.     audio_st=av_new_stream(oc,oc->nb_streams); 
  119.     if(!audio_st) 
  120.     { 
  121. printf("could not alloc audio stream\n"); 
  122.            exit(0); 
  123.     }   
  124.     avcodec_get_context_defaults2(audio_st->codec,CODEC_TYPE_AUDIO); 
  125.     oAcc=avcodec_alloc_context(); 
  126.     oAcc=audio_st->codec; 
  127.     oAcc->codec_id=CODEC_ID_MP3; 
  128.     oAcc->codec_type=CODEC_TYPE_AUDIO; 
  129.     oAcc->bit_rate=aCodecCtx->bit_rate; 
  130.     oAcc->sample_rate=aCodecCtx->sample_rate; 
  131.     oAcc->channels=2; 
  132.     if (av_set_parameters(oc, NULL) < 0)  
  133.     { 
  134. printf( "Invalid output format parameters\n");                          
  135.               exit(0);                                
  136.     }//设置必要的输出参数 
  137.     strcpy(oc->title,ic->title); 
  138.     strcpy(oc->author,ic->author); 
  139.     strcpy(oc->copyright,ic->copyright); 
  140.     strcpy(oc->comment,ic->comment); 
  141.     strcpy(oc->album,ic->album); 
  142.     oc->year=ic->year; 
  143.     oc->track=ic->track; 
  144.     strcpy(oc->genre,ic->genre); 
  145.     dump_format(oc,0,output_file_name,1);//列出输出文件的相关流信息 
  146.     oVc=avcodec_find_encoder(CODEC_ID_MPEG4); 
  147.     if(!oVc) 
  148.     { 
  149. printf("can't find suitable video encoder\n"); 
  150.            exit(0); 
  151.     }//找到合适的视频编码器 
  152.     if(avcodec_open(oVcc,oVc)<0) 
  153.     { 
  154. printf("can't open the output video codec\n"); 
  155.            exit(0); 
  156.     }//打开视频编码器 
  157.     oAc=avcodec_find_encoder(CODEC_ID_MP3); 
  158.     if(!oAc) 
  159.     { 
  160. printf("can't find suitable audio encoder\n"); 
  161.            exit(0); 
  162.     }//找到合适的音频编码器 
  163.     if(avcodec_open(oAcc,oAc)<0) 
  164.     { 
  165. printf("can't open the output audio codec"); 
  166.            exit(0); 
  167.     }//打开音频编码器 
  168.     /*if(url_exist(output_file_name)) 
  169.     { 
  170. printf("file %s exist,please select other\n",output_file_name); 
  171.        exit(0); 
  172.     }//判断该输出文件是否已经存在*/ 
  173.     if (!(oc->flags & AVFMT_NOFILE)) 
  174.     { 
  175. if(url_fopen(&oc->pb,output_file_name,URL_WRONLY)<0) 
  176.        { 
  177. printf("can't open the output file %s\n",output_file_name); 
  178.               exit(0); 
  179.        }//打开输出文件 
  180.     } 
  181.     if(!oc->nb_streams) 
  182.     { 
  183. fprintf(stderr,"output file dose not contain any stream\n"); 
  184.            exit(0); 
  185.     }//查看输出文件是否含有流信息 
  186.   if(av_write_header(oc)<0) 
  187.   { 
  188. fprintf(stderr, "Could not write header for output file\n"); 
  189.       exit(1); 
  190.   }
  191. AVPacket packet; 
  192.   uint8_t *ptr,*out_buf; 
  193.   int out_size; 
  194.   static short *samples=NULL; 
  195.   static unsigned int samples_size=0; 
  196.   uint8_t *video_outbuf,*audio_outbuf;
  197. int video_outbuf_size,audio_outbuf_size; 
  198.   video_outbuf_size=400000; 
  199.   video_outbuf= (unsigned char *) malloc(video_outbuf_size); 
  200.   audio_outbuf_size = 10000; 
  201.   audio_outbuf = av_malloc(audio_outbuf_size); 
  202.   int flag;int frameFinished;int len;int frame_index=0,ret; 
  203. while(av_read_frame(ic,&packet)>=0)//从输入文件中读取一个包 
  204. if(packet.stream_index==videoindex)//判断是否为当前视频流中的包 
  205. len=avcodec_decode_video(vCodecCtx,oVFrame,
  206. &frameFinished,packet.data,packet.size);//若为视频包,解码该视频包 
  207. if(len<0) 
  208. printf("Error while decoding\n"); 
  209. exit(0); 
  210. if(frameFinished)//判断视频祯是否读完 
  211. fflush(stdout); 
  212. oVFrame->pts=av_rescale(frame_index,
  213. AV_TIME_BASE*(int64_t)oVcc->time_base.num,
  214. oVcc->time_base.den); 
  215. oVFrame->pict_type=0; 
  216. out_size = avcodec_encode_video(oVcc, video_outbuf,
  217.  video_outbuf_size, oVFrame);    
  218. if (out_size > 0)             
  219. {                    
  220. AVPacket pkt;                
  221. av_init_packet(&pkt);                                
  222. if (秩名)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201009/10193.html]
本文出处:网络 作者:秩名
顶一下
(1)
33.3%
踩一下
(2)
66.7%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容