简体   繁体   中英

How to generate video file from QImage sequence using QMediaRecorder in Qt5 C++

Basically what I want is to encode a video using QMediaRecorder by supplying as a source a sequence of QImage s that I generate in custom code at run-time.

So far I have found no easy way to do this, and everything points at the solution where I have to somehow implement my own subclass of QMediaService and/or QMediaControl that takes QImage as input, register them and somehow make QMediaRecorder use them. But according to this page in the official Qt documentation on the subject, this is a road less traveled and I am on my own:

In general, implementing a QMediaService is outside of the scope of this documentation and support on the relevant mailing lists or IRC channels should be sought.

I am with this post hoping someone who possesses this knowledge may shed some light on how this would be done. I think documenting this set of features will open up many useful possibilities for the users of Qt5.

Update 2020-06-16: It has been almost 4 years and still not a single answer. I will put a bounty on this question and accept the best answer with working example code for recent Qt5.

It is quite simple to do this directly with ffmpeg. You can either save the images on disk then use an ffmpeg filter from the command line, via QProcess. You can also create a video stream in the code, therefore avoiding the loss of time and performance due to the images being saved on the disk, save the images in that stream

You can try libqtavi . It is a wrapper around the libgwavi. API looking simple and good integration with Qt classes. But only support MJPG codec and avi format and its bigger output size than mpeg/mp4, hevc/mkv...

QAviWriter writer("demo.avi", QSize(800, 600), 24, "MJPG");// set framerate to 24 fps and 'MJPG' codec 
writer.setAudioFileName("audio.wav"); // set audio track
writer.open();
writer.addFrame(QImage("file.png"));
//...add all other video frames here
writer.close();

Directly using ffmpeg to accomplish this is really easy. Either use QProcess or store the photos to disc before using a ffmpeg filter from the command line. You can alternatively establish a video stream in the code and save the photos there to avoid the performance and time penalties associated with saving the images to the disc.

It looks like you are trying to use QMediaRecorder to encode a video from a sequence of QImages. Unfortunately, QMediaRecorder does not support this directly. As the documentation you mentioned notes, implementing a custom QMediaService or QMediaControl would be required to achieve this.

Implementing a custom QMediaService involves creating a class that derives from QMediaService and overrides a number of virtual methods to provide the desired functionality. You would then need to register this service with Qt so that it can be used by QMediaRecorder.

Similarly, implementing a custom QMediaControl involves creating a class that derives from QMediaControl and overrides a number of virtual methods to provide the desired functionality. You would then need to create a QMediaService that uses this control, and register the service with Qt as described above.

It is definitely not a trivial task to implement a custom QMediaService or QMediaControl, and it is beyond the scope of this answer to provide a complete tutorial on how to do so. However, there are a few resources that may be helpful in getting started:

The Qt documentation on QMediaService and QMediaControl: https://doc.qt.io/qt-5/qmediaservice.html and https://doc.qt.io/qt-5/qmediacontrol.html

The Qt Multimedia examples provided with the Qt SDK, which include some examples of custom media services and controls: https://doc.qt.io/qt-5/qtmultimedia-multimediawidgets-example.html and https://doc.qt.io/qt-5/qtmultimedia-customaudiooutput-example.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM