Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void encodeAac( const char *infilename,const char *filename)
- {
- AVCodec *codec;
- AVCodecContext *c = NULL;
- int frame_size, i, j, out_size, outbuf_size;
- FILE *f,*fin;
- SAMPLE *samples;
- float t, tincr;
- uint8_t *outbuf;
- avcodec_register_all(); //Load all codecs
- av_register_all();
- codec = avcodec_find_encoder(AV_CODEC_ID_AAC); //Search for AAC codec
- if (!codec) {
- error("Codec not found");
- }
- c = avcodec_alloc_context();
- c->bit_rate = 64000;
- c->sample_fmt = AV_SAMPLE_FMT_S16;
- c->sample_rate = SAMPLE_RATE;
- c->channels = NUM_CHANNELS;
- c->time_base.num= 1;
- c->time_base.den= SAMPLE_RATE;
- c->profile= FF_PROFILE_AAC_MAIN;
- if (avcodec_open(c, codec) < 0) {
- error(add("couldn","",avcodec_open(c, codec)).c_str());
- exit(1);
- }
- f = fopen(filename, "wb");
- fin=fopen(infilename,"rb");
- if (!fin) {
- error("could not open temporary file");
- }
- if (!f) {
- error("could not open output file");
- }
- std::cout << c->frame_size*c->channels << std::endl;
- samples = new SAMPLE[c->frame_size*c->channels];
- outbuf = new uint8_t[FRAMES_PER_BUFFER * NUM_CHANNELS];
- while(fread(samples,sizeof(SAMPLE),c->frame_size*c->channels,fin)){
- out_size=avcodec_encode_audio(c,outbuf,FRAMES_PER_BUFFER * NUM_CHANNELS,samples);
- fwrite(outbuf,sizeof(uint8_t),out_size,f);
- }
- for(int i=1;i<=4;i++){ //For buffer flushing
- out_size=avcodec_encode_audio(c,outbuf,FRAMES_PER_BUFFER * NUM_CHANNELS,NULL);
- fwrite(outbuf,sizeof(uint8_t),out_size,f);
- }
- fclose(f);
- delete outbuf;
- delete samples;
- avcodec_close(c);
- av_free(c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement