简体   繁体   中英

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 :)

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!

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