[Libav-user] Problem with codec->encode2

Pierre Delmas pierre.delmas at centrale-marseille.fr
Sat Apr 15 19:00:01 EEST 2017


For a project I need to be able to read, and write a video using libav.

So I looked for examples on Internet to do that and I found one about
writing video and I trying to implement it into my own code (available
at the end of mail) but now I have a problem.

When I arrive at /AVPacket *pkt; /the system says "dimensions not set" I
can't find why. So is there a better solution to write than the one I
tried ? If yes do you have some examples ?

void Video::write(string nameVideo)
  AVOutputFormat *oformat = av_guess_format(NULL, nameVideo.c_str(), NULL);
    if (oformat == NULL)
        oformat = av_guess_format("mp4", NULL, NULL);

    AVCodecContext *m_codecContext;
    AVFormatContext *m_formatCtx;

    m_codecContext = avcodec_alloc_context3(codec);
    m_codecContext->codec_id = oformat->video_codec;
    m_codecContext->codec_type = AVMEDIA_TYPE_VIDEO;
    m_codecContext->gop_size = 30;
    m_codecContext->bit_rate = width * height * 4;
    m_codecContext->width = width;
    m_codecContext->height = height;
    m_codecContext->time_base = (AVRational)frameRate;
    m_codecContext->max_b_frames = max_b_frames;
    m_codecContext->pix_fmt = AV_PIX_FMT_YUV420P;

    m_formatCtx = avformat_alloc_context();
    m_formatCtx->oformat = oformat;
    m_formatCtx->video_codec_id = oformat->video_codec;

    snprintf(m_formatCtx->filename, sizeof(m_formatCtx->filename), "%s",

    AVStream *videoStream2 = avformat_new_stream(m_formatCtx, codec);

    avcodec_open2(m_codecContext, m_codecContext->codec,NULL);
    avio_open(&m_formatCtx->pb, nameVideo.c_str(), AVIO_FLAG_WRITE);

    int ret=0;

    AVPacket *pkt;
    int got_packet = 0;

    ret = avcodec_send_frame(codecCtx, frames->at(1));
    if (ret < 0)
        throw "Couldn't send frame. Problem";

    ret = avcodec_receive_packet(codecCtx, pkt);
    if (!ret)
        got_packet = 1;

    if (ret == AVERROR(EAGAIN))
        return 0;

    return ret;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20170415/75f795ca/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20170415/75f795ca/attachment.sig>

More information about the Libav-user mailing list