[Libav-user] Loosing Video Frames

gaurav mishra gauravmishra.itbhu at gmail.com
Mon Oct 12 15:31:51 CEST 2015


Update -
I added some code after my main while loop to flush remaining frames from
codec.
Also i have added one more variable while configuring my encoder ie
gop_size. these two changes have improved the situation.
the lost amount of video has come down to approx 0.9 secs.

*Question- what codec settings i have missed which are required to make
sure that input and output files are identical.  *
my current encoder setting code looks like -

            c->codec_id           = inputStream->codec->codec_id;
            c->bit_rate           = inputStream->codec->bit_rate;

            /* Resolution must be a multiple of two. */
            c->width              = inputStream->codec->width;
            c->height             = inputStream->codec->height;
            c->gop_size           = inputStream->codec->gop_size;

            c->time_base          = inputStream->codec->time_base;
            out_stream->time_base = inputStream->time_base;

av_stream_set_r_frame_rate(out_stream,av_stream_get_r_frame_rate(inputStream));

            c->pix_fmt            = AV_PIX_FMT_YUV420P;
            c->sample_aspect_ratio.num =
inputStream->codec->sample_aspect_ratio.num;
            c->sample_aspect_ratio.den =
inputStream->codec->sample_aspect_ratio.den;
            if (oc->oformat->flags & AVFMT_GLOBALHEADER)
                  c->flags |= CODEC_FLAG_GLOBAL_HEADER;


following are line from ffmpeg -i
*for input file -*

 Duration: 00:00:30.92, start: 0.040000, bitrate: 998 kb/s

    Stream #0:0(eng): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv),
1280x720 [SAR 1:1 DAR 16:9], 996 kb/s, 25 fps, 25 tbr, 25k tbn, 50 tbc
(default)

    Metadata:

      handler_name    : VideoHandler


*for output file - *

Duration: 00:00:30.02, start: 0.000000, bitrate: 1022 kb/s

    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p,
1280x720 [SAR 1:1 DAR 16:9], 1019 kb/s, 25.02 fps, 25 tbr, 25k tbn, 100 tbc
(default)

    Metadata:

      handler_name    : VideoHandler


On Mon, Oct 12, 2015 at 3:13 PM, gaurav mishra <gauravmishra.itbhu at gmail.com
> wrote:

> Hi,
> I have written some code which opens up a mp4 file , decodes each frame ,
> does some manipulation on it and encode it back to new container. Issue i
> am seeing here is i am loosing last 3 sec (approx 65 frames) of video .
> Below is a simplified version of code which i expected to create a
> identical file.which does no manipulation on frame just decode a frame then
> encode it back and finally mux it.
>
> while(1) {
>
>         ret = av_read_frame(ifmt_ctx, &packet);
>         if(ret < 0) {
>             av_log( NULL , AV_LOG_ERROR , "error reading frame or end of
> file");
>             break;
>         }
>         AVStream *in_stream = ifmt_ctx->streams[packet.stream_index];
>         AVStream *output_stream =
> out_stream.format_ctx->streams[packet.stream_index];
>
>
>             stream_index = packet.stream_index;
>             frame_decoded = 0;
>             frame = av_frame_alloc();
>
>
>             ret = avcodec_decode_video2(in_stream->codec, frame,
> &frame_decoded, &packet);
>
>             if(ret<0){
>                 av_frame_free(&frame);
>                 fprintf(stderr,"could not decode a packet....");
>                 return ret;
>             }
>
>             if(frame_decoded){
>
>                 ptsFactor = 2;
>                 frame->pts = ptsFactor*frameEncodedCount ;
>                 frameEncodedCount++;
>
>                 av_init_packet(&encoded_packet);
>
>                 encode_success = 0;
>
>                 ret =
> avcodec_encode_video2(this->out_stream.format_ctx->streams[0]->codec,
>                                             &encoded_packet,
>                                             frame,
>                                             &encode_success);
>
>                 encoded_packet.stream_index = 0;
>
>                 if(encode_success){
>
>                     av_packet_rescale_ts(&encoded_packet,
>
>  this->out_stream.format_ctx->streams[0]->codec->time_base,
>
>  this->out_stream.format_ctx->streams[0]->time_base);
>
>
>                     ret =
> av_interleaved_write_frame(this->out_stream.format_ctx, &encoded_packet);
>                 }
>             }
>
>
> }
>
> I also noticed that the first above code failed to encode first 60 frames
> of video. ie
> it successfully decoded frames but didnt enter the if(encode_success)
> block. Any idea what might be going wrong here.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20151012/1b4595a0/attachment.html>


More information about the Libav-user mailing list