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 Qt ffmpeg Video Storage

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

Share

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

This article mainly explains "how to achieve Qt ffmpeg video storage". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve Qt ffmpeg video storage".

I. Preface

The last article wrote that directly save the video stream to a file, although the naked stream file has certain advantages, after all, what most users need is not a naked stream but a MP4 video file, so they need to save the video stream as a MP4 file. After all, the player on the computer includes a default player, which can play MP4 files directly, but may not be able to play naked files. Naked files need to be installed with a K-Lite decoder. On ffmpeg decoding saved as a MP4 file, there are two processing methods, one is first saved as a naked stream, and then open a background thread, when the naked stream file is saved, automatically trigger H264 to MP4 command execution, you can also quickly complete the conversion, another method is directly decoded when saved as a MP4 file, both methods can, generally recommended the latter.

Process of saving to MP4 file:

Call avformat_alloc_output_context2 to open up a format context AVFormatContext to handle the video stream output.

Call avformat_new_stream to open up a video stream AVStream to output MP4 files.

Reset the various parameters of the output video stream.

Call avio_open to open the output file.

Call avformat_write_header to write the header ID.

Av_write_frame is called to write data to the file after cyclic decoding.

After the end, av_write_trailer is called to write the end identity.

Close decoding output, close files, and release resources

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

4. Core code void FFmpegThread::saveVideoMp4 (const QString & fileName) {QMutexLocker locker (& mutex); closeVideo (); if (videoStreamIndex)

< 0 || !isRtsp) { return; } //转换文件字符串 const char *filename = fileName.toStdString().data(); //开辟一个格式上下文用来处理视频流输出 avformat_alloc_output_context2(&formatOut, NULL, NULL, filename); //开辟一个视频流用来输出MP4文件 AVStream *streamOut = avformat_new_stream(formatOut, NULL); AVStream *streamIn = formatCtx->

Streams [videoStreamIndex]; / / reset various parameters of the output video stream AVCodecContext * codec = streamOut- > codec; codec- > bit_rate = 400000; codec- > codec_id = streamIn- > codec- > codec_id; codec- > codec_type = streamIn- > codec- > codec_type; codec- > time_base.num = streamIn- > time_base.num; codec- > time_base.den = streamIn- > time_base.den; codec- > width = streamIn- > codec- > width Codec- > height = streamIn- > codec- > height; codec- > pix_fmt = streamIn- > codec- > pix_fmt; codec- > flags = streamIn- > codec- > flags; codec- > flags | = AV_CODEC_FLAG_GLOBAL_HEADER; codec- > me_range = streamIn- > codec- > me_range; codec- > max_qdiff = streamIn- > codec- > max_qdiff; codec- > qmin = streamIn- > codec- > qmin; codec- > qmax = streamIn- > codec- > qmax; codec- > qcompress = streamIn- > codec- > qcompress / / Open the output file and write the header ID if (avio_open (& formatOut- > pb, filename, AVIO_FLAG_WRITE) > = 0) {if (avformat_write_header (formatOut, NULL) > = 0) {initSaveOk = true;}} void FFmpegThread::closeVideo () {if (! saveFile) {return } if (saveMp4) {if (formatOut! = NULL) {/ / write end flag av_write_trailer (formatOut); avcodec_close (formatOut- > streams [0]-> codec); av_freep (& formatOut- > streams [0]-> codec); av_freep (& formatOut- > streams [0]); avio_close (formatOut- > pb) Av_free (formatOut); initSaveOk = false; formatOut = NULL;}} else {if (fileVideo.isOpen ()) {fileVideo.close ();} if (fileAudio.isOpen ()) {fileAudio.close () } / / the decoded data can be directly written into the file av_write_frame (formatOut, videoPacket); thank you for reading, the above is the content of "how to achieve Qt ffmpeg video storage". After the study of this article, I believe you have a deeper understanding of how to achieve Qt ffmpeg video storage, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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