Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize ffmpeg Audio playback by Qt

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "Qt how to achieve ffmpeg audio playback", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Qt how to achieve ffmpeg audio playback" this article.

I. Preface

Previously, the audio was decoded with ffmpeg, but only the storage part was done, such as storing it into an aac file. In the early stage of playback, sdl was used to play the audio. Since Qt5 provided QAudioOutput to play the input audio data, it is more convenient. The decoded audio data can be written directly and can be played. This reduces the cost of learning sdl, and is more integrated with Qt, without the need for additional third-party libraries. A decoded video is actually a piece of picture data, which can be drawn directly with QPainter or displayed by QOpenGlWidget through GPU, and the decoded audio is played with QAudioOutput. This is good news for many beginners.

The general process of audio playback is as follows:

Initialize the format QAudioFormat and set the corresponding properties.

Initialize an QAudioOutput audio playback object.

Give the playback device after QAudioOutput startup to QIODevice.

Initialize SwrContext to convert audio data after opening the audio stream.

After decoding the audio data in a loop, call swr_convert to convert the audio data.

Write the converted audio data directly to QIODevice.

II. Functional features

Multi-thread real-time playback video stream + local video + USB camera and so on.

Support windows+linux+mac, support ffmpeg3 and ffmpeg4, support 32-bit and 64-bit.

Multi-thread display image, do not card the main interface.

Automatically reconnect the webcam.

The border size can be set, that is, the offset and the border color.

You can set whether to draw OSD tags, that is, label text or pictures and label locations.

There are two OSD locations and styles that can be set.

You can set whether to save to a file and the file name.

You can drag the file directly to the ffmpegwidget control to play.

Supports h365 video streams + rtmp and other common video streams.

Playback can be paused and resumed.

Support for storing individual video files and storing video files at regular intervals.

Customize the top suspension bar and send click signal notification, which can be set whether it is enabled or not.

Can set screen stretch fill or proportional fill.

Programmable decoding is speed priority, quality priority and balanced processing.

You can take screenshots (original pictures) and screenshots of the video.

Video file storage supports naked streaming and MP4 files.

Qsv, dxva2, d3d11va and other hard decoding are supported.

Support opengl to draw video data with very low CPU usage.

Support embedded linux, cross-compilation can be done.

Third, effect picture

IV. Related sites

Domestic site: https://gitee.com/feiyangqingyun/QWidgetDemo

International site: https://github.com/feiyangqingyun/QWidgetDemo

Personal homepage: https://blog.csdn.net/feiyangqingyun

Zhihu homepage: https://www.zhihu.com/people/feiyangqingyun/

Experience address: https://blog.csdn.net/feiyangqingyun/article/details/97565652

5. Core code void FFmpegThread::initAudioDevice (int sampleRate, int sampleSize, int channelCount) {# if (QT_VERSION > QT_VERSION_CHECK) QAudioFormat format; format.setCodec ("audio/pcm"); format.setSampleRate (sampleRate); format.setSampleSize (sampleSize * 8); format.setChannelCount (channelCount); format.setSampleType (QAudioFormat::SignedInt); format.setByteOrder (QAudioFormat::LittleEndian); QAudioDeviceInfo info (QAudioDeviceInfo::defaultOutputDevice ()) AudioDeviceOk = info.isFormatSupported (format); if (audioDeviceOk) {audioOutput = new QAudioOutput (format); audioDevice = audioOutput- > start ();} else {qDebug () stop (); audioOutput- > deleteLater (); # endif} void FFmpegThread::decodeAudio () {/ / decode audio if (! playAudio) {return;} / / Save audio stream data to file saveFileAac () / / do not decode if (! audioDeviceOk) {return;} / / decode audio streams frameFinish = avcodec_decode_audio4 (audioCtx, audioFrame, & frameFinish, tempPacket); if (frameFinish) {int result = swr_convert (audioSwrCtx, & audioData, audioFrame- > nb_samples, (const uint8_t * *) audioFrame- > data, audioFrame- > nb_samples) If (result) {int outsize = av_samples_get_buffer_size (NULL, audioCtx- > channels, audioFrame- > nb_samples, AV_SAMPLE_FMT_S16, 0); audioDevice- > write ((char *) audioData, outsize);} above is all the content of this article "how to achieve ffmpeg Audio playback by Qt". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report