[Ffmpeg-devel] Captured buffer (PCM) to MPEG frame(s).. How to do this???

Paul paul.pham
Fri Jan 12 07:58:13 CET 2007

I capture (using directsound) my voice to a buffer, and i would like to encode every buffer (captured) into MPEG frame(s) and then write to output file.
Here are my code to encode a buffer (audio date with PCM format) to MPEG frame(s)
But it is not success, i donot know where/what i missing. So, can you help?

   //packet.data is a buffer that stored audio data (PCM format)
   samples = (short *) av_fast_realloc(samples, &samples_size, FFMAX(4096, AVCODEC_MAX_AUDIO_FRAME_SIZE));
   samples = (short *)packet.data;

   avcodec_decode_audio(pCodecCtx, samples, &frameFinished, packet.data, 4096);
   fifo_write(&fifo, (uint8_t *)samples, 4096, &fifo.wptr);

     int frame_bytes = c->frame_size * 2 * c->channels;
     fifo_read(&fifo, data_buf, frame_bytes, &fifo.rptr); 
       // encode the samples   
       pkt.size= avcodec_encode_audio(c, audio_out, 4096,(short *)data_buf);
       pkt.stream_index= audio_st->index;
       pkt.data= audio_out;
       pkt.flags |= PKT_FLAG_KEY;
       // write the compressed frame in the media file 
       if (av_interleaved_write_frame(oc, &pkt) != 0)
          fprintf(stderr, "Error while writing audio frame\n");
          return false;

i plan to write the output with 4 seconds of lenghts. But the output i receive is only one second and the data not same as i "said" into microphone.

More information about the ffmpeg-devel mailing list