批量解帧视频文件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   鹅要长大  阅读(527)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示