批量解帧视频文件cpp

前言

  将多个视频文件进行解帧。

实现过程

1.批量获取文件路径;

2.对某个视频文件进行解帧;

代码

/************************************************************************
* Copyright(c) 2017  ZRJ
* All rights reserved.
*
* File:    video2frames.cpp
* Brief: 批量解帧视频文件
* Version: 1.0
* Author: ZRJ
* Email: happyamyhope@163.com
* Date:    2017/05/10
* History:
* 20170510: 批量解帧视频文件;


************************************************************************/
//-------------------------------------------------------------------------
//头文件
#include<highgui.h>
#include<cv.h>
#include<io.h>
#include<iostream>
#include<fstream>
#include <direct.h>//mkdir
using namespace std;
using namespace cv;
//调参
string filePath = "E:\\carriage_recognition\\video\\2017-05-02";
char picfilename[64];
char save_path[64];
//函数声明
void getFiles(string path, vector<string>& files)
{
    //文件句柄  
    long   hFile = 0;
    //文件信息  
    struct _finddata_t fileinfo;
    string p;
    if ((hFile = (long)_findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
    {
        do
        {
            //如果是目录,迭代之  
            //如果不是,加入列表  
            if ((fileinfo.attrib &  _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
            }
            else
            {
                files.push_back(p.assign(path).append("\\").append(fileinfo.name));
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
}
void mpf2frame(const char* videoPath, char* picfilename)
{
    CvCapture* capture = cvCaptureFromFile(videoPath);
    int fps = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    long nFrame = (long)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT); // 获取总帧数
    int width = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
    int height = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
    IplImage* image = NULL;
    int i = 0;

    if (capture)
    {
        while (1)
        {
            image = cvQueryFrame(capture);
            if (image)
            {
                i++;
                if (i % 5000 == 0 && i <= nFrame )
                {
                    sprintf(save_path, "%s\\%d.jpg", picfilename, i);
                    cvSaveImage(save_path, image);
                }
                else if (i < nFrame)
                {
                    
                }
                else if (i >= nFrame)
                {
                    return;
                }
            }
        }
    }


}
int main()
{

    ofstream fout("video_list.txt");
    vector<string> files;
    //获取该路径下的所有文件
    getFiles(filePath, files);
    int size = (int)files.size();
    for (int i = 0; i < size; i++)
    {
        fout << files[i].c_str() << endl;
        cout << i << endl;
        const char* filename = files[i].c_str();
        sprintf(picfilename, "%s\\frame", filePath.c_str());
        _mkdir(picfilename);
        sprintf(picfilename, "%s\\%d", picfilename, i);
        _mkdir(picfilename);
        mpf2frame(filename, picfilename);

    }
    fout.close();
    return 0;

}
View Code

 

代码说明:

1.每个视频文件的解帧文件夹在视频文件路径下,按序排列;

2.字符串的操作不熟练;

3.批量获取文件参考博客:http://blog.csdn.net/xuejiren/article/details/37040827#

 

posted on 2017-05-10 14:46  鹅要长大  阅读(524)  评论(0编辑  收藏  举报

导航