用ffmpeg先解码视频文件,再将解码后的AFream数据调整分辨率后重新编码(为了压缩视频),输入输出都是在内存中,编码后视频无法播放,编码参数应该怎么调整? 5C

大神们求帮忙看看,
实在无解了,我试着修改编码的上下文参数,也没找到原因

AVFrame *pFrame = NULL, *pFrameYUV = NULL;
pFrame = av_frame_alloc();
pFrameYUV = av_frame_alloc();

uint8_t *out_buffer;
out_buffer = new uint8_t[avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)];
avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, m_codecEnCtx->width, m_codecEnCtx->height);
pFrameYUV->format = AV_PIX_FMT_YUV420P;
pFrameYUV->width = m_codecEnCtx->width;
pFrameYUV->height = m_codecEnCtx->height;

AVPacket *depacket = (AVPacket*)av_malloc(sizeof(AVPacket)); //存储AVFrame解码后生成的数据
AVPacket *enpacket = (AVPacket*)av_malloc(sizeof(AVPacket));; //存储AVFrame编码后生成的数据
int nSizeAfterDecode = m_codecDeCtx->width * m_codecDeCtx->height;
int nSizeAfterEncode = m_codecEnCtx->width * m_codecEnCtx->height;
av_new_packet(depacket, nSizeAfterDecode);
av_new_packet(enpacket, nSizeAfterEncode);

int ret, got_picture;
struct SwsContext *img_convert_ctx;
int pts = 0;
while (av_read_frame(m_pInFormatCtx, depacket) >= 0)
{
    if (depacket->stream_index == nVideoIndex)
    {
        ret = avcodec_decode_video2(m_codecDeCtx, pFrame, &got_picture, depacket); //解码
        if (ret < 0)
        {
            printf("avcodec_decode_video2 error\n.");
        }
        if (got_picture) //解码成功后,先filter,然后encode
        {
            //分辨率转化
            img_convert_ctx = sws_getContext(m_codecDeCtx->width, m_codecDeCtx->height, ConvertDeprecatedFormat(m_codecDeCtx->pix_fmt), m_codecEnCtx->width, m_codecEnCtx->height, m_codecEnCtx->pix_fmt, SWS_FAST_BILINEAR, NULL, NULL, NULL);//SWS_BICUBIC
            sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pFrame->height, pFrameYUV->data, pFrameYUV->linesize);
            if (img_convert_ctx)
            {
                sws_freeContext(img_convert_ctx);
            }
            else
            {
                printf("sws_getContext failed \n");
            }

            /* ------开始编码YUV数据----*/
            //通过AVFrame数据进行编码

            pFrameYUV->pts = pts++;
            ret = avcodec_encode_video2(m_codecEnCtx, enpacket, pFrameYUV, &got_picture);
            if (ret < 0)
            {
                printf("avcodec_encode_video2 failed. \n");
            }
            if (got_picture)
            {
                enpacket->stream_index = m_streamEncode->index;
                ret = av_write_frame(m_pOutFormatCtx, enpacket);
                if (ret < 0)
                {
                    printf("Write frame error \n");
                }
                av_free_packet(enpacket);
            }   
        }
    }
}
av_packet_free(&depacket);
av_packet_free(&enpacket);
av_write_trailer(m_pOutFormatCtx); //写入数据流尾部到输出
av_frame_free(&pFrameYUV);
av_frame_free(&pFrame);
c++

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问