Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qprocess: howto run a command consisting of several commands chained together?



  • Hi All,

    I'm writing a program that calls raspivid to record a video thru QProcess. This works great like this:

    void ExternalCamera::slotStartCapture(QString filename)
    {
        if (myProcess->state() == QProcess::Running)
        {
            qDebug()<<"Process already Running, killing";
            myProcess->kill();
            myProcess->waitForFinished(10000);
            //qDebug()<<"Process Killed";
        }
        qDebug()<<myProcess->state();
        filename.append(".h264");
        QStringList arguments;
        //arguments <<"-o" << "file.h264";    //filename
        arguments <<"-o" << filename;    //filename
        arguments <<"-n";                   //no preview
        arguments <<"--bitrate" << "1000000";//bitrate
        arguments <<"-t"<<"300000";          //time
    
    
        myProcess->setArguments(arguments);
        myProcess->setProgram("/usr/bin/raspivid");
    
        myProcess->start();
        qDebug()<<"start movie: "<<filename;
        myProcess->waitForStarted(10000);
        //qDebug()<<"Process started";
    }
    

    But now I would like to run the following command line. How can I tell QProcess to do this? Should I place this inside a .sh file, and then call the sh file?

    raspivid -t 200000 -o test16.h264 ; avconv -framerate 30 -i test16.h264 -c:v copy test16.mp4
    

    Cheers,
    Cedric


  • Lifetime Qt Champion

    Hi,

    So avconv should be called once raspivid is done, correct ?



  • @cdwijs
    You have 3 options!

    1. Split into two separate commands at the ;. Run the left-hand side, wait to finish, run the right-hand side, as two different QProcesses.

    2. Put that command line into a file. Run QProcess::execute("/bin/sh", QStringList() << "script.sh").

    3. Run the whole command-line as an argument to the shell:

    QProcess::execute("/bin/sh", QStringList() << "-c" << "raspivid -t 200000 -o test16.h264 ; avconv -framerate 30 -i test16.h264 -c:v copy test16.mp4")`.
    

Log in to reply