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

Richard Hussong rhussong at westpond.com
Sat May 9 00:22:49 EEST 2020


On Sat, May 2, 2020 at 2:20 AM Denis Gottardello <info at denisgottardello.it>
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) {
>
> swr_free(&pSwrContext);
>
> 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);
>
> }
>
> }
>
> av_freep(&buffer);
>
> }
>
> }
>
>
>
> In data venerdì 1 maggio 2020 21:53:36 CEST, Richard Hussong ha scritto:
>
>
>
> 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 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.";
>
> avresample_free(&resample_context_);
>
> return;
>
> }
>
>
>
> 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, and there is a usage
> example at
> https://ffmpeg.org/doxygen/trunk/resampling_audio_8c-example.html.
>
>
>
> Can someone suggest me a way from
>
>
>
> while (av_read_frame(pAVFormatContext, &pAVPacket)>= 0 && DoStart) {
>
> if (pAVPacket.stream_index== StreamAudio) {
>
> ...
>
> ..
>
>
>
> ??
>
> Many thanks
>
>
> --
>
> +39.347.4070897
>
> http://www.labcsp.com
>
> http://www.denisgottardello.it
>
> GMT+1
>
> Skype: mrdebug
>
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/libav-user
>
> To unsubscribe, visit link above, or email
> libav-user-request at ffmpeg.org with subject "unsubscribe".
>
>
>
> --
>
> +39.347.4070897
>
> http://www.labcsp.com
>
> http://www.denisgottardello.it
>
> GMT+1
>
> Skype: mrdebug
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/libav-user
>
> To unsubscribe, visit link above, or email
> libav-user-request at ffmpeg.org with subject "unsubscribe".
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20200508/a086c7e7/attachment.html>


More information about the Libav-user mailing list