Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Going through each frame with VideoCapture might be slow. You can get 3x perfomance boost if you invoke ffprobe.exe, assuming its in current folder:

#include <iomanip>
#include <string>
#include <iostream>
using namespace std;

string exec(string cmd) {
    array<char, 128> buffer;
    string result;
    shared_ptr<FILE> pipe(_popen(cmd.c_str(), "r"), _pclose);
    if (!pipe) throw std::runtime_error("popen() failed!");
    while (!feof(pipe.get())) {
        if (fgets(buffer.data(), 128, pipe.get()) != nullptr)
            result += buffer.data();
    }
    return result;
}
int measureFile(string fileName) {
    string cmd = "ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 ";
    auto lnch = cmd + fileName;
    cout << lnch << endl;
    string res = exec(lnch);
    try {
        return stoi(res);
    }
    catch (...) {
        return -1;
    }
}

ffprobe.exe is standalone file, you can download it here: https://ffbinaries.com/downloads

I think this will also work on linux, but you need to replace _popen->popen and _pclose->pclose.