Invalid data found when processing input

ffmpeg——Invalid data found when processing input

一、问题描述

流程描述:我把每一帧原始YUV图像数据,构造成Y4M(YUV4MPEG2)格式的流,然后使用ffmpeg编码成h264格式进行RTMP推流。
ffmpeg执行avformat_open_input打开文件和执行avformat_find_stream_info探测流都正常,但在执行avformat_write_header报错:Invalid data found when processing input

二、原因

编码器参数设置错误

// enc_ctx编码器上下文,dec_ctx解码器上下文
AVCodecContext *dec_ctx, *enc_ctx;
...
dec_ctx = pFormatCtx->streams[i]->codec;
...
if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
    LOGD_DEBUG("height = %d, width = %d, sample_aspect_ratio = %d/%d, pix_fmt = %d, framerate = %d/%d, time_base = %d/%d",
               dec_ctx->height, dec_ctx->width,
               dec_ctx->sample_aspect_ratio.num,
               dec_ctx->sample_aspect_ratio.den,
               dec_ctx->pix_fmt,
               dec_ctx->framerate.num, dec_ctx->framerate.den,
               dec_ctx->time_base.num, dec_ctx->time_base.den);
    enc_ctx->height = dec_ctx->height;
    enc_ctx->width = dec_ctx->width;
    enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;
    /* take first format from list of supported formats */
    if (encoder->pix_fmts)
        enc_ctx->pix_fmt = encoder->pix_fmts[0];
    else
        enc_ctx->pix_fmt = dec_ctx->pix_fmt;
    /* video time_base can be set to whatever is handy and supported by encoder */
    enc_ctx->time_base = dec_ctx->framerate;
    enc_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
}

我这里的编码器参数是根据解码器里参数进行设置的,但解码器取得的部分参数时不准确的,推测原因可能和我构造的Y4M(YUV4MPEG2)流格式有关。

三、解决方案

将上面的代码修改为:

// enc_ctx编码器上下文,dec_ctx解码器上下文
AVCodecContext *dec_ctx, *enc_ctx;
...
dec_ctx = pFormatCtx->streams[i]->codec;
...
if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
    LOGD_DEBUG("height = %d, width = %d, sample_aspect_ratio = %d/%d, pix_fmt = %d, framerate = %d/%d, time_base = %d/%d",
               dec_ctx->height, dec_ctx->width,
               dec_ctx->sample_aspect_ratio.num,
               dec_ctx->sample_aspect_ratio.den,
               dec_ctx->pix_fmt,
               dec_ctx->framerate.num, dec_ctx->framerate.den,
               dec_ctx->time_base.num, dec_ctx->time_base.den);
    enc_ctx->height = dec_ctx->height;
    enc_ctx->width = dec_ctx->width;
    enc_ctx->sample_aspect_ratio = {476, 477};//dec_ctx->sample_aspect_ratio;
    /* take first format from list of supported formats */
    if (encoder->pix_fmts)
        enc_ctx->pix_fmt = encoder->pix_fmts[0];
    else
        enc_ctx->pix_fmt = dec_ctx->pix_fmt;
    /* video time_base can be set to whatever is handy and supported by encoder */
    enc_ctx->time_base = av_inv_q({25, 1});//dec_ctx->framerate);
    enc_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
}
  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值