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

Creating a video file in Qt



  • I want to make a video file (.avi or .mp4) in qt. Through some online sources I came to know that video can be made by combining QImages into a single file, but I don't know how to exactly do that. Any lead will be helpful

    A different approach is also welcomed......


  • Lifetime Qt Champion

    Hi
    For fast way, i would just use ffmpeg
    http://hamelot.io/visualization/using-ffmpeg-to-convert-a-set-of-images-into-a-video/
    with QProcess.

    As creating a mp4 from scratch is pretty involving due to codex compressing and all the header info.

    ffmpeg runs on linux, mac and windows so its only if u use android it would be a bad idea.



  • @NIXIN what about trying with a QML Camera component?



  • @mrjj Thanks for your response. Based on your suggestion I tried this:

    void Video::slot_generateVideo()
    {
    QStringList arguments;
    QString imgPath("home/uidm9805/images/pic%04d.png");
    int fps = 2;
    QString frameRate;
    QString compressionLevel("image2");
    QString resolution("1920x1080");
    QString startFrame("1");
    QString videoFrames("1000");
    QString codec("libx264");
    QString quality("25");
    QString pixelFormat("yuv420p");
    QString outputFile("\"home/uidm9805/images/test.mp4\"");
    
    arguments << "-r";
    arguments << frameRate.setNum(fps);
    
    arguments << "-f";
    arguments << compressionLevel;
    
    arguments << "-s";
    arguments << resolution;
    
    arguments << "-start_number";
    arguments << startFrame;
    
    arguments << "-i";
    arguments << imgPath;
    
    arguments << "-vframes";
    arguments << videoFrames;
    
    arguments << "-vcodec";
    arguments << codec;
    
    arguments << "-crf";
    arguments << quality;
    
    arguments << "-pix_fmt";
    arguments << pixelFormat;
    
    arguments << outputFile;
    
    qDebug() << arguments;
    videoProcess = new QProcess(this);
    connect(videoProcess, SIGNAL(started()), this, SLOT(monitorStarted()));
    connect(videoProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(monitorError(QProcess::ProcessError)));
    connect(videoProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput()));
    videoProcess->start("ffmpeg", arguments);
    videoProcess->waitForFinished(-1);
    }
    
    void Video::monitorStarted()
    {
        qDebug() << "Video Process started";
    }
    
    void Video::monitorError(QProcess::ProcessError error)
    {
        qDebug() << "Video Process error: " << error;
    }
    
    void Video::readOutput()
    {
        qDebug() << videoProcess->readAllStandardOutput();
    }
    

    I am working on ubuntu.
    when I run the same set of commands from terminal everything is working fine, test.mp4 is generated.
    But using QProcess, I got this output:
    Video Process error: 0
    I don't understand what might be the problem, any help will be appreciable.
    Thank you


  • Qt Champions 2019

    @NIXIN "QString imgPath("home/uidm9805/images/pic%04d.png");" you're missing / in front of home. Also why do you have % in file name?
    Same here QString outputFile("\"home/uidm9805/images/test.mp4\""); and remove \" as Qt will do it for you.



  • I tried giving the following paths:

    QString folderName ="images/pic"
    QString imgPath("/home/uidm9805/" + folderName + "%04d.png");
    QString outputFile("/home/uidm9805/images/test.mp4"); 
    

    still not working, getting same errors


  • Lifetime Qt Champion

    Hi
    Code seems fine.
    try with full path to ffmpeg
    also
    folderName ="images/pic"

    Depending on how it uses this, it might not be correct.



  • @mrjj
    Tried this

    videoProcess->start("usr/bin/ffmpeg", arguments);
    

    but got the same errors.



  • @mrjj

    folderName ="images/pic"
    
    Depending on how it uses this, it might not be correct.
    

    Can you explain this please.....


  • Lifetime Qt Champion

    @NIXIN

    If something just appends that to other path or use it relative,
    it might not work as expected.
    Like
    somewhere = "/home"
    open (somewhere + folderName )
    "/homeimages/pic"
    and so one.
    but if it adds / when doing + , all is fine.

    since there is no error reported with QProcess i assume it runs the app but app do not produce output for some reason.


  • Lifetime Qt Champion

    Hi,

    You are missing the leading / in your path to ffmpeg.


  • Qt Champions 2019

    @NIXIN said in Creating a video file in Qt:

    usr/bin/ffmpeg

    Please take a closer look at the path. You're doing same mistake over and over even after it was pointed out.


Log in to reply