<div class="">
        <p>Hey,</p><p>I am encoding live rendered video data and an existing .wav file into an mpg-file.</p>

<p>To do that I first write all audio frames, and then the video frames 
as they come in from the render engine. For smaller .wav files (< 25 
seconds), everything works perfectly fine. But as soon as I use a longer
 .wav file, av_write_frame (when writing the audio frame) just returns 
-1 after having written some 100 frames. It is never the same frame at 
which it fails, also it is never the last frame.
All test .wav files can be played perfectly with any player I tested.</p>

<p>I am following <a href="http://ffmpeg.org/doxygen/trunk/muxing_8c-source.html" rel="nofollow">the muxing example</a> (more or less).</p>

<p>Here is my function that writes an audio frame:</p>

<pre><code>void write_audio_frame( Cffmpeg_dll * ptr, AVFormatContext *oc, AVStream *st, int16_t sample_val )
{
AVCodecContext *c;
AVPacket pkt = { 0 }; // data and size must be 0;
AVFrame *frame = avcodec_alloc_frame();
int got_packet;

av_init_packet(&pkt);
c = st->codec;

get_audio_frame(ptr, ptr->samples, ptr->audio_input_frame_size, c->channels);
frame->nb_samples =  ptr->audio_input_frame_size;
int result = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
                        (uint8_t *) ptr->samples,
                         ptr->audio_input_frame_size *
                        av_get_bytes_per_sample(c->sample_fmt) *
                        c->channels, 0);
if (result != 0)
{
    av_log(c, AV_LOG_ERROR, "Error filling audio frame. Code: %i\n", result);
    exit(1);
}

result = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
if (result != 0)
{
    av_log(c, AV_LOG_ERROR, "Error encoding audio. Code: %i\n", result);
    exit(1);
}


if (c->coded_frame && c->coded_frame->pts != AV_NOPTS_VALUE)
    pkt.pts= av_rescale_q(c->coded_frame->pts, c->time_base, st->time_base);
pkt.flags |= AV_PKT_FLAG_KEY;

pkt.stream_index= st->index;
av_log(c, AV_LOG_ERROR, "Got? %i Pts: %i Dts: %i Flags: %i Side Elems: %i Size: %i\n", 
        got_packet, pkt.pts, pkt.dts, pkt.flags, pkt.side_data_elems, pkt.size);

/* write the compressed frame in the media file */
result = av_write_frame(oc, &pkt);
if (result != 0) 
{
    av_log(c, AV_LOG_ERROR, "Error while writing audio frame. Result: %i\n", result);
    exit(1);
}
</code></pre>

<p>}</p>

<p>And here is my get_audio_frame function:</p>

<pre><code>void get_audio_frame( Cffmpeg_dll* ptr, int16_t* samples, int frame_size, int nb_channels, int16_t sample_val ) 
{
    fread( samples, sizeof( int16_t ), frame_size * nb_channels, ptr->fp_sound_input );
};
</code></pre>

