libx264 encoded video lags by few seconds in comparison to audio

تعرفه تبلیغات در سایت

آخرین مطالب

امکانات وب

Vote count: 0

Below is the code i am using for reading a .3gp file encoding it using libx264 and writing it to .mp4 container. Code is working but when i play .mp4 file then video is not in sync with audio. Video is few second slow in comparison to audio.

// Read all packet from context
while( av_read_frame( fmt_ctx , pAVPacket ) >= 0 )
{
    int decoded = pAVPacket->size;
    if(pAVPacket->stream_index == video_stream_idx)
    {
        //decode packet
        value = avcodec_decode_video2(video_dec_ctx , pAVFrame , 
        &frameFinished , pAVPacket );
        if( value < 0)
        {
            av_free_packet(pAVPacket);
            return -1;
        }

        if(frameFinished)// Frame successfully decoded :)
        {
            sws_scale(swsCtx_, (const uint8_t *const *) pAVFrame->data, 
            pAVFrame->linesize, 0, video_dec_ctx->height, outFrame->data, 
            outFrame->linesize);
            av_init_packet(&outPacket);
            outPacket.data = NULL;    // packet data will be allocated by 
            the encoder
            outPacket.size = 0;

            outFrame->pts=z;
            //encode frame
            avcodec_encode_video2(outAVCodecContext , &outPacket ,outFrame , 
            &got_picture);


            if(got_picture)
            {
                if(outPacket.pts != AV_NOPTS_VALUE)
                    outPacket.pts = av_rescale_q(outPacket.pts, video_st-
                    >codec->time_base, video_st->time_base);
                if(outPacket.dts != AV_NOPTS_VALUE)
                    outPacket.dts = av_rescale_q(outPacket.dts, video_st-
                    >codec->time_base, video_st->time_base);

                //Write encoded packet to file.
                if(av_write_frame(outAVFormatContext , &outPacket) != 0)
                {
                    av_free_packet(pAVPacket);
                    av_free_packet(&outPacket);
                    return -1;
                }
                z++;
                av_free_packet(&outPacket);
            } // got_picture
        } // got_picture
    }
    // If packet is from audio stream
    else if (pAVPacket->stream_index == audio_stream_idx)
    {
       //Write to file without decoding and encoding it
       if(av_write_frame(outAVFormatContext , pAVPacket) != 0)
        {
        }
    }
    av_free_packet(pAVPacket);
    //z++;
}// End of while-loop

value = av_write_trailer(outAVFormatContext);
if( value < 0)
{
    return -1;
}

Can you please help in finding out what i am doing wrong?

asked 10 secs ago
Herdesh Verma

نویسنده : استخدام کار بازدید : 16 تاريخ : دوشنبه 27 شهريور 1396 ساعت: 1:53

فهرست وبلاگ