PyAV icon indicating copy to clipboard operation
PyAV copied to clipboard

How to convert the packet's format from avcc to annex-b?

Open bazige opened this issue 3 years ago • 0 comments

I want to convert the video frame of avcc to annex-b, but currently pyav does not encapsulate a similar interface. I have implemented avcc2annex through c++, but I am not sure if AVFormatContext *pFormatCtx_ is different from pyav's class *av.codec.context.CodecContext, can you help me? Or is there any better suggestion other than calling the ffmpeg command line?

const AVBitStreamFilter *pFilter = av_bsf_get_by_name("h264_mp4toannexb");

int avcc2annex(AVFormatContext *pFormatCtx_, AVPacket *pkt, int videoStream) 
{
    AVBSFContext *pBsfCtx_ = nullptr; 
    int ret = av_bsf_alloc(pFilter, &pBsfCtx_);
    if (ret != 0) {
        LOG("Alloc bsf failed!");
        return -1;
    }

    ret = avcodec_parameters_from_context(pBsfCtx_->par_in, pFormatCtx_->streams[videoStream]->codec);
    if (ret < 0) {
        LOG("Set Codec failed!");
        return -1;
    }

    ret = av_bsf_init(pBsfCtx_);
    if (ret < 0) {
        LOG("Init bsf failed!");
        return -1;
    }

    av_bsf_send_packet(pBsfCtx_, pkt);
    ret = av_bsf_receive_packet(pBsfCtx_, pkt);
    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
        return -1;
    else if (ret < 0) {
        LOG("Receive packet failed!");
        return -1;
    }
    return 0;
}

bazige avatar Jul 19 '22 19:07 bazige