<p>And finally, this is the loop in which I write all audio frames 
(don't worry about the .wav header, I skipped it before that loop):</p>

<pre><code>while (!feof(ptr->fp_sound_input))
    {
        write_audio_frame( ptr, ptr->oc, ptr->audio_st, -1 );
    }
</code></pre>

<p>As you can see, I'm outputting almost everything in the packet and 
check for any possible error. Other than av_write_frame failing after 
some time when I am encoding a longer audio file, everything seems 
perfectly fine. All the packet values I am tracking are 100% the same 
for all frames (except the data pointer, obviously). Also, as stated, the
 same procedure works flawlessly for shorter fp_sound_input files. 
avcodec_encode_audio2() and avcodec_fill_audio_frame() also never fail. </p>

<p>The codecs I use for encoding are CODEC_ID_MPEG2VIDEO (video) and 
CODEC_ID_MP2 (audio). The .wav files are saved in PCM 16 LE (all use the
 exact same encoding).</p>

<p>What could be wrong here?</p>

    </div><br clear="all"><br>-- <br><font color="#666666"><font size="1"><font face="Tahoma">Jan Drabner </font></font></font><font face="Times New Roman"><font size="3"><font color="#666666"><font face="Tahoma"><font style="font-size:7pt" size="1">׀ TD Programming Engine & Animation</font></font></font></font></font><p style="margin-top:0.05cm;margin-bottom:0.05cm;line-height:100%">
<font face="Times New Roman, serif"><font size="3"><font color="#666666"><font face="Tahoma, serif"><font style="font-size:7pt" size="1"></font></font></font></font></font></p>
<p style="margin-top:0.05cm;margin-bottom:0.05cm;line-height:100%">
<br><br>
</p>
<p style="margin-top:0.05cm;margin-bottom:0.05cm;line-height:100%">
<font color="#ff0000"><font face="Times New Roman, serif"><font size="3"><font face="Tahoma, serif"><font style="font-size:7pt" size="1">zoobe</font></font></font><font color="#666666"><font face="Tahoma, serif"><font style="font-size:7pt" size="1"> message
entertainment gmbh</font></font></font></font></font></p>
<p style="margin-top:0.05cm;margin-bottom:0.05cm;line-height:100%">
<font color="#666666"><font face="Times New Roman, serif"><font size="3"><font face="Tahoma, serif"><font style="font-size:7pt" size="1">kurfürstendamm
226 l 10719 berlin</font></font></font></font></font></p>
<p style="margin-top:0.05cm;margin-bottom:0.05cm;line-height:100%">
<br><br>
</p>
<p style="margin-top:0.05cm;margin-bottom:0.05cm;line-height:100%"><a name="SafeHtmlFilter_SafeHtmlFilter__GoBack"></a>
<font color="#666666"> <font face="Times New Roman, serif"><font size="3"><font face="Tahoma, serif"><font style="font-size:7pt" size="1">email: </font></font></font><a href="mailto:nachname@zoobe.com" target="_blank"><font color="#808080"><font face="Tahoma, serif"><font style="font-size:7pt" size="1">drabner@zoobe.com</font></font></font></a><font color="#7f7f7f"><font face="Tahoma, serif"><font style="font-size:7pt" size="1"><span style="text-decoration:none">
</span></font></font></font><font color="#666666"><font face="Tahoma, serif"><font style="font-size:7pt" size="1">l  mob:
0172 7017640<br></font></font></font></font></font></p>
<p style="margin-top:0.05cm;margin-bottom:0.05cm;line-height:100%">
<br><br>
</p>
<p style="margin-top:0.05cm;margin-bottom:0.05cm;line-height:100%">
<font color="#666666"> <font face="Times New Roman, serif"><font size="3"><font face="Tahoma, serif"><font style="font-size:7pt" size="1">geschäftsführer:
lenard f. krawinkel</font></font></font></font></font></p>
<p style="margin-top:0.05cm;margin-bottom:0.05cm;line-height:100%">
<font color="#666666"> <font face="Times New Roman, serif"><font size="3"><font face="Tahoma, serif"><font style="font-size:7pt" size="1">tel:
+49 30. 288 838 88 l site: </font></font></font><a href="http://www.zoobe.com/" target="_blank"><font color="#808080"><font face="Tahoma, serif"><font style="font-size:7pt" size="1"><u>www.zoobe.com</u></font></font></font></a><font color="#666666"><font face="Tahoma, serif"><font style="font-size:7pt" size="1"> </font></font></font></font></font><font face="Times New Roman"><font size="3"><font color="#666666"><font face="Tahoma"><font style="font-size:7pt" size="1">׀
</font></font></font></font></font><font face="Times New Roman, serif"><font size="3"><font color="#666666"><font face="Tahoma, serif"><font style="font-size:7pt" size="1">email: </font></font></font><a href="mailto:info@zoobe.com" target="_blank"><font color="#808080"><font face="Tahoma, serif"><font style="font-size:7pt" size="1"><u>info@zoobe.com</u></font></font></font></a><font color="#808080"><font face="Tahoma, serif"><font> </font></font></font></font></font></p>

<p style="margin-top:0.05cm;margin-bottom:0.05cm;line-height:100%">
<br><br>
</p>
<p style="margin-top:0.05cm;margin-bottom:0.05cm;line-height:100%">
<font color="#666666"> <font face="Times New Roman, serif"><font size="3"><font face="Tahoma, serif"><font style="font-size:7pt" size="1">amtsgericht
charlottenburg, berlin </font></font></font></font></font><font face="Times New Roman"><font size="3"><font color="#666666"><font face="Tahoma"><font style="font-size:7pt" size="1">׀
</font></font></font></font></font><font face="Times New Roman, serif"><font size="3"><font color="#666666"><font face="Tahoma, serif"><font style="font-size:7pt" size="1">hrb-nr.
11 42 79 b</font></font></font></font></font><br></p>
<br>
<div style id="__af745f8f43-e961-4b88-8424-80b67790c964__"></div>