FFmpeg浅尝辄止(二)——YUV视频序列编码为视频

转自:http://blog.csdn.net/yang_xian521/article/details/7698742#

上一篇已经写了如何配置好开发环境,这次就先小试牛刀,来个视频的编码。搞视频处理的朋友肯定比较熟悉YUV视频序列,很多测试库提供的视频数据都是YUV视频序列,我们这里就用用YUV视频序列来做视频。关于YUV视频序列,我就不多讲了,可以看书学习,通常的视频序列都是YUV420格式的。

步骤也就那几步,添加视频流,打开编码器,开辟相应的内存空间,然后就可以打开YUV序列逐帧写入数据了,so easy!记得最后要做好文件的关闭和内存的释放,因为FFmpeg是c风格的(不知道新版本是否是c++风格的),这些工作都需要自己做好啊。过多的说明是没用的,直接上代码:

这里我补充一下,大多数的视频格式好像只支持YUV格式的视频帧AVFrame,我试图直接把RGB的视频序列直接编码到视频这条路好像走不通,都需要把RGB的视频帧再转成YUV视频帧才行,不知道高手有没有其他高见。

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <string.h>  
  3.   
  4. extern "C"  
  5. {  
  6. #include <libavcodec\avcodec.h>  
  7. #include <libavformat\avformat.h>  
  8. #include <libswscale\swscale.h>  
  9. };  
  10.   
  11. void main(int argc, char ** argv)  
  12. {  
  13.     AVFormatContext* oc;  
  14.     AVOutputFormat* fmt;  
  15.     AVStream* video_st;  
  16.     double video_pts;  
  17.     uint8_t* video_outbuf;  
  18.     uint8_t* picture_buf;  
  19.     AVFrame* picture;  
  20. //  AVFrame* pictureRGB;  
  21.     int size;  
  22.     int ret;  
  23.     int video_outbuf_size;  
  24.   
  25.     FILE *fin = fopen("akiyo_qcif.yuv""rb"); //视频源文件   
  26.   
  27.     const char* filename = "test.mpg";  
  28. //  const char* filename;  
  29. //  filename = argv[1];  
  30.   
  31.     av_register_all();  
  32.   
  33. //  avcodec_init(); // 初始化codec库  
  34. //  avcodec_register_all(); // 注册编码器  
  35.   
  36.     fmt = guess_format(NULL, filename, NULL);  
  37.     oc = av_alloc_format_context();  
  38.     oc->oformat = fmt;  
  39.     snprintf(oc->filename, sizeof(oc->filename), "%s", filename);  
  40.   
  41.     video_st = NULL;  
  42.     if (fmt->video_codec != CODEC_ID_NONE)  
  43.     {  
  44.         AVCodecContext* c;  
  45.         video_st = av_new_stream(oc, 0);  
  46.         c = video_st->codec;  
  47.         c->codec_id = fmt->video_codec;  
  48.         c->codec_type = CODEC_TYPE_VIDEO;  
  49.         c->bit_rate = 400000;  
  50.         c->width = 176;  
  51.         c->height = 144;  
  52.         c->time_base.num = 1;  
  53.         c->time_base.den = 25;   
  54.         c->gop_size = 12;  
  55.         c->pix_fmt = PIX_FMT_YUV420P;  
  56.         if (c->codec_id == CODEC_ID_MPEG2VIDEO)  
  57.         {  
  58.             c->max_b_frames = 2;  
  59.         }  
  60.         if (c->codec_id == CODEC_ID_MPEG1VIDEO)  
  61.         {  
  62.             c->mb_decision = 2;  
  63.         }  
  64.         if (!strcmp(oc->oformat->name, "mp4") || !strcmp(oc->oformat->name, "mov") || !strcmp(oc->oformat->name, "3gp"))  
  65.         {  
  66.             c->flags |= CODEC_FLAG_GLOBAL_HEADER;  
  67.         }  
  68.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值