Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to record video as soon as application started?
Forum Updated to NodeBB v4.3 + New Features

How to record video as soon as application started?

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hix_boz
    wrote on last edited by
    #1

    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);
    
    }
    jsulmJ 1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #15

      I think you should connect to the QCamera::statusChanged signal, and start the recorder after the status becomes QCamera::ActiveStatus (maybe also stop it when it is non-active).

      H 1 Reply Last reply
      2
      • H hix_boz

        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);
        
        }
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @hix_boz You can override https://doc.qt.io/qt-5/qwidget.html#showEvent and start recording there.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        H 1 Reply Last reply
        0
        • jsulmJ jsulm

          @hix_boz You can override https://doc.qt.io/qt-5/qwidget.html#showEvent and start recording there.

          H Offline
          H Offline
          hix_boz
          wrote on last edited by
          #3

          @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.

          jsulmJ 1 Reply Last reply
          0
          • H hix_boz

            @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.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @hix_boz Please add error handling:
            https://doc.qt.io/qt-5/qmediarecorder.html#error-1
            https://doc.qt.io/qt-5/qmediarecorder.html#stateChanged

            And 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.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            H 1 Reply Last reply
            0
            • jsulmJ jsulm

              @hix_boz Please add error handling:
              https://doc.qt.io/qt-5/qmediarecorder.html#error-1
              https://doc.qt.io/qt-5/qmediarecorder.html#stateChanged

              And 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.

              H Offline
              H Offline
              hix_boz
              wrote on last edited by
              #5

              @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?

              jsulmJ Christian EhrlicherC 2 Replies Last reply
              0
              • H hix_boz

                @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?

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @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?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                H 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @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?

                  H Offline
                  H Offline
                  hix_boz
                  wrote on last edited by
                  #7

                  @jsulm

                  Qt Creator 4.13.1
                  Based on Qt 5.15.1 (GCC 5.3.1 20160406 (Red Hat 5.3.1-6), 64 bit)

                  1 Reply Last reply
                  0
                  • H hix_boz

                    @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?

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @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.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    H 1 Reply Last reply
                    1
                    • Christian EhrlicherC Christian Ehrlicher

                      @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.

                      H Offline
                      H Offline
                      hix_boz
                      wrote on last edited by hix_boz
                      #9

                      @Christian-Ehrlicher
                      Do you ask to share all prject files when you say real code? Here the errors I got.

                      Screenshot from 2022-02-17 10-27-22.png

                      Additionaly, I took the error handler connect snippet from Qt camera example which is working with my Qt version.

                      jsulmJ 1 Reply Last reply
                      0
                      • H hix_boz

                        @Christian-Ehrlicher
                        Do you ask to share all prject files when you say real code? Here the errors I got.

                        Screenshot from 2022-02-17 10-27-22.png

                        Additionaly, I took the error handler connect snippet from Qt camera example which is working with my Qt version.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        @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...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        H 1 Reply Last reply
                        0
                        • jsulmJ jsulm

                          @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...

                          H Offline
                          H Offline
                          hix_boz
                          wrote on last edited by
                          #11

                          @jsulm

                          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-gnu

                          I 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.

                          jsulmJ Christian EhrlicherC 2 Replies Last reply
                          0
                          • H hix_boz

                            @jsulm

                            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-gnu

                            I 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.

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @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){ /* ... */ });
                            

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            H 1 Reply Last reply
                            0
                            • H hix_boz

                              @jsulm

                              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-gnu

                              I 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.

                              Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #13

                              @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

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              1 Reply Last reply
                              1
                              • jsulmJ jsulm

                                @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){ /* ... */ });
                                
                                H Offline
                                H Offline
                                hix_boz
                                wrote on last edited by
                                #14

                                @jsulm

                                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

                                1 Reply Last reply
                                0
                                • B Offline
                                  B Offline
                                  Bonnie
                                  wrote on last edited by Bonnie
                                  #15

                                  I think you should connect to the QCamera::statusChanged signal, and start the recorder after the status becomes QCamera::ActiveStatus (maybe also stop it when it is non-active).

                                  H 1 Reply Last reply
                                  2
                                  • B Bonnie

                                    I think you should connect to the QCamera::statusChanged signal, and start the recorder after the status becomes QCamera::ActiveStatus (maybe also stop it when it is non-active).

                                    H Offline
                                    H Offline
                                    hix_boz
                                    wrote on last edited by
                                    #16

                                    @Bonnie
                                    It works, thanks you so much.

                                    1 Reply Last reply
                                    0

                                    • Login

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved