[Ffmpeg-devel] Using libavformat and libavcodec(help me Martin Böhme)

Måns Rullgård mru
Tue May 31 16:30:58 CEST 2005


Chang Min Jeon <jcm1981 at gmail.com> writes:

> hello, I'm student in South Korea.

KAIST, by any chance?

> I read Martin B?hme's article about using libavcodec and libavformat
> in ffmpeg doc.
>
> So I can read video frame from video file, Wow amazing.~
>
> Now I'm trying to read audio stream.
>
> How to read raw audio stream from video file ?
>
> //find the first audio stream.
>
>  m_nAudioStream = -1;
>
>  for(j=0; j<m_pFormatCtx->nb_streams; j++)
>   if(m_pFormatCtx->streams[j]->codec.codec_type == CODEC_TYPE_AUDIO)
>   {
>    m_nAudioStream =j;
>    break;
>   }
>
> // didn't find a video stream
>  if(m_nAudioStream==-1)
>   return -1;
>
>  // get a pointer to the codec context for the audio stream.
>  m_pAudioCtx=&m_pFormatCtx->streams[m_nAudioStream]->codec;
>
> // fine the decoder for the audio stream
>  pAudioCodec=avcodec_find_decoder(m_pAudioCtx->codec_id);
>  if(pAudioCodec==NULL)
>   return -1;
>
> // open audio codec
>
>  if(avcodec_open(m_pAudioCtx, pAudioCodec) <0)
>   return -1;

Looking good so far.

AVPacket packet;

/* you must already be doing this for the video */
if(av_read_frame(m_pFormatCtx, &packet) < 0)
    /* error */

Check packet.stream_index to see where it belongs.  If you can decode
video you're already doing it right, just add some code for decoding
audio:

int16_t *audio_out;
int outsize;
int decoded;

audio_out = malloc(something reasonable);

decoded = avcodec_decode_audio(m_pAudioCtx, audio_out, &outsize,
                               packet.data, packet.size);
if(decoded < 0)
    /* error */

/* do something with decoded audio */

avcodec_decode_audio() returns the number of encoded bytes that have
been used.  If this value is smaller than the packet size, you'll have
to keep calling it until the packet has been used up.

It's quite similar to decoding video.

-- 
M?ns Rullg?rd
mru at inprovide.com





More information about the ffmpeg-devel mailing list