Webcam video recording with Qt5

I try to get this to work 2 days and I am desperate. Basically I want to record video using my webcam using Qt5. I still realized that I can get a widget to display what the webcam sees, but for some reason, when I try to write it with the QMediaRecorder class, it does not save anything in the output table.

The output file is created, but contains 0 bytes. I tried playing with the settings of the video codec, but still no luck. I would think that such simple code 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 mainly for simple writing to a file. I stopped writing to the destructor. So the question is, why is this not working?

Thanks in advance:)

+4

All Articles