[Libav-user] How to extract audio from an rtsp stream

Denis Gottardello info at denisgottardello.it
Sat May 2 09:20:34 EEST 2020

May thanks for your reply. Now I can extract the audio track from an avi file and to store it in a s16 file.
There is a problem, the audio duration is two times than the original track so I can listen but not correctly. There is the error for you?

    *SwrContext* *pSwrContext= swr_alloc();
    if (!pSwrContext) emit UpdateLog("swr_alloc Error!!!");
    else {
        av_opt_set_int(pSwrContext, "in_channel_layout", av_get_default_channel_layout(pAVCodecContextAudio->channels), 0);
        av_opt_set_int(pSwrContext, "in_sample_fmt", pAVCodecContextAudio->sample_fmt, 0);
        av_opt_set_int(pSwrContext, "in_sample_rate", pAVCodecContextAudio->sample_rate, 0);

        av_opt_set_int(pSwrContext, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
        av_opt_set_int(pSwrContext, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
        av_opt_set_int(pSwrContext, "out_sample_rate", 44100, 0);
        if (*swr_init*(pSwrContext)< 0) {
            pSwrContext= nullptr;

    int gotFrameAudio= 0;
    int Ret= *avcodec_decode_audio4*(pAVCodecContextAudio, pAVFrame, &gotFrameAudio, &pAVPacket);
    if (Ret< 0) break;
    if (gotFrameAudio) {
        uint8_t* buffer;
        Ret= *av_samples_alloc*(static_cast<uint8_t**>(&buffer), nullptr, AV_CH_LAYOUT_STEREO, pAVFrame->nb_samples, AV_SAMPLE_FMT_DBL, 0);
        if (Ret< 0) emit UpdateLog("av_samples_alloc Error!!!");
        else {
            Ret= *swr_convert*(pSwrContext, static_cast<uint8_t**>(&buffer), pAVFrame->nb_samples, (const uint8_t**)pAVFrame->data, pAVFrame->nb_samples);
            if (Ret< 0) emit UpdateLog("swr_convert Error!!!");
            else {
                Ret= *av_samples_get_buffer_size*(nullptr, pAVCodecContextAudio->channels, Ret, AV_SAMPLE_FMT_S16, 1);
                if (Ret< 0) emit UpdateLog("av_samples_get_buffer_size Error!!!");
                else {
                    *fwrite(buffer, 1, static_cast<size_t>(Ret), dst_file);*

On Thu, Apr 30, 2020 at 9:27 AM Denis Gottardello <info at denisgottardello.it[1]> wrote:

Hi, I need to extract audio packets from an rtsp stream and to listen them with a Qt program.
The examples are based on *avcodec_decode_audio4* function but it is deprecated.
Now I already have an audio packet. Now I have to trascode it in a format that I can manage with Qt, something like
*AVAudioResampleContext** resample_context_ = NULL;
av_opt_set_int(resample_context_, "in_channel_layout", av_get_default_channel_layout(codec_context_->channels), 0);
av_opt_set_int(resample_context_, "out_channel_layout", av_get_default_channel_layout(outputFormat_.channels), 0);
av_opt_set_int(resample_context_, "in_sample_rate", codec_context_->sample_rate, 0);
av_opt_set_int(resample_context_, "out_sample_rate", outputFormat_.rate, 0);
av_opt_set_int(resample_context_, "in_sample_fmt", codec_context_->sample_fmt, 0);
av_opt_set_int(resample_context_, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
if (avresample_open(resample_context_) < 0) {
qDebug() << "Could not open resample context.";
but *AVAudioResampleContext* but it is deprecated too.

The entire libavresample library is deprecated. You should use libswresample instead. Library documentation is at https://ffmpeg.org/doxygen/trunk/group__lswr.html[2], and there is a usage example at https://ffmpeg.org/doxygen/trunk/resampling_audio_8c-example.html[3].
Can someone suggest me a way from 
while (av_read_frame(pAVFormatContext, &pAVPacket)>= 0 && DoStart) {
if (pAVPacket.stream_index== StreamAudio) {
Many thanks

Skype: mrdebug
_______________________________________________Libav-user mailing list

Libav-user at ffmpeg.org[6]
libav-user-request at ffmpeg.org[8] with subject "unsubscribe".

Skype: mrdebug

[1] mailto:info at denisgottardello.it
[2] https://ffmpeg.org/doxygen/trunk/group__lswr.html
[3] https://ffmpeg.org/doxygen/trunk/resampling_audio_8c-example.html
[4] http://www.labcsp.com
[5] http://www.denisgottardello.it
[6] mailto:Libav-user at ffmpeg.org
[7] https://ffmpeg.org/mailman/listinfo/libav-user
[8] mailto:libav-user-request at ffmpeg.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20200502/b9e8d6ee/attachment.html>

More information about the Libav-user mailing list