[Libav-user] Converting PCM to AAC

Oosman Saeed oosman3 at yahoo.com
Wed Sep 28 07:49:55 EEST 2016

I have PCM data that I converted to FLT and I am trying to encode that to AAC. But the audio is twice as long as it should be and very bad. I can barely make it out.
This is my code below. audio_samples is an array of floats (between -1.0 and 1.0). nb_samples is number of floats in the array.
I do not get any errors and the audio is encoded and saved to file. But, like I said, it is twice as long and horrible. I will appreciate any help.
static void write_audio_frame2(AVFormatContext *oc, AVStream *st,                               int16_t *audio_samples, int nb_samples){    AVCodecContext *c;    AVPacket pkt = { 0 }; // data and size must be 0;    AVFrame *frame = av_frame_alloc();    int got_packet, ret;
    av_init_packet(&pkt);    c = st->codec;
    frame->format = st->codec->sample_fmt;    frame->nb_samples = nb_samples;    frame->channels = c->channels;    frame->channel_layout = c->channel_layout;    frame->sample_rate = c->sample_rate;    ret = avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,                             (uint8_t *)audio_samples,                             nb_samples *                             av_get_bytes_per_sample(c->sample_fmt) *                             c->channels, 1);
    if (ret < 0) {        fprintf(stderr, "Error filling audio frame\n");        exit(1);    }    ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);    if (ret < 0) {        fprintf(stderr, "Error encoding audio frame\n");        exit(1);    }
    if (!got_packet){        return;    }
    pkt.stream_index = st->index;
    /* Write the compressed frame to the media file. */    if (av_interleaved_write_frame(oc, &pkt) != 0) {        fprintf(stderr, "Error while writing audio frame\n");        exit(1);    }    av_frame_free(&frame);}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160928/6a8e4800/attachment.html>

More information about the Libav-user mailing list