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

Denis Gottardello info at denisgottardello.it
Sat May 9 09:15:42 EEST 2020

Many thanks for your reply, this problem is already resolved. I can watch and listen a rtsp 
Can you help me with this one?
[Libav-user] How to fill an AVFrame with audio data
07.05.2020 09:11

In data venerdì 8 maggio 2020 23:22:49 CEST, Richard Hussong ha scritto:

On Sat, May 2, 2020 at 2:20 AM Denis Gottardello <info at denisgottardello.it[1]> wrote:

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?

I'm sorry - I don't know anything about storing or playing raw S16 files. I don't see 
anything obviously wrong with your code. You could try doing this:
ffmpeg -i input -f s16le -c:a pcm_s16le output.raw
That will give you a raw S16 file. Feed that file into your program, and see how the output 
differs from the input. That should give some clue to what is wrong.
*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, 
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, 
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_> wrote:

Hi, I need to extract audio packets from an rtsp stream and to listen them with a Qt 
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 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20200509/160830a4/attachment.html>

More information about the Libav-user mailing list