I've been searching everywhere for a solution to this and I've seen some threads here about this problem but without a solution.
I'm trying to record a video from two different webcams that I have on my computer. I am able to show the video for both of them but the problem is recording. When I try it nothing happens. No file is saved and the states also never change. Bellow I have attached the code that loads the cameras and the code that loads the QMediaRecorders. I also tried with just one of the cameras and it still doesn't work. The QT example "camera" does not work as well, the option for recording is always disabled.
void MainWindow::setCamera() {
foreach (const QCameraInfo &cameraInfo, QCameraInfo::availableCameras()) {
qDebug() << "Device Name: " + cameraInfo.deviceName();
qDebug() << "Device Description: " + cameraInfo.description();
if (cameraInfo.description() == "Integrated Camera")
camera1 = new QCamera(cameraInfo);
else if(cameraInfo.description() == "QuickCam for Notebooks Pro")
camera2 = new QCamera(cameraInfo);
}
camera1->setViewfinder(ui->camScreen1);
camera2->setViewfinder(ui->camScreen2);
camera1->setCaptureMode(QCamera::CaptureVideo);
camera2->setCaptureMode(QCamera::CaptureVideo);
camera1->start();
camera2->start();
qDebug() << camera1->status();
qDebug() << camera1->state();
qDebug() << camera1->error();
}
void MainWindow::setVideoEncoding() {
recorder1 = new QMediaRecorder(camera1);
recorder2 = new QMediaRecorder(camera2);
QVideoEncoderSettings settings = recorder1->videoSettings();
settings.setCodec("video/mpeg2");
settings.setQuality(QMultimedia::LowQuality);
settings.setResolution(640,480);
settings.setFrameRate(30.0);
recorder1->setVideoSettings(settings);
recorder2->setVideoSettings(settings);
recorder1->setMetaData(QMediaMetaData::Title, QVariant(QLatin1String("Record1")));
recorder2->setMetaData(QMediaMetaData::Title, QVariant(QLatin1String("Record2")));
recorder1->setOutputLocation(QUrl::fromLocalFile(QCoreApplication::applicationDirPath() + "/" + "testvide1o.mp4"));
recorder2->setOutputLocation(QUrl::fromLocalFile(QCoreApplication::applicationDirPath() + "/" + "testvideo2.mp4"));
qDebug() << recorder1->status();
qDebug() << recorder1->state();
qDebug() << recorder1->error();
}
When I press the record button:
void MainWindow::toggleRecord(bool startRecord) {
if(startRecord) {
recorder1->record();
recorder2->record();
}
else {
recorder1->stop();
recorder2->stop();
}
qDebug() << recorder1->state();
qDebug() << recorder1->status();
qDebug() << recorder1->error();
qDebug() << recorder2->state();
qDebug() << recorder2->status();
qDebug() << recorder2->error();
}
The state, status and error are as follows (every time I call them)
Camera:
State: QCamera::UnavailableStatus;
Status: QCamera::ActiveState;
Error: QCamera::NoError.
Recorder:
State: QMediaRecorder::UnavailableStatus;
Status: QMediaRecorder::StoppedState;
Error: QMediaRecorder::NoError.
Thanks in advance for your time.
It seems the problem is that the recording is still not working for windows, as explained here
I tested it on a mac a it worked fine. To fix this on windows I used the QtMEL library.
来源:https://stackoverflow.com/questions/25061102/qts-qmediarecorder-not-working-unable-to-record-from-webcam