The program below compiles and runs without segfaulting however it
doesn't seem to do anything.
#include <iostream>
extern "C" {
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
}
using namespace std;
int main(int argc, char **argv)
{
if (argc <= 1) {
cout << "usage audio_convert <fielname>\n" << endl;
}
// initialise libavformat /libavcodec
avcodec_register_all();
av_register_all();
// data structures
AVFormatContext *pFormatCtx;
// open input file
av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL);
// get stream info
av_find_stream_info(pFormatCtx);
// dump the format
dump_format(pFormatCtx, 0, argv[1], false);
// get first audio stream
AVCodecContext *pCodecCtx;
int audioStream=-1;
for(int i=0; i<pFormatCtx->nb_streams; i++) {
pCodecCtx=pFormatCtx->streams[i]->codec;
if(pCodecCtx->codec_type == AVMEDIA_TYPE_AUDIO) {
audioStream=i;
break;
}
}
if(audioStream==-1) {
cout << "No audio stream found" << endl;
}
// try to find it's codec and open it
AVCodec *pCodec;
pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
if(pCodec==NULL) {
cout << "can't find codec" << endl;
}
avcodec_open(pCodecCtx,pCodec);
cout << "got the codec for the input stream" << endl;
// now try to get packets of samples from the input stream...i.e.
decoding
// allocate a buffer for holding samples
int buf_size=100000000;
int16_t *audio_buffer=new int16_t[buf_size];
// keep reading in frames till we've processed them all
AVPacket pkt;
int frame_cnt=0;
int frame_flag;
while(frame_flag=av_read_frame(pFormatCtx, &pkt)>=0) {
cout << "got frame " << frame_cnt++ <<" : " << frame_flag << endl;
// now to decode the frame into our samples audio_buffer
int num_bytes=avcodec_decode_audio3(pCodecCtx, audio_buffer,
&buf_size, &pkt);
cout << "decoded " << num_bytes << " bytes" << endl;
}
// cleanup
av_close_input_file(pFormatCtx);
return 0;
}
It just prints...
[mp3 @ 0x101008000]max_analyze_duration reached
[mp3 @ 0x101008000]Estimating duration from bitrate, this may be inaccurate
Input #0, mp3, from 'music.mp3':
Metadata:
TCON : DANCE MUSIC,2000S$DRAMA,ACTION$DRAMA,GENERAL
RECOMMENDED CDS$FILM STYLES,ACTION$FILM STYLES,DETECTIVE /
SPY$DRAMA,URBAN$DANCE MUSIC,BREAKBEAT$DRAMA,INVESTIGATIVE / CRIME
SCENES$FILM STYLES,THRILLER
TIT3 : Car Crime - Car cops
TOPE : Knowler, Jeff(PRS)<BRUTON APM>
TCOM : Knowler, Jeff(PRS)<BRUTON APM>
TALB : THE BEST & WORST OF BRITAIN
TLEN : 149000
TPUB : ASCAP
TPE1 : BRU
TIT2 : Smash & Grab
TOAL : BTV_0001~2_033.01
TSSE : LAME 3.96.1 - Metadata by Soundminer Inc.
www.soundminer.com
TPE4 : <MAGIC><Encoder>Metadata enbedded by
Soundminer.</Encoder><kAudioFilePropertyDataFormat>2 ch, 48000 Hz,
'lpcm' (0x0000000C)16-bit
</kAudioFilePropertyDataFormat><Channels
type="NSNumber">2</Channels><BWDate>2007-07-13</BWDate><Category>DANCE
MUSIC,2000S$DRAMA,ACTION$DRAMA,GENERAL RECOMMENDED CDS$FILM
STYLES,ACTION$FILM STYLES,DETECTIVE / SPY$DRAMA,URBAN$DANCE
MUSIC,BREAKBEAT$DRAMA,INVESTIGATIVE / CRIME SCENES$FILM
STYLES,THRILLER</Category><BWCodingHistory>Metadata added by Soundminer.</B
Duration: 00:02:33.24, start: 0.000000, bitrate: 320 kb/s
Stream #0.0: Audio: mp3, 44100 Hz, 2 channels, s16, 320 kb/s
[mp3 @ 0x101009200]buffer smaller than AVCODEC_MAX_AUDIO_FRAME_SIZE
got the codec for the input stream
got frame 0 : 1
decoded 1044 bytes
got frame 1 : 1
decoded -1 bytes
got frame 2 : 1
decoded -1 bytes
got frame 3 : 1
decoded -1 bytes
got frame 4 : 1
decoded -1 bytes
got frame 5 : 1
any idea what the problem is ?
_______________________________________________
libav-user mailing list
[email protected]
https://lists.mplayerhq.hu/mailman/listinfo/libav-user