<div dir="ltr"><div>[Sorry this is a repost, as the code indentation was scrubbed away in the previous post. Please bear with a newbie. ]</div><div><br></div><div>I am have trouble encoding an H264 video correctly using FFmpeg libav. I could not play the encoded video in VLC media player, and although I could play the video on MPC-HC the time shows 00:00/00:00. Clearly I'm missing something.</div><div><br></div><div>The Media info from MPC-HC shows this:</div><div>  </div><div>    General  </div><div>    Format                         : AVC  </div><div>    Format/Info                    : Advanced Video Codec  </div><div>    File size                      : 110 KiB  </div><div>    Duration                       : 2s 400ms  </div><div>    Overall bit rate               : 375 Kbps  </div><div>    Writing library                : x264 core 148 r2665 a01e339  </div><div>    Encoding settings              : cabac=0 / ref=3 / deblock=1:0:0 / analyse=0x1:0x111 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=0 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=7 / lookahead_threads=1 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=0 / weightp=0 / keyint=12 / keyint_min=1 / scenecut=40 / intra_refresh=0 / rc_lookahead=12 / rc=abr / mbtree=1 / bitrate=2000 / ratetol=1.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00  </div><div>      </div><div>    Video  </div><div>    Format                         : AVC  </div><div>    Format/Info                    : Advanced Video Codec  </div><div>    Format profile                 : Baseline@L2.1  </div><div>    Format settings, CABAC         : No  </div><div>    Format settings, ReFrames      : 3 frames  </div><div>    Format settings, GOP           : M=1, N=12  </div><div>    Duration                       : 2s 400ms  </div><div>    Bit rate                       : 2 000 Kbps  </div><div>    Width                          : 320 pixels  </div><div>    Height                         : 240 pixels  </div><div>    Display aspect ratio           : 4:3  </div><div>    Frame rate mode                : Variable  </div><div>    Frame rate                     : 20.833 fps  </div><div>    Color space                    : YUV  </div><div>    Chroma subsampling             : 4:2:0  </div><div>    Bit depth                      : 8 bits  </div><div>    Scan type                      : Progressive  </div><div>    Bits/(Pixel*Frame)             : 1.250  </div><div>    Stream size                    : 586 KiB  </div><div>    Writing library                : x264 core 148 r2665 a01e339  </div><div>    Encoding settings              : cabac=0 / ref=3 / deblock=1:0:0 / analyse=0x1:0x111 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=0 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=7 / lookahead_threads=1 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=0 / weightp=0 / keyint=12 / keyint_min=1 / scenecut=40 / intra_refresh=0 / rc_lookahead=12 / rc=abr / mbtree=1 / bitrate=2000 / ratetol=1.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00   </div><div><br></div><div>I noticed something odd in the above info: </div><div>- The frame rate is 20.833 fps, instead of the specified 10 fps. </div><div>- Duration of 2s 400ms did not seem right either, since the video played for more than 4s.</div><div><br></div><div>Also, (AVFrame* picture)->pict_type is always set to AV_PICTURE_TYPE_NONE. Is this normal?</div><div> </div><div>The library that I'm using is ffmpeg-20160219-git-98a0053-win32-dev. I would really really appreciate if you could help me out of this confusion. </div><div><br></div><div>/*</div><div> * Video encoding example</div><div> */</div><div>char filename[] = "test.mp4";</div><div>int main(int argc, char argv)</div><div>{</div><div>    AVCodec *codec = NULL;</div><div>    AVCodecContext *codecCtx= NULL;</div><div>    AVFormatContext *pFormatCtx = NULL;</div><div>    AVStream * pVideoStream = NULL;</div><div>    AVFrame *picture = NULL;</div><div>    </div><div>    int i, x, y,            //</div><div>        ret,                 // Return value</div><div>        got_packet_ptr;     // Data encoded into packet</div><div><br></div><div>    printf("Video encoding\n");</div><div>    </div><div>    // Register all formats and codecs</div><div>    av_register_all();</div><div><br></div><div>    // allocate context</div><div>    pFormatCtx = avformat_alloc_context();</div><div>    memcpy(pFormatCtx->filename,filename,</div><div>        min(strlen(filename), sizeof(pFormatCtx->filename)));</div><div>    </div><div>    // guess format</div><div>    pFormatCtx->oformat = av_guess_format("h264", NULL, NULL);</div><div>    if (NULL==pFormatCtx->oformat)</div><div>    {</div><div>        cerr << "Could not guess output format" << endl;</div><div>        return -1;</div><div>    }    </div><div>    </div><div>    // Find the codec.</div><div>    codec = avcodec_find_encoder(pFormatCtx->oformat->video_codec);</div><div>    if (codec == NULL) {</div><div>        fprintf(stderr, "Codec not found\n");</div><div>        return -1;</div><div>    }</div><div>    </div><div>    // Set context</div><div>    int framerate = 10;</div><div>    codecCtx = avcodec_alloc_context3(codec);</div><div>    avcodec_get_context_defaults3(codecCtx, codec); </div><div>    codecCtx->pix_fmt = AV_PIX_FMT_YUV420P;</div><div>    codecCtx->profile = FF_PROFILE_H264_BASELINE; </div><div>    // Resolution must be a multiple of two.</div><div>    codecCtx->width  = 320;</div><div>    codecCtx->height = 240;</div><div>        </div><div>    codecCtx->bit_rate = 2000000;</div><div>    codecCtx->time_base.den = framerate;</div><div>    codecCtx->time_base.num = 1;</div><div>    codecCtx->gop_size = 12; // emit one intra frame every twelve frames at most</div><div>    </div><div>    // Open the codec.    </div><div>    if (avcodec_open2(codecCtx, codec, NULL) < 0) </div><div>    {</div><div>        printf("Cannot open video codec\n");</div><div>        return -1;</div><div>    }</div><div>    </div><div>    // Add stream to pFormatCtx</div><div>    pVideoStream = avformat_new_stream(pFormatCtx, codec);</div><div>    if (!pVideoStream) </div><div>    {</div><div>        printf("Cannot add new video stream\n");</div><div>        return -1;</div><div>    }</div><div>    pVideoStream->codec = codecCtx;</div><div>    pVideoStream->time_base.den = framerate;</div><div>    pVideoStream->time_base.num = 1;</div><div><br></div><div>    if (avio_open2(&pFormatCtx->pb, filename, AVIO_FLAG_WRITE, NULL, NULL) < 0) </div><div>    {</div><div>        printf("Cannot open file\n");</div><div>        return -1;</div><div>    }</div><div>    </div><div>    // Write file header.</div><div>    avformat_write_header(pFormatCtx, NULL);</div><div>    </div><div>    // Create frame</div><div>    picture= av_frame_alloc();</div><div>    picture->format = codecCtx->pix_fmt;</div><div>    picture->width  = codecCtx->width;</div><div>    picture->height = codecCtx->height;</div><div><br></div><div>    int bufferImgSize = av_image_get_buffer_size(codecCtx->pix_fmt, codecCtx->width,</div><div>                    codecCtx->height,1);    </div><div>    av_image_alloc(picture->data, picture->linesize, codecCtx->width, codecCtx->height,                 codecCtx->pix_fmt, 32);</div><div><br></div><div>    AVPacket avpkt;</div><div><br></div><div>    /* encode 1 second of video */</div><div>    for(i=0;i<50;i++) </div><div>    {</div><div>        /* prepare a dummy image */</div><div>        /* Y */</div><div>        for(y=0;y<codecCtx->height;y++)</div><div>        {</div><div>            for(x=0;x<codecCtx->width;x++) </div><div>            {</div><div>                picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;</div><div>            }</div><div>        }</div><div>        /* Cb and Cr */</div><div>        for(y=0;y<codecCtx->height/2;y++) </div><div>        {</div><div>            for(x=0;x<codecCtx->width/2;x++) </div><div>            {</div><div>                picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;</div><div>                picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;</div><div>            }</div><div>        }</div><div><br></div><div>        // Get timestamp</div><div>        picture->pts = (float) i * (1000.0/(float)(codecCtx->time_base.den)) * 90; </div><div><br></div><div>        // Encode frame to packet</div><div>        av_init_packet(&avpkt);</div><div>        got_packet_ptr = 0;</div><div>        int error = avcodec_encode_video2(codecCtx, &avpkt, picture, &got_packet_ptr);</div><div>        if (!error && got_packet_ptr > 0) </div><div>        {</div><div>            // Write packet with frame.</div><div>            ret = (av_interleaved_write_frame(pFormatCtx, &avpkt) == 0);        </div><div>        }    </div><div>        av_packet_unref(&avpkt); </div><div>    }</div><div><br></div><div>    // Flush remaining encoded data</div><div>    while(1)</div><div>    {</div><div>        av_init_packet(&avpkt);</div><div>        got_packet_ptr = 0;</div><div>        // Encode frame to packet.</div><div>        int error = avcodec_encode_video2(codecCtx, &avpkt, NULL, &got_packet_ptr);</div><div>        if (!error && got_packet_ptr > 0) </div><div>        {</div><div>            // Write packet with frame.</div><div>            ret = (av_interleaved_write_frame(pFormatCtx, &avpkt) == 0);        </div><div>        } </div><div>        else </div><div>        {</div><div>            break;</div><div>        }</div><div>        av_packet_unref(&avpkt); </div><div>    }</div><div>    av_write_trailer(pFormatCtx);</div><div>    </div><div>    av_packet_unref(&avpkt);</div><div>    av_frame_free(&picture);</div><div>    </div><div>    avcodec_close(codecCtx);</div><div>    av_free(codecCtx);</div><div><br></div><div>    cin.get();</div><div>}</div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, 30 Mar 2016 at 17:17 Yu Ang Tan <<a href="mailto:isoboy@gmail.com">isoboy@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>I am have trouble encoding an H264 video correctly using FFmpeg libav. I could not play the encoded video in VLC media player, and although I could play the video on MPC-HC the time shows 00:00/00:00. Clearly I'm missing something.</div><div><br></div><div>The Media info from MPC-HC shows this:</div><div>  </div><div>    General  </div><div>    Format                         : AVC  </div><div>    Format/Info                    : Advanced Video Codec  </div><div>    File size                      : 110 KiB  </div><div>    Duration                       : 2s 400ms  </div><div>    Overall bit rate               : 375 Kbps  </div><div>    Writing library                : x264 core 148 r2665 a01e339  </div><div>    Encoding settings              : cabac=0 / ref=3 / deblock=1:0:0 / analyse=0x1:0x111 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=0 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=7 / lookahead_threads=1 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=0 / weightp=0 / keyint=12 / keyint_min=1 / scenecut=40 / intra_refresh=0 / rc_lookahead=12 / rc=abr / mbtree=1 / bitrate=2000 / ratetol=1.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00  </div><div>      </div><div>    Video  </div><div>    Format                         : AVC  </div><div>    Format/Info                    : Advanced Video Codec  </div><div>    Format profile                 : Baseline@L2.1  </div><div>    Format settings, CABAC         : No  </div><div>    Format settings, ReFrames      : 3 frames  </div><div>    Format settings, GOP           : M=1, N=12  </div><div>    Duration                       : 2s 400ms  </div><div>    Bit rate                       : 2 000 Kbps  </div><div>    Width                          : 320 pixels  </div><div>    Height                         : 240 pixels  </div><div>    Display aspect ratio           : 4:3  </div><div>    Frame rate mode                : Variable  </div><div>    Frame rate                     : 20.833 fps  </div><div>    Color space                    : YUV  </div><div>    Chroma subsampling             : 4:2:0  </div><div>    Bit depth                      : 8 bits  </div><div>    Scan type                      : Progressive  </div><div>    Bits/(Pixel*Frame)             : 1.250  </div><div>    Stream size                    : 586 KiB  </div><div>    Writing library                : x264 core 148 r2665 a01e339  </div><div>    Encoding settings              : cabac=0 / ref=3 / deblock=1:0:0 / analyse=0x1:0x111 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=0 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=7 / lookahead_threads=1 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=0 / weightp=0 / keyint=12 / keyint_min=1 / scenecut=40 / intra_refresh=0 / rc_lookahead=12 / rc=abr / mbtree=1 / bitrate=2000 / ratetol=1.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00   </div><div><br></div><div>I noticed something odd in the above info: </div><div>- The frame rate is 20.833 fps, instead of the specified 10 fps. </div><div>- Duration of 2s 400ms did not seem right either, since the video played for more than 4s.</div><div><br></div><div>Also, (AVFrame* picture)->pict_type is always set to AV_PICTURE_TYPE_NONE. Is this normal?</div><div> </div><div>The library that I'm using is ffmpeg-20160219-git-98a0053-win32-dev. I would really really appreciate if you could help me out of this confusion. </div><div><br></div><div>/*</div><div> * Video encoding example</div><div> */</div><div>char filename[] = "test.mp4";</div><div>int main(int argc, char argv)</div><div>{</div><div><span style="white-space:pre-wrap">  </span>AVCodec *codec = NULL;</div><div><span style="white-space:pre-wrap">   </span>AVCodecContext *codecCtx= NULL;</div><div><span style="white-space:pre-wrap">  </span>AVFormatContext *pFormatCtx = NULL;</div><div><span style="white-space:pre-wrap">      </span>AVStream * pVideoStream = NULL;</div><div><span style="white-space:pre-wrap">  </span>AVFrame *picture = NULL;</div><div><span style="white-space:pre-wrap"> </span></div><div><span style="white-space:pre-wrap"> </span>int i, x, y,            //</div><div><span style="white-space:pre-wrap">         </span>ret, <span style="white-space:pre-wrap">                           </span>// Return value</div><div><span style="white-space:pre-wrap">          </span>got_packet_ptr;     // Data encoded into packet</div><div><br></div><div><span style="white-space:pre-wrap">       </span>printf("Video encoding\n");</div><div><span style="white-space:pre-wrap">    </span></div><div><span style="white-space:pre-wrap"> </span>// Register all formats and codecs</div><div><span style="white-space:pre-wrap">       </span>av_register_all();</div><div><br></div><div><span style="white-space:pre-wrap">      </span>// allocate context</div><div><span style="white-space:pre-wrap">      </span>pFormatCtx = avformat_alloc_context();</div><div><span style="white-space:pre-wrap">   </span>memcpy(pFormatCtx->filename,filename,</div><div><span style="white-space:pre-wrap">         </span>min(strlen(filename), sizeof(pFormatCtx->filename)));</div><div><span style="white-space:pre-wrap"> </span></div><div><span style="white-space:pre-wrap"> </span>// guess format</div><div><span style="white-space:pre-wrap">  </span>pFormatCtx->oformat = av_guess_format("h264", NULL, NULL);</div><div><span style="white-space:pre-wrap">  </span>if (NULL==pFormatCtx->oformat)</div><div><span style="white-space:pre-wrap">        </span>{</div><div><span style="white-space:pre-wrap">                </span>cerr << "Could not guess output format" << endl;</div><div><span style="white-space:pre-wrap">           </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}<span style="white-space:pre-wrap">       </span></div><div><span style="white-space:pre-wrap"> </span></div><div><span style="white-space:pre-wrap"> </span>// Find the codec.</div><div><span style="white-space:pre-wrap">       </span>codec = avcodec_find_encoder(pFormatCtx->oformat->video_codec);</div><div><span style="white-space:pre-wrap">    </span>if (codec == NULL) {</div><div><span style="white-space:pre-wrap">             </span>fprintf(stderr, "Codec not found\n");</div><div><span style="white-space:pre-wrap">          </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><span style="white-space:pre-wrap">        </span></div><div><span style="white-space:pre-wrap"> </span>// Set context</div><div><span style="white-space:pre-wrap">   </span>int framerate = 10;</div><div><span style="white-space:pre-wrap">      </span>codecCtx = avcodec_alloc_context3(codec);</div><div><span style="white-space:pre-wrap">        </span>avcodec_get_context_defaults3(codecCtx, codec); </div><div><span style="white-space:pre-wrap">        </span>codecCtx->pix_fmt = AV_PIX_FMT_YUV420P;</div><div><span style="white-space:pre-wrap">       </span>codecCtx->profile = FF_PROFILE_H264_BASELINE; </div><div><span style="white-space:pre-wrap">       </span>// Resolution must be a multiple of two.</div><div><span style="white-space:pre-wrap"> </span>codecCtx->width  = 320;</div><div><span style="white-space:pre-wrap">      </span>codecCtx->height = 240;</div><div><span style="white-space:pre-wrap">               </span></div><div><span style="white-space:pre-wrap"> </span>codecCtx->bit_rate = 2000000;</div><div><span style="white-space:pre-wrap"> </span>codecCtx->time_base.den = framerate;</div><div><span style="white-space:pre-wrap">  </span>codecCtx->time_base.num = 1;</div><div><span style="white-space:pre-wrap">  </span>codecCtx->gop_size = 12; // emit one intra frame every twelve frames at most</div><div><span style="white-space:pre-wrap">  </span></div><div><span style="white-space:pre-wrap"> </span>// Open the codec.<span style="white-space:pre-wrap">      </span></div><div><span style="white-space:pre-wrap"> </span>if (avcodec_open2(codecCtx, codec, NULL) < 0) </div><div><span style="white-space:pre-wrap">       </span>{</div><div><span style="white-space:pre-wrap">                </span>printf("Cannot open video codec\n");</div><div><span style="white-space:pre-wrap">           </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><span style="white-space:pre-wrap">        </span></div><div><span style="white-space:pre-wrap"> </span>// Add stream to pFormatCtx</div><div><span style="white-space:pre-wrap">      </span>pVideoStream = avformat_new_stream(pFormatCtx, codec);</div><div><span style="white-space:pre-wrap">   </span>if (!pVideoStream) </div><div><span style="white-space:pre-wrap">     </span>{</div><div><span style="white-space:pre-wrap">                </span>printf("Cannot add new video stream\n");</div><div><span style="white-space:pre-wrap">               </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><span style="white-space:pre-wrap">        </span>pVideoStream->codec = codecCtx;</div><div><span style="white-space:pre-wrap">       </span>pVideoStream->time_base.den = framerate;</div><div><span style="white-space:pre-wrap">      </span>pVideoStream->time_base.num = 1;</div><div><br></div><div><span style="white-space:pre-wrap">     </span>if (avio_open2(&pFormatCtx->pb, filename, AVIO_FLAG_WRITE, NULL, NULL) < 0) </div><div><span style="white-space:pre-wrap">  </span>{</div><div><span style="white-space:pre-wrap">                </span>printf("Cannot open file\n");</div><div><span style="white-space:pre-wrap">          </span>return -1;</div><div><span style="white-space:pre-wrap">       </span>}</div><div><span style="white-space:pre-wrap">        </span></div><div><span style="white-space:pre-wrap"> </span>// Write file header.</div><div><span style="white-space:pre-wrap">    </span>avformat_write_header(pFormatCtx, NULL);</div><div><span style="white-space:pre-wrap"> </span></div><div><span style="white-space:pre-wrap"> </span>// Create frame</div><div><span style="white-space:pre-wrap">  </span>picture= av_frame_alloc();</div><div><span style="white-space:pre-wrap">       </span>picture->format = codecCtx->pix_fmt;</div><div><span style="white-space:pre-wrap">       </span>picture->width  = codecCtx->width;</div><div><span style="white-space:pre-wrap">        </span>picture->height = codecCtx->height;</div><div><br></div><div><span style="white-space:pre-wrap">       </span>int bufferImgSize = av_image_get_buffer_size(codecCtx->pix_fmt, codecCtx->width,</div><div><span style="white-space:pre-wrap">                                   </span>codecCtx->height,1);<span style="white-space:pre-wrap"> </span></div><div><span style="white-space:pre-wrap"> </span>av_image_alloc(picture->data, picture->linesize, codecCtx->width, codecCtx->height, <span style="white-space:pre-wrap">                                </span>codecCtx->pix_fmt, 32);</div><div><br></div><div><span style="white-space:pre-wrap">      </span>AVPacket avpkt;</div><div><br></div><div><span style="white-space:pre-wrap"> </span>/* encode 1 second of video */</div><div><span style="white-space:pre-wrap">   </span>for(i=0;i<50;i++) </div><div><span style="white-space:pre-wrap">   </span>{</div><div><span style="white-space:pre-wrap">                </span>/* prepare a dummy image */</div><div><span style="white-space:pre-wrap">              </span>/* Y */</div><div><span style="white-space:pre-wrap">          </span>for(y=0;y<codecCtx->height;y++)</div><div><span style="white-space:pre-wrap">            </span>{</div><div><span style="white-space:pre-wrap">                        </span>for(x=0;x<codecCtx->width;x++) </div><div><span style="white-space:pre-wrap">                   </span>{</div><div><span style="white-space:pre-wrap">                                </span>picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;</div><div><span style="white-space:pre-wrap">                    </span>}</div><div><span style="white-space:pre-wrap">                </span>}</div><div><span style="white-space:pre-wrap">                </span>/* Cb and Cr */</div><div><span style="white-space:pre-wrap">          </span>for(y=0;y<codecCtx->height/2;y++) </div><div><span style="white-space:pre-wrap">                </span>{</div><div><span style="white-space:pre-wrap">                        </span>for(x=0;x<codecCtx->width/2;x++) </div><div><span style="white-space:pre-wrap">                 </span>{</div><div><span style="white-space:pre-wrap">                                </span>picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;</div><div><span style="white-space:pre-wrap">                          </span>picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;</div><div><span style="white-space:pre-wrap">                   </span>}</div><div><span style="white-space:pre-wrap">                </span>}</div><div><br></div><div><span style="white-space:pre-wrap">               </span>// Get timestamp</div><div><span style="white-space:pre-wrap">         </span>picture->pts = (float) i * (1000.0/(float)(codecCtx->time_base.den)) * 90; </div><div><br></div><div><span style="white-space:pre-wrap">              </span>// Encode frame to packet</div><div><span style="white-space:pre-wrap">                </span>av_init_packet(&avpkt);</div><div><span style="white-space:pre-wrap">              </span>got_packet_ptr = 0;</div><div><span style="white-space:pre-wrap">              </span>int error = avcodec_encode_video2(codecCtx, &avpkt, picture, &got_packet_ptr);</div><div><span style="white-space:pre-wrap">           </span>if (!error && got_packet_ptr > 0) </div><div><span style="white-space:pre-wrap">           </span>{</div><div><span style="white-space:pre-wrap">                        </span>// Write packet with frame.</div><div><span style="white-space:pre-wrap">                      </span>ret = (av_interleaved_write_frame(pFormatCtx, &avpkt) == 0);<span style="white-space:pre-wrap">                </span></div><div><span style="white-space:pre-wrap">         </span>}<span style="white-space:pre-wrap">       </span></div><div><span style="white-space:pre-wrap">         </span>av_packet_unref(&avpkt); </div><div><span style="white-space:pre-wrap">   </span>}</div><div><br></div><div><span style="white-space:pre-wrap">       </span>// Flush remaining encoded data</div><div><span style="white-space:pre-wrap">  </span>while(1)</div><div><span style="white-space:pre-wrap"> </span>{</div><div><span style="white-space:pre-wrap">                </span>av_init_packet(&avpkt);</div><div><span style="white-space:pre-wrap">              </span>got_packet_ptr = 0;</div><div><span style="white-space:pre-wrap">              </span>// Encode frame to packet.</div><div><span style="white-space:pre-wrap">               </span>int error = avcodec_encode_video2(codecCtx, &avpkt, NULL, &got_packet_ptr);</div><div><span style="white-space:pre-wrap">              </span>if (!error && got_packet_ptr > 0) </div><div><span style="white-space:pre-wrap">           </span>{</div><div><span style="white-space:pre-wrap">                        </span>// Write packet with frame.</div><div><span style="white-space:pre-wrap">                      </span>ret = (av_interleaved_write_frame(pFormatCtx, &avpkt) == 0);<span style="white-space:pre-wrap">                </span></div><div><span style="white-space:pre-wrap">         </span>} </div><div><span style="white-space:pre-wrap">              </span>else </div><div><span style="white-space:pre-wrap">           </span>{</div><div><span style="white-space:pre-wrap">                        </span>break;</div><div><span style="white-space:pre-wrap">           </span>}</div><div><span style="white-space:pre-wrap">                </span>av_packet_unref(&avpkt); </div><div><span style="white-space:pre-wrap">   </span>}</div><div><span style="white-space:pre-wrap">        </span>av_write_trailer(pFormatCtx);</div><div><span style="white-space:pre-wrap">    </span></div><div><span style="white-space:pre-wrap"> </span>av_packet_unref(&avpkt);</div><div><span style="white-space:pre-wrap">     </span>av_frame_free(&picture);</div><div><span style="white-space:pre-wrap">     </span></div><div><span style="white-space:pre-wrap"> </span>avcodec_close(codecCtx);</div><div><span style="white-space:pre-wrap"> </span>av_free(codecCtx);</div><div><br></div><div><span style="white-space:pre-wrap">      </span>cin.get();</div><div>}</div></div></blockquote></div>