extract frame from a video for detection with opencv
<p>i'm developing a program to detect object in video or image.
It works with the image, but now i want to use it with video. I use a specific folder to pick the image so i wanted to save frames from video in that folder before the detection.
The variable video and salvataggio are alredy setted. 
In the following code i navigate throw the folder to analize the video:</p>

<pre><code>DIR *dir;
dir = opendir(video.c_str());
string vidName;
struct dirent *ent;
if (dir != NULL) {
        while ((ent = readdir (dir)) != NULL) {
            vidName= ent-&gt;d_name;
            if(vidName.compare(".")!= 0 &amp;&amp; vidName.compare("..")!= 0)
            {
            //string vidPath(neg + vidName);
                estraiframe(video, vidName, salvataggio);
            }
        }
        closedir (dir);

}
else {
    cout&lt;&lt;"directory "&lt;&lt; video &lt;&lt; " not present"&lt;&lt;endl;
}
}
</code></pre>

<p>The function estraiframe save the frame in the output folder.</p>

<pre><code>void estraiframe(string path, string vidName, string output){

string vidPath(path + vidName);
VideoCapture cap(vidPath);
if( !cap.isOpened()){
        cout &lt;&lt; "Cannot open the video file" &lt;&lt; endl;
        return;
}

double count = cap.get(CV_CAP_PROP_FRAME_COUNT);
double rate = cap.get(CV_CAP_PROP_FPS);
int counter = 0;
for (int i=1; i&lt; count; i+=rate*5)
{


cap.set(CV_CAP_PROP_POS_FRAMES,i);

Mat frame;
cap.read(frame);

counter++;
string nomeframe = to_string(counter) + "-frame_from"+vidName+".jpg";
string percorso (output+nomeframe);
cout &lt;&lt; percorso;
imwrite(percorso,frame);
}
}
</code></pre>

<p>Apparently it works but after the last frame it gave me the following error:</p>

<blockquote>
  <p>Assertion stream_index &lt; ogg->nstreams failed at libavformat/oggdec.c:898
  I locked for it but i didin't find where is the error</p>
</blockquote>