使用FFmpeg将yuv420p编码为h264

代码对一些数据没做判断,仅仅是做个备忘!请谨慎参考!

#include <libavcodec/avcodec.h>
#include <libavutil/imgutils.h>
#include <stdio.h>

void encode_to_h264(AVCodecContext *codec_ctx, AVFrame *frame, AVPacket *pkt,
                    FILE *file_out) {
  int ret;
  ret = avcodec_send_frame(codec_ctx, frame);
  while (!ret) {
    ret = avcodec_receive_packet(codec_ctx, pkt);
    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
      printf("AVERROR(EAGAIN) or AVERROR_EOF\n");
      return;
    } else if (ret < 0) {
      printf("error 8\n");
      exit(1);
    }
    printf("ENCODE SUCCESS\n");
    fwrite(pkt->data, 1, pkt->size, file_out);
    av_packet_unref(pkt);
  }
}

int main(int argc, char **argv) {
  AVCodecContext *codec_ctx;
  AVCodec *codec;
  FILE *file_in;
  FILE *file_out;
  uint8_t *in_buf;
  size_t in_size;
  char *in_yuv;
  char *out_h264;
  AVFrame *frame;
  AVPacket *pkt;
  int ret;
  if (argc != 3) {
    printf("error 0\n");
    exit(1);
  }
  in_yuv = argv[1];
  out_h264 = argv[2];
  file_in = fopen(in_yuv, "rb");
  file_out = fopen(out_h264, "wb");
  codec = avcodec_find_encoder(AV_CODEC_ID_H264);
  if (!codec) {
    printf("error 1\n");
    exit(1);
  }
  codec_ctx = avcodec_alloc_context3(codec);
  if (!codec_ctx) {
    printf("error 2\n");
    exit(1);
  }
  codec_ctx->width = 320;
  codec_ctx->height = 240;
  codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
  codec_ctx->gop_size = 12;
  codec_ctx->max_b_frames = 2;
  codec_ctx->framerate = (AVRational){25, 1};
  codec_ctx->time_base = (AVRational){1, 25};
  codec_ctx->bit_rate = 516 * 1024;
  ret = avcodec_open2(codec_ctx, codec, NULL);
  if (ret) {
    printf("error 5\n");
    exit(1);
  }
  in_size = av_image_get_buffer_size(codec_ctx->pix_fmt, codec_ctx->width,
                                     codec_ctx->height, 1);
  in_buf = av_malloc(in_size);
  frame = av_frame_alloc();
  frame->width = codec_ctx->width;
  frame->height = codec_ctx->height;
  frame->format = codec_ctx->pix_fmt;
  ret = av_frame_get_buffer(frame, 0);
  if (ret) {
    printf("error 3\n");
    exit(1);
  }
  pkt = av_packet_alloc();
  av_init_packet(pkt);
  int pts = 0;
  while (fread(in_buf, 1, in_size, file_in) == in_size) {
    ret = av_image_fill_arrays(frame->data, frame->linesize, in_buf,
                               codec_ctx->pix_fmt, codec_ctx->width,
                               codec_ctx->height, 1);
    if (ret < 0) {
      printf("error 4\n");
      exit(1);
    }
    frame->width = codec_ctx->width;
    frame->height = codec_ctx->height;
    frame->format = codec_ctx->pix_fmt;
    frame->pts = pts++;
    encode_to_h264(codec_ctx, frame, pkt, file_out);
    av_frame_unref(frame);
  }
  encode_to_h264(codec_ctx, NULL, pkt, file_out);
end:
  fclose(file_in);
  fclose(file_out);
  av_frame_free(&frame);
  av_packet_free(&pkt);
  avcodec_free_context(&codec_ctx);
  av_freep(&in_buf);
  printf("Finished!\n");
  return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值