ffmpeg avcodec_encode_video2 前面10多帧不能实时编码

本文针对FFmpeg v1.2.1版本中avcodec_encode_video2前几帧无法实时编码的问题进行了解析,并通过调整编码速度及实时性设置解决了延迟问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ffmpeg avcodec_encode_video2 前面10多帧不能实时编码,查找原因!版本v1.2.1

 

原因找到:需要设置编码的速度和是否实时编码;见红色的部分!

 

注意:

 #include <libavutil/opt.h>

 

/* Add an output stream. */
static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec,
       enum AVCodecID codec_id)
{
 AVCodecContext *c;
 AVStream *st;

 /* find the encoder */
 *codec = avcodec_find_encoder(codec_id);
 if (!(*codec)) {
  fprintf(stderr, "Could not find encoder for '%s'\n",
   avcodec_get_name(codec_id));
  exit(1);
 }

 st = avformat_new_stream(oc, *codec);
 if (!st) {
  fprintf(stderr, "Could not allocate stream\n");
  exit(1);
 }
 st->id = oc->nb_streams-1;
 c = st->codec;

 switch ((*codec)->type) {
 case AVMEDIA_TYPE_AUDIO:
  st->id = 1;
  c->sample_fmt  = AV_SAMPLE_FMT_S16;
  c->bit_rate    = 64000;
  c->sample_rate = 44100;
  c->channels    = 2;
  break;

 case AVMEDIA_TYPE_VIDEO:
  c->codec_id = codec_id;

  c->bit_rate = 400000;
  /* Resolution must be a multiple of two. */
  c->width    = 352;
  c->height   = 288;
  /* timebase: This is the fundamental unit of time (in seconds) in terms
  * of which frame timestamps are represented. For fixed-fps content,
  * timebase should be 1/framerate and timestamp increments should be
  * identical to 1. */
  c->time_base.den = STREAM_FRAME_RATE;
  c->time_base.num = 1;
  c->gop_size      = 12; /* emit one intra frame every twelve frames at most */
  c->pix_fmt       = STREAM_PIX_FMT;
  if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
   /* just for testing, we also add B frames */
   c->max_b_frames = 2;
  }
  if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
   /* Needed to avoid using macroblocks in which some coeffs overflow.
   * This does not happen with normal video, it just happens here as
   * the motion of the chroma plane does not match the luma plane. */
   c->mb_decision = 2;
  }

  //防止编码延迟修改参数
  av_opt_set(c->priv_data, "preset", "superfast", 0); 
  av_opt_set(c->priv_data, "tune", "zerolatency", 0);


  break;

 default:
  break;
 }

 /* Some formats want stream headers to be separate. */
 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
  c->flags |= CODEC_FLAG_GLOBAL_HEADER;

 return st;
}

 

修改后的例子见:

http://blog.youkuaiyun.com/smilestone_322/article/details/17002575

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值