I have had this problem for two weeks and not two days, I have also sent other mails to the mailing list and got no answer (And didn't include my code in them.) I have been asking for help daily on the IRC channel, no one could help. What I tried was changing the whole EOF state and flushing code of transoding multiple times and changing options in the AVCodecContext, and also tried changing libx264 options.<br /><br />I no longer want help as there wont be anyways since no one seems to know the problem, but I will try my best to fix it, and when I do, I wont post on how I did solve it, let it stay a mystery.<br /><br /><div>11.10.2016, 09:32, "Masneri, Stefano" <stefano.masneri@brain.mpg.de>:</div><blockquote type="cite"><div><blockquote> -----Original Message-----<br /> From: Libav-user [mailto:<a>libav-user-bounces@ffmpeg.org</a>] On Behalf Of M N<br /> Sent: Montag, 10. Oktober 2016 22:20<br /> To: This list is about using libavcodec, libavformat, libavutil, libavdevice and<br /> libavfilter.<br /> Subject: Re: [Libav-user] Last audio frame missing when transcoding to H264<br /><br /> No one in the mailing list nor the IRC channel knows the answer? Nice :)<br /> That's really great!<br /><br /> 08.10.2016, 00:45, "M N" <<a>assemblerx86@yandex.com</a>>:<br /> > Hi,<br /> ><br /> > I am doing a program to transcode .mp4 files to H264, but I am running<br /> > into a problem which is that the last audio frame is not being written<br /> > to the output stream, and MediaInfo gives (Duration_LastFrame: -20<br /> > ms.) I also don't know if it has to do with this, but Windows Media<br /> > Player doesn't show the video of the generated mp4 file, it just plays<br /> > the sound but the video is black screen (Checked the color space and<br /> > chroma subsampling, its yuv420p.)<br /> ><br /> > Here is my code:<br /> ><br /> > #include "libavformat/avformat.h"<br /> > #include "libavcodec/avcodec.h"<br /> > #include "libavutil/avutil.h"<br /> > #include "libavutil/rational.h"<br /> > #include "libavutil/timestamp.h"<br /> ><br /> > #include <stdio.h><br /> ><br /> > static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket<br /> > *pkt, const char *tag) {<br /> >     AVRational *time_base =<br /> > &fmt_ctx->streams[pkt->stream_index]->time_base;<br /> >     printf("%s: pts:%s pts_time:%s dts:%s dts_time:%s duration:%s<br /> > duration_time:%s stream_index:%d\n\n",<br /> >            tag,<br /> >            av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),<br /> >            av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),<br /> >            av_ts2str(pkt->duration), av_ts2timestr(pkt->duration,<br /> > time_base),<br /> >            pkt->stream_index);<br /> > }<br /> ><br /> > int main()<br /> > {<br /> >     av_register_all();<br /> ><br /> >     av_log_set_level(AV_LOG_FATAL);<br /> ><br /> >     AVFormatContext *ps = avformat_alloc_context();<br /> ><br /> >     AVFormatContext *ps2 = NULL;<br /> >     AVOutputFormat *oF = av_guess_format("mp4", NULL, "video/mp4");<br /> ><br /> >     FILE *gSize = fopen("vid.mp4", "rb");<br /> >     fseek(gSize, 0, SEEK_END);<br /> >     size_t iSize = ftell(gSize);<br /> >     fclose(gSize);<br /> ><br /> >     if(avformat_open_input(&ps, "vid.mp4", NULL, NULL) != 0)<br /> >     {<br /> >         printf("Failed to open input file.\n");<br /> >         return -1;<br /> >     }<br /> ><br /> >     avformat_alloc_output_context2(&ps2, oF, NULL, "vid2.mp4");<br /> ><br /> >     avformat_find_stream_info(ps, NULL);<br /> ><br /> >     AVCodecContext **pC = (AVCodecContext**)malloc(ps->nb_streams),<br /> > **p2C = (AVCodecContext**)malloc(ps->nb_streams);<br /> ><br /> >     AVStream *oStream = NULL;<br /> >     AVStream *iStream = NULL;<br /> ><br /> >     AVCodec *encoder = NULL;<br /> >     AVCodec *decoder = NULL;<br /> >     AVCodecContext *strCtx = NULL;<br /> ><br /> >     unsigned int i;<br /> ><br /> >     avio_open(&ps2->pb, "vid2.mp4", AVIO_FLAG_WRITE);<br /> ><br /> >     for(i = 0; i < ps->nb_streams; i++)<br /> >     {<br /> >         printf("%d\n", i);<br /> ><br /> >         iStream = ps->streams[i];<br /> ><br /> >         pC[i] = iStream->codec;<br /> ><br /> >         if(pC[i]->codec_type == AVMEDIA_TYPE_UNKNOWN)<br /> >         {<br /> >             printf("Skipping bad stream\n");<br /> >             continue;<br /> >         }<br /> ><br /> >         if(pC[i]->codec_type == AVMEDIA_TYPE_VIDEO ||<br /> > pC[i]->codec_type == AVMEDIA_TYPE_AUDIO)<br /> >         {<br /> >             encoder = avcodec_find_encoder(pC[i]->codec_id);<br /> >             if (!encoder)<br /> >             {<br /> >                 av_log(NULL, AV_LOG_FATAL, "Necessary encoder not<br /> > found\n");<br /> >                 return AVERROR_INVALIDDATA;<br /> >             }<br /> ><br /> >             oStream = avformat_new_stream(ps2, encoder);<br /> ><br /> >             //av_dict_copy(&oStream->metadata, iStream->metadata, 0);<br /> ><br /> >             strCtx = oStream->codec; //We have to set oStream->codec<br /> > parameters for write_header to work,<br /> >                                     //since write_header only relies on the stream<br /> parameters.<br /> ><br /> >             //avcodec_parameters_copy(oStream->codecpar,<br /> > iStream->codecpar);<br /> >             //p2C[i] = oStream->codec;<br /> >             p2C[i] = avcodec_alloc_context3(encoder); //H264 codec<br /> > context must be set using alloc_context<br /> ><br /> >             //AVCodecParameters *pars = avcodec_parameters_alloc();<br /> >             //avcodec_parameters_from_context(pars, pC[i]);<br /> >             //avcodec_parameters_to_context(p2C[i], pars);<br /> ><br /> >             AVDictionary *param = NULL;<br /> ><br /> >             if (pC[i]->codec_type == AVMEDIA_TYPE_VIDEO)<br /> >             {<br /> >                 p2C[i]->width = pC[i]->width;<br /> >                 p2C[i]->height = pC[i]->height;<br /> ><br /> >                 if (encoder->pix_fmts)<br /> >                     p2C[i]->pix_fmt = encoder->pix_fmts[0];<br /> >                 else<br /> >                     p2C[i]->pix_fmt = pC[i]->pix_fmt;<br /> ><br /> >                 p2C[i]->sample_rate = pC[i]->sample_rate;<br /> >                 p2C[i]->sample_aspect_ratio =<br /> > pC[i]->sample_aspect_ratio;<br /> >                 //p2C[i]->bits_per_coded_sample =<br /> > pC[i]->bits_per_coded_sample;<br /> >                 //p2C[i]->bits_per_raw_sample =<br /> > pC[i]->bits_per_raw_sample;<br /> >                 //p2C[i]->flags = pC[i]->flags;<br /> >                 //p2C[i]->flags2 = pC[i]->flags2;<br /> >                 p2C[i]->time_base = pC[i]->time_base;<br /> >                 //p2C[i]->bit_rate = pC[i]->bit_rate;<br /> >                 //p2C[i]->bit_rate_tolerance =<br /> > pC[i]->bit_rate_tolerance;<br /> >                 free(p2C[i]->extradata);<br /> >                 p2C[i]->extradata =<br /> > (uint8_t*)malloc(pC[i]->extradata_size);<br /> >                 p2C[i]->extradata = pC[i]->extradata;<br /> >                 p2C[i]->extradata_size = pC[i]->extradata_size;<br /> >                 p2C[i]->gop_size = pC[i]->gop_size;<br /> ><br /> >                 strCtx->width = pC[i]->width;<br /> >                 strCtx->height = pC[i]->height;<br /> ><br /> >                 /*if (encoder->pix_fmts)<br /> >                     strCtx->pix_fmt = encoder->pix_fmts[0];<br /> >                 else<br /> >                     strCtx->pix_fmt = pC[i]->pix_fmt;*/<br /> >                 //strCtx->sample_rate = pC[i]->sample_rate;<br /> >                 //strCtx->sample_aspect_ratio =<br /> > pC[i]->sample_aspect_ratio;<br /> >                 strCtx->time_base = pC[i]->time_base;<br /> >                 free(strCtx->extradata);<br /> >                 strCtx->extradata =<br /> > (uint8_t*)malloc(pC[i]->extradata_size);<br /> >                 strCtx->extradata = pC[i]->extradata;<br /> >                 strCtx->extradata_size = pC[i]->extradata_size;<br /> ><br /> >                 //av_dict_set(&param, "qp", "23", 0);<br /> >                 //av_opt_set(p2C[i]->priv_data, "profile", "high", (1<br /> > << 0));<br /> >                 //av_opt_set(strCtx->priv_data, "profile", "high", (1<br /> > << 0));<br /> >                 /*<br /> >                 Change options to trade off compression efficiency against<br /> encoding speed. If you specify a preset, the changes it makes will be applied<br /> before all other parameters are applied.<br /> >                 You should generally set this option to the slowest you can bear.<br /> >                 Values available: ultrafast, superfast, veryfast, faster, fast,<br /> medium, slow, slower, veryslow, placebo.<br /> >                 */<br /> >                 //av_dict_set(&param, "preset", "placebo", 0);<br /> >                 /*<br /> >                 Tune options to further optimize them for your input content. If<br /> you specify a tuning, the changes will be applied after --preset but before all<br /> other parameters.<br /> >                 If your source content matches one of the available tunings you can<br /> use this, otherwise leave unset.<br /> >                 Values available: film, animation, grain, stillimage, psnr, ssim,<br /> fastdecode, zerolatency.<br /> >                 */<br /> >                 //av_dict_set(&param, "crf", "23", 0);<br /> >                 //av_dict_set(&param, "coder", "1", 0);<br /> >                 //av_dict_set(&param, "vprofile", "film", 0);<br /> >                 //av_dict_set(&param, "tune", "zerolatency", 0);<br /> >                 //av_dict_set(&param, "no-cabac", "0", 0);<br /> >                 //av_dict_set(&param, "preset", "medium", 0);<br /> >             }<br /> >             else<br /> >             {<br /> >                 //av_opt_set(p2C[i]->priv_data, "profile", "high", (1<br /> > << 0));<br /> >                 p2C[i]->sample_rate = pC[i]->sample_rate;<br /> >                 p2C[i]->sample_aspect_ratio =<br /> > pC[i]->sample_aspect_ratio;<br /> >                 p2C[i]->channel_layout = pC[i]->channel_layout;<br /> >                 p2C[i]->channels =<br /> > av_get_channel_layout_nb_channels(p2C[i]->channel_layout);<br /> >                 // take first format from list of supported formats<br /> >                 p2C[i]->sample_fmt = encoder->sample_fmts[0];<br /> >                 p2C[i]->time_base = (AVRational){1,<br /> > p2C[i]->sample_rate};<br /> >                 p2C[i]->frame_size = pC[i]->frame_size;<br /> >                 free(p2C[i]->extradata);<br /> >                 p2C[i]->extradata =<br /> > (uint8_t*)malloc(pC[i]->extradata_size);<br /> >                 p2C[i]->extradata = pC[i]->extradata;<br /> >                 p2C[i]->extradata_size = pC[i]->extradata_size;<br /> >                 //p2C[i]->gop_size = pC[i]->gop_size;<br /> ><br /> >                 strCtx->sample_rate = pC[i]->sample_rate;<br /> >                 strCtx->sample_aspect_ratio =<br /> > pC[i]->sample_aspect_ratio;<br /> >                 //strCtx->channel_layout = pC[i]->channel_layout;<br /> >                 //strCtx->channels =<br /> > av_get_channel_layout_nb_channels(strCtx->channel_layout);<br /> >                 // take first format from list of supported formats<br /> >                 //strCtx->sample_fmt = encoder->sample_fmts[0];<br /> >                 strCtx->time_base = (AVRational){1,<br /> > strCtx->sample_rate};<br /> >                 strCtx->frame_size = pC[i]->frame_size;<br /> >                 free(strCtx->extradata);<br /> >                 strCtx->extradata =<br /> > (uint8_t*)malloc(pC[i]->extradata_size);<br /> >                 strCtx->extradata = pC[i]->extradata;<br /> >                 strCtx->extradata_size = pC[i]->extradata_size;<br /> >             }<br /> ><br /> >             //AVCodecParameters *par = avcodec_parameters_alloc();<br /> >             //avcodec_parameters_from_context(par, pC[i]);<br /> >             //avcodec_parameters_to_context(p2C[i], par);<br /> ><br /> >             decoder = avcodec_find_decoder(pC[i]->codec_id);<br /> >             if(decoder == NULL) printf("Couldn't find decoder\n");<br /> ><br /> >             int ret1 = avcodec_open2(pC[i], decoder, NULL);<br /> >             int ret2 = avcodec_open2(p2C[i], encoder, NULL);<br /> >             printf("Ret1: %d | Ret2: %d\n", ret1, ret2);<br /> ><br /> >         }<br /> >         else if (pC[i]->codec_type == AVMEDIA_TYPE_UNKNOWN) {<br /> >             av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of<br /> > unknown type, cannot proceed\n", i);<br /> ><br /> >         }<br /> >         else<br /> >         {<br /> >             //avcodec_copy_context(oStream->codec, iStream->codec);<br /> >             //printf("BUG\n");<br /> >         }<br /> >     }<br /> >     printf("done\n");<br /> ><br /> >     AVDictionaryEntry *tag = NULL;<br /> >     while ((tag = av_dict_get(ps2->metadata, "", tag,<br /> > AV_DICT_IGNORE_SUFFIX)))<br /> >         printf("%s=%s\n", tag->key, tag->value);<br /> ><br /> >     int ret = avformat_write_header(ps2, NULL);<br /> >     char err[200];<br /> >     av_make_error_string(err, 200, ret);<br /> >     printf("Write header %d: %s\n", ret, err);<br /> >     printf("Frames in 0: %d\n", ps->streams[0]->nb_frames);<br /> >     printf("Frames in 1: %d\n", ps->streams[1]->nb_frames);<br /> >     int decoded_af = 0;<br /> >     int audio_frames = 0;<br /> >     int encoded_af = 0, encoded2_af = 0;<br /> ><br /> >     int state = 0;<br /> >     int prevStream = 0;<br /> ><br /> >     unsigned long long j = 0;<br /> >     for(;; ++j)<br /> >     {<br /> >         AVPacket *pkts = av_packet_alloc();<br /> >         av_init_packet(pkts);<br /> >         pkts->data = NULL;<br /> >         pkts->size = 0;<br /> >         AVPacket *pktr = av_packet_alloc();<br /> >         av_init_packet(pktr);<br /> >         pktr->data = NULL;<br /> >         pktr->size = 0;<br /> >         AVFrame *rawFrame = av_frame_alloc();<br /> ><br /> >         if(av_read_frame(ps, pkts) == AVERROR_EOF)<br /> >         {<br /> >             //printf("END\n");<br /> ><br /> >             if(state == 0)<br /> >             {<br /> >                 state++;<br /> >                 printf("Changed to state %d\n", state);<br /> >             }<br /> ><br /> >         }<br /> ><br /> >         int stream_index = pkts->stream_index;<br /> >         //if(prevStream != stream_index)<br /> >         prevStream = stream_index;<br /> ><br /> >         if(!(ps2->flags & AVFMT_NOTIMESTAMPS))<br /> >         {<br /> >             pkts->dts = av_rescale_q(pkts->dts,<br /> > ps->streams[stream_index]->time_base,<br /> > ps2->streams[stream_index]->time_base);<br /> >             pkts->pts = av_rescale_q(pkts->pts,<br /> > ps->streams[stream_index]->time_base,<br /> > ps2->streams[stream_index]->time_base);<br /> >             pkts->duration = av_rescale_q(pkts->duration,<br /> > ps->streams[stream_index]->time_base,<br /> > ps2->streams[stream_index]->time_base);<br /> >             //pkts->pos = -1;<br /> >             //log_packet(ps2, pkts, "out");<br /> >         }<br /> >         else<br /> >         {<br /> >             pkts->dts = AV_NOPTS_VALUE;<br /> >             pkts->pts = AV_NOPTS_VALUE;<br /> >             printf("NO TIME STAMPS!\n");<br /> >         }<br /> ><br /> >         //decoding<br /> >         int dret = 0, eret = 0;<br /> ><br /> >         if(state == 0) avcodec_send_packet(pC[stream_index], pkts);<br /> >         else if(state == 1)<br /> >         {<br /> >             avcodec_send_packet(pC[pkts->stream_index], NULL);<br /> >             state++;<br /> >         }<br /> ><br /> >         dret = avcodec_receive_frame(pC[stream_index], rawFrame);<br /> >         if(dret == 0 || state >= 3)<br /> >         {<br /> >             if(stream_index == 1) decoded_af++;<br /> >             //encoding<br /> >             if(state < 3)<br /> >             {<br /> >                 rawFrame->pts =<br /> > av_frame_get_best_effort_timestamp(rawFrame);<br /> >                 int rets = avcodec_send_frame(p2C[stream_index],<br /> > rawFrame);<br /> >                 if(rets == 0 && stream_index == 1) encoded_af++;<br /> >                 //if(stream_index == 1) printf("Frame: %d\n",<br /> > p2C[stream_index]->frame_number);<br /> >             }<br /> >             else if (state == 3)<br /> >             {<br /> >                 avcodec_send_frame(p2C[stream_index], NULL);<br /> >                 state++;<br /> >             }<br /> ><br /> >             eret = avcodec_receive_packet(p2C[stream_index], pktr);<br /> >             if(eret == 0)<br /> >             {<br /> >                 if(stream_index == 1) encoded2_af++;<br /> ><br /> >                 while(eret == 0)<br /> >                 {<br /> >                     pktr->stream_index = stream_index;<br /> >                     int retW = av_interleaved_write_frame(ps2, pktr);<br /> ><br /> >                     if(retW != 0)<br /> >                     {<br /> >                         printf("Failed to write packet\n");<br /> >                         break;<br /> >                     }<br /> >                     else if(retW == 0 && stream_index == 1)<br /> > audio_frames++;<br /> >                     eret = avcodec_receive_packet(p2C[stream_index],<br /> > pktr);<br /> >                 }<br /> >                 //avcodec_flush_buffers(pC[stream_index]);<br /> >             }<br /> >             else if(eret == AVERROR_EOF)<br /> >             {<br /> >                 if(stream_index == 1) printf("Audio frame failure at<br /> > EOF\n");<br /> >                 avcodec_flush_buffers(pC[stream_index]);<br /> >                 printf("Finished\n");<br /> >                 break;<br /> >             }<br /> >             else if(eret == AVERROR(EAGAIN))<br /> >             {<br /> >                 if(stream_index == 1) printf("Audio frame failure at<br /> > AVERROR(EAGAIN)\n");<br /> >                 else printf("AVERROR(EAGAIN)\n");<br /> >                 //continue;<br /> >                 goto clean;<br /> >             }<br /> >             else<br /> >             {<br /> >                 if(stream_index == 1) printf("Audio frame failure at<br /> > other error.\n");<br /> >                 printf("other error\n");<br /> >             }<br /> >         }<br /> >         else if(dret == AVERROR_EOF && state == 2)<br /> >         {<br /> >             state++;<br /> ><br /> >             printf("Changed to state %d\n", state);<br /> >         }<br /> ><br /> > clean:<br /> ><br /> >         av_packet_free(&pkts);<br /> >         av_packet_free(&pktr);<br /> >         av_frame_free(&rawFrame);<br /> >         av_frame_unref(rawFrame);<br /> >     }<br /> ><br /> >     printf("Written AF: %d\nDecoded AF: %d\nEncoded AF:<br /> > %d\nEncoded2_AF: %d\n", audio_frames, decoded_af, encoded_af,<br /> > encoded2_af);<br /> ><br /> >     if(av_write_trailer(ps2) == 0) printf("Wrote trailer\n");<br /> ><br /> > }<br /> > ********************************************<br /> ><br /> > Thanks!<br /> > _______________________________________________<br /></blockquote><p><br />I mostly use the list just for asking question, but here's my 2 cents: you cannot really expect to post 200 lines of code, saying "it doesn't work" and pretending someone fixes that for you. The passive-aggressive message you just posted only makes things worse, imho. What have you done to fix the error in the last two days?<br />_______________________________________________<br />Libav-user mailing list<br /><a>Libav-user@ffmpeg.org</a><br /><a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank" rel="noopener noreferrer">http://ffmpeg.org/mailman/listinfo/libav-user</a><br /></p></div></blockquote><div><br /></div><div><br /></div><div></div><div><br /></div>