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

What is the method of decoding ffmpeg for Qt USB camera

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

Share

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

This article mainly introduces "what is the Qt USB camera decoding ffmpeg method". In the daily operation, I believe that many people have doubts about what the Qt USB camera decoding ffmpeg method is. The editor consulted all kinds of data and sorted out a simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "what is the Qt USB camera decoding ffmpeg method?" Next, please follow the editor to study!

I. Preface

Using ffmpeg to deal with USB cameras was done some time ago when studying the video surveillance ffmpeg kernel. Since ffmpeg such a powerful library can parse all kinds of audio and video, I think it should not be difficult to deal with a local USB camera. If there is a lot of search, of course, it is mainly because there is a project application that needs to use ffmpeg to deal with local USB cameras and need to get each image for intelligent analysis. It is not easy to deal with the camera class that comes with Qt, so it just makes the processing flow of ffmpeg clear. Just use ffmpeg to deal with it directly. With such a powerful decoding library, it theoretically supports a variety of USB cameras. The local USB camera does not need hard decoding, and the video stream coding type is AV_CODEC_ID_RAWVIDEO pixel format and AV_PIX_FMT_YUYV422 can be displayed directly without decoding operation.

Ffmpeg scenario processing flow:

Introduce the avdevice.h header file and call avdevice_register_all (); register the local device processing.

Call av_dict_set to set parameters such as resolution (video_size), frame rate (framerate) and so on.

Call av_find_input_format to set the input format.

Call avformat_open_input to open the file.

Call av_find_best_stream to find the video stream address.

Call avcodec_find_decoder to set the video decoder.

Call av_read_frame loop decoding to read frame data.

Call avcodec_send_packet avcodec_receive_frame to decode the data.

After processing, call av_frame_free avcodec_close and other resources to release resources.

II. Functional features

At the same time, it supports real-time acquisition of USB camera on windows, linux and embedded linux.

Support multi-channel USB camera multi-thread real-time acquisition.

On embedded linux devices, automatically find USB device files and load them.

The device file name can be set manually and loaded according to the device file set manually.

The face recognition interface is supported on the embedded linux device to draw the human face frame in real time.

With open, pause, resume, close, screenshot and other general functions.

Two OSD tags can be set, such as text, color, font size, position, etc.

It can be used as a video surveillance system.

Third, effect picture

4. The core code void FFmpegThread::initOption () {/ / specifies various parameters such as detection time / timeout / maximum delay before opening the code stream / / sets the cache size. 1080p can increase the value of av_dict_set (& options, "buffer_size", "8192000", 0) / / Open it in tcp mode, and replace tcp with udp av_dict_set (& options, "rtsp_transport", transport.toUtf8 (). ConstData (), 0) if opened in udp mode; / / set the timeout disconnection time (in microseconds, 3000000 means 3 seconds av_dict_set (& options, "stimeout", "3000000", 0) / / set the maximum delay (in microseconds). 1000000 means 1 second av_dict_set (& options, "max_delay", "1000000", 0); / / automatically open the number of threads av_dict_set (& options, "threads", "auto", 0) / / set parameters if (isUsbCamera) {/ / set input format / / av_dict_set (& options, "input_format", "mjpeg", 0) for USB camera separately; / / set resolution QString size = QString ("% 1x%2") .arg (videoWidth) .arg (videoHeight) Av_dict_set (& options, "video_size", size.toUtf8 () .constData (), 0); / / set frame rate av_dict_set (& options, "framerate", "25", 0) } / / Local USB camera does not need hard decoding, forced to callback operation and no hard decoding / / Video stream encoding type is AV_CODEC_ID_RAWVIDEO pixel format AV_PIX_FMT_YUYV422 can display if (isUsbCamera) {callback = true; hardware = "none" without decoding operation;} / / if opengl is not enabled, it is forced to change to callback # ifndef opengl callback = true # endif / / rtmp video stream is forced to be stored as h364 naked stream. There is still a problem with storing it to mp4. If (url.startsWith ("rtmp", Qt::CaseInsensitive)) {saveMp4 = false;}} bool FFmpegThread::initInput () {/ / instantiation format processing context formatCtx = avformat_alloc_context () / / set timeout callback. If the address does not exist or the network is not good, it will be stuck for a long time formatCtx- > interrupt_callback.callback = AVInterruptCallBackFun; formatCtx- > interrupt_callback.opaque = this; / / there must be a tryOpen flag bit to control the timeout callback. It is up to him to control whether to continue blocking and wait for tryOpen = false to be opened. / / determine whether it is a local device (video= device name string). Open it in a different way: QByteArray urlData = url.toUtf8 (); AVInputFormat * ifmt = NULL; if (isUsbCamera) {# if defined (Q_OS_WIN) ifmt = av_find_input_format ("dshow"); # elif defined (Q_OS_LINUX) / / ifmt = av_find_input_format ("v4l2") Ifmt = av_find_input_format ("video4linux2"); # elif defined (Q_OS_MAC) ifmt = av_find_input_format ("avfoundation"); # endif} int result = avformat_open_input (& formatCtx, urlData.data (), ifmt, & options); tryOpen = true; if (result < 0) {qDebug ()

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