项目需要,安排我实现mp4格式视频截取帧功能
网络部分源码加略微修改,一个晚上就搞定了。
使用的是openCV3.0+vs2015
系统是win64位
openCV3.0 下载地址
https://jaist.dl.sourceforge.net/project/opencvlibrary/opencv-win/3.0.0/opencv-3.0.0.exe
安装完毕后配置环境变量
D:\Program Files (x86)\Microsoft Visual Studio 12.0\opencv\build\x86\vc12\bin
我的机器是64位的这里我也是选了x86 opencv\build\x86\vc12\bin
后面的也都是选x86
VS2015项目配置
附加目录
安装路径\opencv\build\include\opencv2
安装路径\opencv\build\include\opencv
安装路径\opencv\build\include
库目录
安装路径\opencv\build\x86\vc12\lib
链接器
输入
附加依赖项
opencv_ts300d.lib
opencv_world300d.lib
然后配置就ok了
下面是我的代码,可以拿来测试一下
运行提示缺DLL的话,就去百度上下载,放在exe目录下,视频文件放在源代码目录下
补:DLL文件 http://pan.baidu.com/s/1dFj9SjR 密码 k057
#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include "cv.h"
#include "highgui.h"
#include"stdlib.h"
#include<string>
#include<iostream>
using namespace std;
int main()
{
CvCapture *capture;
capture = cvCreateFileCapture("2333.mp4");
assert(capture != NULL); //使用断言对函数参数进行确认,“假设”不成立则中断。
//显示视频信息--我的视频h576*w704,fps25,frame number15036
int frameH = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);
int frameW = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);
int fps = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
int numFrames = (int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT);
printf("\tvideo height : %d\n\tvideo width : %d\n\tfps : %d\n\tframe numbers : %d\n", frameH, frameW, fps, numFrames);
IplImage *frame = 0; //初始化定义
int n = 0; //初始化
char image_name[256]; //image_name的名字、类型、存放路径的字符串长度,小了可能会内存溢出中断
//要求提取5分10秒的截图
cout << endl;
cout << "视频总长度为" << int(numFrames / 15.0 / 60.0) <<"分钟"<< endl;
cout << "截图格式为 分-秒 (不带空格)如5分10秒则输入5-10" << endl<<"请输入要截图的时间:";
string time,min,sec;
//time = "5-10";
cin >> time;
if (time.find("-")) {
min = time.substr(0, time.find("-"));
sec = time.substr(min.length() + 1, time.length() - 1);
}
else
{
cout << "输入错误" << endl;
}
int fra = fps*(stoi(min) * 60 + stoi(sec));
while (1)
{
frame = cvQueryFrame(capture); //获取一帧图像
++n;
if (n == fra)break;
}
sprintf(image_name, "%s%d%s", ".\\", fra, ".jpg");
//保存的文件地址、类型、名称,中文路径保存不了。
//IplImage*out = cvCreateImage(CvSize(frame->width/2 , frame->height/2 ), frame->depth, frame->nChannels);
//cvPyrDown(frame, out, CV_GAUSSIAN_5x5); //下采样缩小图像
cvSaveImage(image_name, frame); //保存一帧图像
//cvReleaseImage(&frame);
cvReleaseCapture(&capture);
frame = NULL; //cvReleaseImage()和cvCreateImage()相对应的。在程序中如果没有使用cvCreateImage()“创建”就不能“释放”。
cout << "截图成功!" << endl;
system("pause"); //是为了让输出结果在cmd停留,前提是有#include"stdlib.h"
return 0;
}