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. Unable to run ffmpeg with qProcess to extract a single frame from Video
QtWS25 Last Chance

Unable to run ffmpeg with qProcess to extract a single frame from Video

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.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.
  • K Offline
    K Offline
    kulvi
    wrote on last edited by kulvi
    #1

    I am trying to extract a single frame from a video using the Qprocess and ffmpeg and store it in a tmp folder. When I run the program in terminal command line it is able to extract it but not when qprocess runs it. I'm developing on a mac. What am I doing wrong.

    QString extractSingleFrame(QString videoPath, QDir tmpDir)
    {
        QString program = ffmpegPath();
        QProcess *ffmpegProcess = new QProcess();
        QString arguments;
        QFileInfo videoInfo(videoPath);
        QString videoName = videoInfo.fileName();
    
        QString firstFrameName(tmpDir.absolutePath() + "/" + videoName + ".jpg");
        arguments = " -y -i " + QDir::toNativeSeparators(videoPath)
                    + " -frames:v 1 -q:v 1 -f image2 " + QDir::toNativeSeparators(firstFrameName);
    
        QFile::remove(firstFrameName);
        qDebug() << "Starting program" << program + arguments;
        ffmpegProcess->start(program + arguments);
    
        if (ffmpegProcess->state() == QProcess::Starting){
           qDebug() << "program is starting" + ffmpegProcess->state();
        }
        ffmpegProcess->waitForFinished(-1);
    
        qDebug() << "done";
    
        delete ffmpegProcess;
    
        return firstFrameName;
    }
    

    This is the printed command
    Starting program "ffmpeg -y -i "/Users/userX/test.mov" -vframes 1 -q:v 1 -f image2 "/var/folders/s1/vtv2cx36p3h0000gn/T/test-ywDBgj/test.mov.jpg""

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      You seems to append all parameters to one string.
      It might be the reason.
      Normally its a QStringList and given as a separate parameter.

       QObject *parent;
          ...
          QString program = "./path/to/Qt/examples/widgets/analogclock";
          QStringList arguments;
          arguments << "-style" << "fusion";
      
          QProcess *myProcess = new QProcess(parent);
          myProcess->start(program, arguments);
      
      1 Reply Last reply
      4
      • K Offline
        K Offline
        kulvi
        wrote on last edited by
        #3

        I have tried to do:

        ffmpegProcess->start(program , QStringList()
                                 << "-y"
                                 << "-i"
                                 << QDir::toNativeSeparators(videoPath)
                                 << "-frames:v 1"
                                 << "-q:v 1"
                                 << "-f"
                                 << "image2"
                                 << QDir::toNativeSeparators(firstFrameName));
        

        This just gives the same result. In both cases ffmpegProcess->state() == QProcess::NotRunning gives a true flag.

        I'm having a hard time getting some error message output to pinpoint this issue. I am new to Qt so further help would be deeply appreciated.
        Using this to debug this gives me only an empty string:

        QString StdOut_sdcompare = ffmpegProcess->readAllStandardOutput();
            qDebug() << StdOut_sdcompare;
            QString StdError_sdcompare = ffmpegProcess->readAllStandardError();
            qDebug() << StdError_sdcompare;
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          You still don't generate the parameter list correctly:

              QProcess ffmpeg;
              ffmpeg.start("ffmpeg" , QStringList()
                                   << "-y"
                                   << "-i" << QDir::toNativeSeparators(videoPath)
                                   << "-frames:v" << "1"
                                   << "-q:v" << "1"
                                   << "-f" << "image2"
                                   << QDir::toNativeSeparators(firstFrameName));
              ffmpeg.waitForFinished(-1);
          

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          K 1 Reply Last reply
          5
          • SGaistS SGaist

            Hi,

            You still don't generate the parameter list correctly:

                QProcess ffmpeg;
                ffmpeg.start("ffmpeg" , QStringList()
                                     << "-y"
                                     << "-i" << QDir::toNativeSeparators(videoPath)
                                     << "-frames:v" << "1"
                                     << "-q:v" << "1"
                                     << "-f" << "image2"
                                     << QDir::toNativeSeparators(firstFrameName));
                ffmpeg.waitForFinished(-1);
            
            K Offline
            K Offline
            kulvi
            wrote on last edited by
            #5

            Thanks that did the trick, also I wasn't passing in the full path of ffmpeg.

            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