[Libav-user] How to decode an mp3 and grab a specific channel ?

feng zh feng.zh.zq at gmail.com
Sat Jan 7 11:43:27 CET 2012


Hello, all. I'm a student in China majoring in Computer Science, and I'm
working on my final year project.
In my project, I want to make a software to compare sounds. So, I must
write C codes to decode mp3, grab wave data and implement some algorithm.
I've search a lot of websites and try some method, but my problem is not

Now, I have some mp3 that record sounds. All of them are stereo which
contains 2 channels. I can use ffmpeg command line to extract each channels
into 2 mono files which contains only 1 channel. But this is a lot of

I've read the tutorials on ffmpeg.org and know how to decode mp3.   Here's
some codes.

while ( av_read_frame( pFmtCtx, &packet ) == 0 ) {

if ( packet.stream_index != audioStream ) {
// all packet are audio packet
memset( sample, 0, sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE );
// printf( "%d\n", packet.size );
if ( avcodec_decode_audio3( pCodecCtx, sample, &sample_size, &packet ) >= 0
printf( "%d %d %d\n", ++i, packet.size, sample_size );
av_free_packet( &packet );

This is what I can get from the two tutorials on ffmpeg.org...  But I don't
know the meanings of the data in the sample buffer. Are they 16 bit PCM??

Further more, there are two channel in the mp3, and I don't know how to
extract the data of each channel and decode it into different buffer ??

Can anyone give me some sample codes in C language ???

Thanks to all. I need your helps.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20120107/4aabee1e/attachment.html>

More information about the Libav-user mailing list