QT的QMediaRecorder无法正常工作 - 无法从网络摄像头录制

时间:2014-07-31 14:10:40

标签: c++ qt video webcam video-recording

我一直在寻找解决方案,我在这里看到了一些关于这个问题的线索,但是没有解决方案。

我正在尝试从计算机上的两个不同的网络摄像头录制视频。我能够显示他们两个的视频,但问题是录制。当我尝试它没有任何反应。没有文件保存,状态也永远不会改变。 Bellow我附加了加载摄像头的代码和加载QMediaRecorders的代码。我也尝试过只使用其中一个摄像头,它仍然无法正常工作。 QT示例“相机”也不起作用,录制选项始终禁用。

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();
}

当我按下录制按钮时:

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();
}

状态,状态和错误如下(每次我打电话给他们)

相机: 州:QCamera::UnavailableStatus; 状态:QCamera::ActiveState; 错误:QCamera::NoError.

录像机: 州:QMediaRecorder::UnavailableStatus; 状态:QMediaRecorder::StoppedState; 错误:QMediaRecorder::NoError.

提前感谢您的时间。

1 个答案:

答案 0 :(得分:0)

似乎问题是录制仍然不适用于Windows,如here

所述

我在mac上测试了它工作正常。要在Windows上修复此问题,我使用了QtMEL library