Record a video from a webcam with Qt5

2019-03-21 08:23发布

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

1条回答
ゆ 、 Hurt°
2楼-- · 2019-03-21 08:45

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!

查看更多
登录 后发表回答