Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Record a video from a webcam with Qt5

I have been trying to get this to work for 2 days now and I am desperate. Basically I want to record a video with my webcam using Qt5. I got so far that I can get a widget to display what the webcam is seeing, but for some reason whenever I try to record it with the QMediaRecorder class it doesn't save anything to the outputLocation.

The output file is created but contains 0 bytes. I have tried playing around with the settings for the video codec, but still no luck. I would think that simple code like this would work:

    QCamera *camera = new QCamera(QCameraInfo::availableCameras().at(0));
    QCameraViewfinder *viewFinder = new QCameraViewfinder(this);
    camera->setViewfinder(viewFinder);
    ui->verticalLayout->addWidget(viewFinder);

    recorder = new QMediaRecorder(camera);
    recorder->setOutputLocation(QUrl(QString("/home/user/test.mp4"))); // removed my name

    camera->setCaptureMode(QCamera::CaptureVideo);
    camera->start();
    recorder->record();

I expected this to be basically it for simple recording to a file. I stopped the recording in the destructor. So, the question is, why is this not working?

Thanks in advance :)

like image 520
blackwolf123333 Avatar asked Oct 01 '22 07:10

blackwolf123333


1 Answers

If you are on a Windows platform this issue is simply because Qt does not support video recording for Windows. Take a look at this http://doc.qt.io/qt-5/qtmultimedia-windows.html

A work around is to use this 3rd-party library https://github.com/kibsoft/QtMEL I hope Qt does something about this soon. Good luck!

like image 76
Fortune Avatar answered Oct 15 '22 21:10

Fortune