<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=utf-8">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Hello,<br>
    <br>
    For a project I need to be able to read, and write a video using
    libav.<br>
    <br>
    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. <br>
    <br>
    When I arrive at <i>AVPacket *pkt; </i>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 ?<br>
    <br>
    void Video::write(string nameVideo)<br>
    {<br>
      AVOutputFormat *oformat = av_guess_format(NULL, nameVideo.c_str(),
    NULL);<br>
        if (oformat == NULL)<br>
        {<br>
            oformat = av_guess_format("mp4", NULL, NULL);<br>
        }<br>
    <br>
        AVCodecContext *m_codecContext;<br>
        AVFormatContext *m_formatCtx;<br>
    <br>
        m_codecContext = avcodec_alloc_context3(codec);<br>
        m_codecContext->codec_id = oformat->video_codec;<br>
        m_codecContext->codec_type = AVMEDIA_TYPE_VIDEO;<br>
        m_codecContext->gop_size = 30;<br>
        m_codecContext->bit_rate = width * height * 4;<br>
        m_codecContext->width = width;<br>
        m_codecContext->height = height;<br>
        m_codecContext->time_base = (AVRational)frameRate;<br>
        m_codecContext->max_b_frames = max_b_frames;<br>
        m_codecContext->pix_fmt = AV_PIX_FMT_YUV420P;<br>
    <br>
        m_formatCtx = avformat_alloc_context();<br>
        m_formatCtx->oformat = oformat;<br>
        m_formatCtx->video_codec_id = oformat->video_codec;<br>
    <br>
        snprintf(m_formatCtx->filename,
    sizeof(m_formatCtx->filename), "%s", nameVideo.c_str());<br>
    <br>
        AVStream *videoStream2 = avformat_new_stream(m_formatCtx,
    codec);<br>
    <br>
        avcodec_open2(m_codecContext, m_codecContext->codec,NULL);<br>
        avio_open(&m_formatCtx->pb, nameVideo.c_str(),
    AVIO_FLAG_WRITE);<br>
        avformat_write_header(m_formatCtx,NULL);<br>
    <br>
        int ret=0;<br>
    <br>
        AVPacket *pkt;<br>
        int got_packet = 0;<br>
    <br>
        ret = avcodec_send_frame(codecCtx, frames->at(1));<br>
        if (ret < 0)<br>
        {<br>
            throw "Couldn't send frame. Problem";<br>
        }<br>
    <br>
        ret = avcodec_receive_packet(codecCtx, pkt);<br>
        if (!ret)<br>
        {<br>
            got_packet = 1;<br>
        }<br>
    <br>
        if (ret == AVERROR(EAGAIN))<br>
        {<br>
            return 0;<br>
        }<br>
    <br>
        return ret;<br>
    }<br>
  </body>
</html>