[Libav-user] Encoding live audiopackets

Ruurd Adema ruurdadema at me.com
Tue Jul 14 20:49:03 CEST 2015


I'm trying to write live incoming audiopackets into a mov file with AAC encoding using the FFmpeg api.

When using no encoding (AV_CODEC_ID_PCM_S16LE) it works well, when using AAC encoding (AV_CODEC_ID_AAC) it fails. The resulting audiofile plays too fast and sounds distorted.

I’m new to the FFmpeg api, (and quite a beginner in programming anyway), so big chance I forgot something or doing something wrong. Is there anyone willing to help me with this one?

audiopacket_sample_count  = audiopacket->GetSampleFrameCount();
audiopacket_channel_count = decklink_config()->audio_channel_count;
audiopacket_size          = audiopacket_sample_count * (decklink_config()->audio_sampletype/8) * audiopacket_channel_count;

audiopacket->GetBytes(&audiopacket_data);

av_init_packet(&pkt);    

if (AUDIO_TYPE == AV_CODEC_ID_PCM_S16LE)
{
    audiopacket->GetPacketTime(&audio_pts, audio_stream->time_base.den);

    pkt.pts          = audio_pts;
    pkt.dts          = pkt.pts; 
    pkt.flags       |= AV_PKT_FLAG_KEY;                 
    pkt.stream_index = audio_stream->index;
    pkt.data         = (uint8_t *)audiopacket_data;
    pkt.size         = audiopacket_size;

    av_interleaved_write_frame(output_fmt_ctx, &pkt);
} 
else if (AUDIO_TYPE == AV_CODEC_ID_AAC)
{
    frame = av_frame_alloc();
    frame->format = audio_stream->codec->sample_fmt;
    frame->channel_layout = audio_stream->codec->channel_layout;
    frame->sample_rate = audio_stream->codec->sample_rate;
    frame->nb_samples = audiopacket_sample_count;

    audiopacket->GetPacketTime(&audio_pts, audio_stream->time_base.den);

    frame->pts = audio_pts;

    if (avcodec_fill_audio_frame(frame, audiopacket_channel_count, audio_stream->codec->sample_fmt, (const uint8_t *)audiopacket_data, audiopacket_size, 0) < 0)
    {
        fprintf(stderr, "[ERROR] Filling audioframe failed!\n");
        exit(-1);
    }

    if (avcodec_encode_audio2(audio_stream->codec, &pkt, frame, &got_packet) != 0)
    {
        fprintf(stderr, "[ERROR] Encoding audio failed\n");
    }

    if (got_packet) 
    {
        pkt.stream_index = audio_stream->index;
        pkt.flags       |= AV_PKT_FLAG_KEY; 

        av_interleaved_write_frame(output_fmt_ctx, &pkt);
    }
    av_frame_free(&frame); 
}
av_free_packet(&pkt);


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20150714/1d25dce8/attachment.html>


More information about the Libav-user mailing list