Chinaunix首页 | 论坛 | 博客
  • 博客访问: 959550
  • 博文数量: 77
  • 博客积分: 821
  • 博客等级: 军士长
  • 技术积分: 1905
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-23 16:17
个人简介

学校:上海交通大学软件工程 学历:硕士 行业:从事流媒体移动开发 QQ: 412595942 邮箱:yiikai1987910@gmail.com

文章分类

全部博文(77)

文章存档

2016年(4)

2015年(15)

2014年(16)

2013年(12)

2012年(21)

2011年(9)

分类: C/C++

2014-10-30 22:33:55

     前一篇介绍过用x264编码文件,ffmpeg内部也是用x264编码,这里记录下简单的用ffmpeg的编码
    直接上代码:

点击(此处)折叠或打开

  1. onst char* SRC_FILE = "1.mp4";
  2. const char* OUT_FILE = "outfile.h264";

  3. typedef struct X264Context {
  4.     AVClass *pclass;
  5.     x264_param_t params;
  6.     x264_t *enc;
  7.     x264_picture_t pic;
  8.     uint8_t *sei;
  9.     int sei_size;
  10.     char *preset;
  11.     char *tune;
  12.     char *profile;
  13.     char *level;
  14.     int fastfirstpass;
  15.     char *wpredp;
  16.     char *x264opts;
  17.     float crf;
  18.     float crf_max;
  19.     int cqp;
  20.     int aq_mode;
  21.     float aq_strength;
  22.     char *psy_rd;
  23.     int psy;
  24.     int rc_lookahead;
  25.     int weightp;
  26.     int weightb;
  27.     int ssim;
  28.     int intra_refresh;
  29.     int bluray_compat;
  30.     int b_bias;
  31.     int b_pyramid;
  32.     int mixed_refs;
  33.     int dct8x8;
  34.     int fast_pskip;
  35.     int aud;
  36.     int mbtree;
  37.     char *deblock;
  38.     float cplxblur;
  39.     char *partitions;
  40.     int direct_pred;
  41.     int slice_max_size;
  42.     char *stats;
  43.     int nal_hrd;
  44.     char *x264_params;
  45. } X264Context;


  46. int main()
  47. {
  48.     FILE *yuv_file = fopen("yuv_file", "ab");
  49.     if (!yuv_file)
  50.         return 0;
  51.     av_register_all();
  52.     AVFormatContext* pFormat = NULL;
  53.     if (avformat_open_input(&pFormat, SRC_FILE, NULL, NULL) < 0)
  54.     {
  55.         return 0;
  56.     }
  57.     AVCodecContext* video_dec_ctx = NULL;
  58.     AVCodec* video_dec = NULL;
  59.     if (avformat_find_stream_info(pFormat, NULL) < 0)
  60.     {
  61.         return 0;
  62.     }
  63.     av_dump_format(pFormat, 0, SRC_FILE, 0);
  64.     video_dec_ctx = pFormat->streams[0]->codec;
  65.     video_dec = avcodec_find_decoder(video_dec_ctx->codec_id);
  66.     if (avcodec_open2(video_dec_ctx, video_dec, NULL) < 0)
  67.     {
  68.         return 0;
  69.     }

  70.     AVFormatContext* pOFormat = NULL;
  71.     AVOutputFormat* ofmt = NULL;
  72.     if (avformat_alloc_output_context2(&pOFormat, NULL, NULL, OUT_FILE) < 0)
  73.     {
  74.         return 0;
  75.     }
  76.     ofmt = pOFormat->oformat;
  77.     if (avio_open(&(pOFormat->pb), OUT_FILE, AVIO_FLAG_READ_WRITE) < 0)
  78.     {
  79.         return 0;
  80.     }
  81.     AVCodecContext *video_enc_ctx = NULL;
  82.     AVCodec *video_enc = NULL;
  83.     video_enc = avcodec_find_encoder(AV_CODEC_ID_H264);
  84.     AVStream *video_st = avformat_new_stream(pOFormat, video_enc);
  85.     if (!video_st)
  86.         return 0;
  87.     video_enc_ctx = video_st->codec;
  88.     video_enc_ctx->profile = FF_PROFILE_H264_HIGH;
  89.     video_enc_ctx->width = video_dec_ctx->width;
  90.     video_enc_ctx->height = video_dec_ctx->height;
  91.     video_enc_ctx->pix_fmt = PIX_FMT_YUV420P;
  92.     video_enc_ctx->time_base.num = 1;
  93.     video_enc_ctx->time_base.den = 25;
  94.     video_enc_ctx->bit_rate = 400000;
  95.     video_enc_ctx->gop_size = 250;
  96.     video_enc_ctx->max_b_frames = 10;
  97.     video_enc_ctx->qmin = 10;
  98.     video_enc_ctx->qmax = 51;
  99.     ((X264Context*)(video_enc_ctx->priv_data))->preset = "superfast";
  100.     ((X264Context*)(video_enc_ctx->priv_data))->tune = "zerolatency";
  101.     if (avcodec_open2(video_enc_ctx, video_enc, NULL) < 0)
  102.     {
  103.         printf("编码器打开失败!\n");
  104.         return 0;
  105.     }


  106.     printf("Output264video Information====================\n");
  107.     av_dump_format(pOFormat, 0, OUT_FILE, 1);
  108.     printf("Output264video Information====================\n");
  109.     avformat_write_header(pOFormat, NULL);
  110.     AVPacket *pkt = new AVPacket();
  111.     av_init_packet(pkt);
  112.     AVFrame *pFrame = avcodec_alloc_frame();
  113.     int ts = 0;
  114.     while (1)
  115.     {
  116.         if (av_read_frame(pFormat, pkt) < 0)
  117.         {
  118.             avio_close(pOFormat->pb);
  119.             av_write_trailer(pOFormat);
  120.             delete pkt;
  121.             return 0;
  122.         }
  123.         if (pkt->stream_index == 0)
  124.         {

  125.             int got_picture = 0, ret = 0;
  126.             ret = avcodec_decode_video2(video_dec_ctx, pFrame, &got_picture, pkt);
  127.             if (ret < 0)
  128.             {
  129.                 delete pkt;
  130.                 return 0;
  131.             }
  132.             pFrame->pts = ts++;
  133.             if (got_picture)
  134.             {
  135.                 AVPacket *tmppkt = new AVPacket;
  136.                 av_init_packet(tmppkt);
  137.                 int size = video_enc_ctx->width*video_enc_ctx->height * 3 / 2;
  138.                 char* buf = new char[size];
  139.                 memset(buf, 0, size);
  140.                 tmppkt->data = (uint8_t*)buf;
  141.                 tmppkt->size = size;
  142.                 ret = avcodec_encode_video2(video_enc_ctx, tmppkt, pFrame, &got_picture);
  143.                 if (ret < 0)
  144.                 {
  145.                     avio_close(pOFormat->pb);
  146.                     delete buf;
  147.                     return 0;
  148.                 }
  149.                 if (got_picture)
  150.                 {
  151.                     printf(".");
  152.                     ret = av_interleaved_write_frame(pOFormat, tmppkt);
  153.                     delete tmppkt;
  154.                     delete buf;
  155.                 }
  156.             }
  157.         }
  158.         
  159.     }
  160.     avcodec_free_frame(&pFrame);
  161.     return 0;
  162. }


阅读(12992) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~