使用 avcodec/H264 编码实时视频时

发布于 2024-12-08 09:08:09 字数 1418 浏览 6 评论 0原文

我想使用 avcodec/H264 对实时视频进行编码。 我尝试了一些设置参数。

    m_pCodec = avcodec_find_encoder(CODEC_ID_H264);
    m_pCodecCtx = avcodec_alloc_context3(m_pCodec);

    m_pCodecCtx->coder_type=1;
    m_pCodecCtx->flags|=CODEC_FLAG_LOOP_FILTER;
    m_pCodecCtx->me_cmp |= FF_CMP_CHROMA;
    m_pCodecCtx->partitions|=X264_PART_I8X8+X264_PART_I4X4+X264_PART_P8X8+X264_PART_B8X8; // partitions=+parti8x8+parti4x4+partp8x8+partb8x8
    m_pCodecCtx->me_method=ME_HEX;
    m_pCodecCtx->me_subpel_quality = 6;
    m_pCodecCtx->me_range=16;
    m_pCodecCtx->gop_size=30;
    m_pCodecCtx->keyint_min=10;
    m_pCodecCtx->scenechange_threshold=40;
    m_pCodecCtx->i_quant_factor=0.71;
    m_pCodecCtx->b_frame_strategy=1;
    m_pCodecCtx->qcompress=0.6;
    m_pCodecCtx->qmin=10;
    m_pCodecCtx->qmax=51;
    m_pCodecCtx->max_qdiff=4;
    m_pCodecCtx->max_b_frames=1;
    m_pCodecCtx->refs=2;
    m_pCodecCtx->directpred=3;
    m_pCodecCtx->trellis=1;
    m_pCodecCtx->flags2|=CODEC_FLAG2_BPYRAMID|CODEC_FLAG2_WPRED|CODEC_FLAG2_8X8DCT|CODEC_FLAG2_FASTPSKIP;// +bpyramid+wpred+dct8x8+fastpskip
    m_pCodecCtx->width = 720;
    m_pCodecCtx->height = 480;
    m_pCodecCtx->time_base.num = 1;
    m_pCodecCtx->time_base.den = 15;
    m_pCodecCtx->pix_fmt = PIX_FMT_YUV420P; 

有用。解码方面很好。 但框架尺寸太大。 I帧和P帧约25万字节,不制作B帧。 怎么了?

谢谢。

I want to encode live video whith avcodec/H264.
I tryed with some setting params.

    m_pCodec = avcodec_find_encoder(CODEC_ID_H264);
    m_pCodecCtx = avcodec_alloc_context3(m_pCodec);

    m_pCodecCtx->coder_type=1;
    m_pCodecCtx->flags|=CODEC_FLAG_LOOP_FILTER;
    m_pCodecCtx->me_cmp |= FF_CMP_CHROMA;
    m_pCodecCtx->partitions|=X264_PART_I8X8+X264_PART_I4X4+X264_PART_P8X8+X264_PART_B8X8; // partitions=+parti8x8+parti4x4+partp8x8+partb8x8
    m_pCodecCtx->me_method=ME_HEX;
    m_pCodecCtx->me_subpel_quality = 6;
    m_pCodecCtx->me_range=16;
    m_pCodecCtx->gop_size=30;
    m_pCodecCtx->keyint_min=10;
    m_pCodecCtx->scenechange_threshold=40;
    m_pCodecCtx->i_quant_factor=0.71;
    m_pCodecCtx->b_frame_strategy=1;
    m_pCodecCtx->qcompress=0.6;
    m_pCodecCtx->qmin=10;
    m_pCodecCtx->qmax=51;
    m_pCodecCtx->max_qdiff=4;
    m_pCodecCtx->max_b_frames=1;
    m_pCodecCtx->refs=2;
    m_pCodecCtx->directpred=3;
    m_pCodecCtx->trellis=1;
    m_pCodecCtx->flags2|=CODEC_FLAG2_BPYRAMID|CODEC_FLAG2_WPRED|CODEC_FLAG2_8X8DCT|CODEC_FLAG2_FASTPSKIP;// +bpyramid+wpred+dct8x8+fastpskip
    m_pCodecCtx->width = 720;
    m_pCodecCtx->height = 480;
    m_pCodecCtx->time_base.num = 1;
    m_pCodecCtx->time_base.den = 15;
    m_pCodecCtx->pix_fmt = PIX_FMT_YUV420P; 

It works. Decoding side is well.
But the frame size is too large.
I-frame and P-frame is about 250,000 bytes, and B-frame is not made.
What's wrong?

thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花肆意 2024-12-15 09:08:09

当我手动设置参数时,我也遇到了问题。当我使用配置文件而不是设置您列出的所有选项时,我的问题得到了解决:

m_pCodecCtx->profile = FF_PROFILE_H264_BASELINE;

之后您可以使用 crf 设置恒定质量,例如:

m_pCodecCtx->crf = 28;

然后像您一样设置选项宽度、高度、time_base 和 pix_fmt 。

希望有帮助!

I had problems with the parameters when I set them manually as well. My problem was solved when I used a profile instead of setting all the options you listed:

m_pCodecCtx->profile = FF_PROFILE_H264_BASELINE;

After that you can set constant quality with crf, e.g.:

m_pCodecCtx->crf = 28;

Then you set the options width, height, time_base and pix_fmt like you did.

Hope it helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文