在C ++程序中使用libav-aacenc和libav

时间:2013-01-14 15:46:31

标签: c++ windows encoding aac libav

我正在创建一个程序来记录和编码这个记录到aac.I为linux和libfaac创建了一个完成这项工作的函数。现在我需要为windows制作这个程序。我知道我需要使用libvo-aacenc,我不知道在我的代码中要改变什么。你能告诉我该怎么办?这是我的代码。

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("","",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);
}

0 个答案:

没有答案