How to record video as soon as application started?
-
Hello all,
I would like to record video as soon as my Qt application started. I have managed to start the video recordings with buttons but weirdly I couldnt record the video without any button signals and slots. How can I record the video from startup ? Here is the associated parts of my code with button signals and slots. Any help will be appreciated.
code_text ``` const QList<QCameraInfo> cameras = QCameraInfo::availableCameras(); for (const QCameraInfo &cameraInfo : cameras) { qDebug() << cameraInfo.deviceName(); if (cameraInfo.deviceName() == "/dev/video0"){ camera = new QCamera(cameraInfo); mediaRecorder = new QMediaRecorder(camera); mediaRecorder->setContainerFormat("mp4"); viewfinder = new QCameraViewfinder(this->ui->stackedWidget); viewfinder->setGeometry(0,0,900,400); viewfinder->show(); connect(mediaRecorder, &QMediaRecorder::durationChanged, this, &MainWindow::updateRecordTime); camera->setViewfinder(viewfinder); camera->unload(); camera->setCaptureMode(QCamera::CaptureVideo); camera->start(); } void MainWindow::on_pushButton_pressed() { camera->setCaptureMode(QCamera::CaptureVideo); record(); camera->setCaptureMode(QCamera::CaptureVideo); this->ui->pushButton_stop->setEnabled(true); if (mediaRecorder->state() == QMediaRecorder::RecordingState){ qDebug() << "It is in recording state"; } } void MainWindow::record() { camera->setCaptureMode(QCamera::CaptureVideo); QString name = filename + QDateTime::currentDateTime().toString("dd.MM.yy-h-m-s");//3 mediaRecorder->setOutputLocation(QUrl::fromLocalFile(outputpath + "/" + name + ".mp4")); mediaRecorder->record(); this->ui->pushButton->setEnabled(false); }
-
Hello all,
I would like to record video as soon as my Qt application started. I have managed to start the video recordings with buttons but weirdly I couldnt record the video without any button signals and slots. How can I record the video from startup ? Here is the associated parts of my code with button signals and slots. Any help will be appreciated.
code_text ``` const QList<QCameraInfo> cameras = QCameraInfo::availableCameras(); for (const QCameraInfo &cameraInfo : cameras) { qDebug() << cameraInfo.deviceName(); if (cameraInfo.deviceName() == "/dev/video0"){ camera = new QCamera(cameraInfo); mediaRecorder = new QMediaRecorder(camera); mediaRecorder->setContainerFormat("mp4"); viewfinder = new QCameraViewfinder(this->ui->stackedWidget); viewfinder->setGeometry(0,0,900,400); viewfinder->show(); connect(mediaRecorder, &QMediaRecorder::durationChanged, this, &MainWindow::updateRecordTime); camera->setViewfinder(viewfinder); camera->unload(); camera->setCaptureMode(QCamera::CaptureVideo); camera->start(); } void MainWindow::on_pushButton_pressed() { camera->setCaptureMode(QCamera::CaptureVideo); record(); camera->setCaptureMode(QCamera::CaptureVideo); this->ui->pushButton_stop->setEnabled(true); if (mediaRecorder->state() == QMediaRecorder::RecordingState){ qDebug() << "It is in recording state"; } } void MainWindow::record() { camera->setCaptureMode(QCamera::CaptureVideo); QString name = filename + QDateTime::currentDateTime().toString("dd.MM.yy-h-m-s");//3 mediaRecorder->setOutputLocation(QUrl::fromLocalFile(outputpath + "/" + name + ".mp4")); mediaRecorder->record(); this->ui->pushButton->setEnabled(false); }
@hix_boz You can override https://doc.qt.io/qt-5/qwidget.html#showEvent and start recording there.
-
@hix_boz You can override https://doc.qt.io/qt-5/qwidget.html#showEvent and start recording there.
@jsulm
Hello jsulm,
Thanks fro your asnwer. I added the function like before.void MainWindow::showEvent(QShowEvent *event){ qDebug() << "////DEBUGGG////"; QString name = filename + QDateTime::currentDateTime().toString("dd.MM.yy-h-m-s");//3 mediaRecorder->setOutputLocation(QUrl::fromLocalFile(outputpath + "/" + name + ".mp4")); mediaRecorder->record(); if (mediaRecorder->state() == QMediaRecorder::RecordingState){ qDebug() << "It is in recording state"; } }
The function has been called and debug printed. It does not record the video and the recording state debug does not printed. What could be the problem? Thanks inadvanced.
-
@jsulm
Hello jsulm,
Thanks fro your asnwer. I added the function like before.void MainWindow::showEvent(QShowEvent *event){ qDebug() << "////DEBUGGG////"; QString name = filename + QDateTime::currentDateTime().toString("dd.MM.yy-h-m-s");//3 mediaRecorder->setOutputLocation(QUrl::fromLocalFile(outputpath + "/" + name + ".mp4")); mediaRecorder->record(); if (mediaRecorder->state() == QMediaRecorder::RecordingState){ qDebug() << "It is in recording state"; } }
The function has been called and debug printed. It does not record the video and the recording state debug does not printed. What could be the problem? Thanks inadvanced.
@hix_boz Please add error handling:
https://doc.qt.io/qt-5/qmediarecorder.html#error-1
https://doc.qt.io/qt-5/qmediarecorder.html#stateChangedAnd I'm not sure you can expect state() to return QMediaRecorder::RecordingState just after calling record() - it can take some time until the sate changes.
-
@hix_boz Please add error handling:
https://doc.qt.io/qt-5/qmediarecorder.html#error-1
https://doc.qt.io/qt-5/qmediarecorder.html#stateChangedAnd I'm not sure you can expect state() to return QMediaRecorder::RecordingState just after calling record() - it can take some time until the sate changes.
@jsulm I have added the error handling like before,
connect(mediaRecorder, QOverload<QMediaRecorder::Error>::of(&QMediaRecorder::error),this, &MainWindow::displayRecorderError);
void MainWindow::displayRecorderError() { QMessageBox::warning(this, tr("Capture Error"), mediaRecorder->errorString()); }
I got an error like "no template named 'QOverLoad'" . I have checked camera example of qt which records video and capture images application and I did almost same thing. I suspect the version incompatibility. Do you think version differences cause this issue?
-
@jsulm I have added the error handling like before,
connect(mediaRecorder, QOverload<QMediaRecorder::Error>::of(&QMediaRecorder::error),this, &MainWindow::displayRecorderError);
void MainWindow::displayRecorderError() { QMessageBox::warning(this, tr("Capture Error"), mediaRecorder->errorString()); }
I got an error like "no template named 'QOverLoad'" . I have checked camera example of qt which records video and capture images application and I did almost same thing. I suspect the version incompatibility. Do you think version differences cause this issue?
@hix_boz said in How to record video as soon as application started?:
Do you think version differences cause this issue?
What Qt version do you use?
-
@hix_boz said in How to record video as soon as application started?:
Do you think version differences cause this issue?
What Qt version do you use?
-
@jsulm I have added the error handling like before,
connect(mediaRecorder, QOverload<QMediaRecorder::Error>::of(&QMediaRecorder::error),this, &MainWindow::displayRecorderError);
void MainWindow::displayRecorderError() { QMessageBox::warning(this, tr("Capture Error"), mediaRecorder->errorString()); }
I got an error like "no template named 'QOverLoad'" . I have checked camera example of qt which records video and capture images application and I did almost same thing. I suspect the version incompatibility. Do you think version differences cause this issue?
@hix_boz said in How to record video as soon as application started?:
QOverload
I got an error like "no template named 'QOverLoad'"
QOverload != QOverLoad - please post the real code and the real error message.
-
@hix_boz said in How to record video as soon as application started?:
QOverload
I got an error like "no template named 'QOverLoad'"
QOverload != QOverLoad - please post the real code and the real error message.
@Christian-Ehrlicher
Do you ask to share all prject files when you say real code? Here the errors I got.Additionaly, I took the error handler connect snippet from Qt camera example which is working with my Qt version.
-
@Christian-Ehrlicher
Do you ask to share all prject files when you say real code? Here the errors I got.Additionaly, I took the error handler connect snippet from Qt camera example which is working with my Qt version.
@hix_boz said in How to record video as soon as application started?:
Do you ask to share all prject files when you say real code?
No, your connect statement.
As @Christian-Ehrlicher pointed out it is QOverload NOT QOverLoad...And the version you posted only says something about Qt version used to build QtCreator, not Qt version you're using...
-
@hix_boz said in How to record video as soon as application started?:
Do you ask to share all prject files when you say real code?
No, your connect statement.
As @Christian-Ehrlicher pointed out it is QOverload NOT QOverLoad...And the version you posted only says something about Qt version used to build QtCreator, not Qt version you're using...
After commanding "qmake --version" from terminal, I got this one.
QMake version 3.0
Using Qt version 5.5.1 in /usr/lib/x86_64-linux-gnuI also typed "qtdiag" and the first line of response like below.
Qt 5.5.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 5.4.0 20160609) on "xcb"
I tried either QOverLoad and QOverload and still got an error. As I said before, I took the connect part from camera example which has worked and I did not change anything. They used "QOverload" in connect line.
-
After commanding "qmake --version" from terminal, I got this one.
QMake version 3.0
Using Qt version 5.5.1 in /usr/lib/x86_64-linux-gnuI also typed "qtdiag" and the first line of response like below.
Qt 5.5.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 5.4.0 20160609) on "xcb"
I tried either QOverLoad and QOverload and still got an error. As I said before, I took the connect part from camera example which has worked and I did not change anything. They used "QOverload" in connect line.
@hix_boz According to Qt5.5 documentation (https://doc.qt.io/archives/qt-5.5/qmediarecorder.html#error-1):
connect(mediaRecorder, static_cast<void(QMediaRecorder::*)(QMediaRecorder::Error)>(&QMediaRecorder::error), [=](QMediaRecorder::Error error){ /* ... */ });
-
After commanding "qmake --version" from terminal, I got this one.
QMake version 3.0
Using Qt version 5.5.1 in /usr/lib/x86_64-linux-gnuI also typed "qtdiag" and the first line of response like below.
Qt 5.5.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 5.4.0 20160609) on "xcb"
I tried either QOverLoad and QOverload and still got an error. As I said before, I took the connect part from camera example which has worked and I did not change anything. They used "QOverload" in connect line.
@hix_boz said in How to record video as soon as application started?:
I tried either QOverLoad and QOverload and still got an error
No - there must be at least another error message, please post the error message when you use the correct function QOverload
-
@hix_boz According to Qt5.5 documentation (https://doc.qt.io/archives/qt-5.5/qmediarecorder.html#error-1):
connect(mediaRecorder, static_cast<void(QMediaRecorder::*)(QMediaRecorder::Error)>(&QMediaRecorder::error), [=](QMediaRecorder::Error error){ /* ... */ });
It worked like this,
connect(mediaRecorder, static_cast<void(QMediaRecorder::*)(QMediaRecorder::Error)>(&QMediaRecorder::error), [=](QMediaRecorder::Error error){ qDebug() << mediaRecorder->errorString(); });
The error I got ,
"Service has not been started" .Also the below message I keep getting.
(videoPlayer:9057): GStreamer-CRITICAL **: gst_mini_object_unref: assertion 'mini_object != NULL' failed -
I think you should connect to the
QCamera::statusChanged
signal, and start the recorder after the status becomesQCamera::ActiveStatus
(maybe also stop it when it is non-active).