alensama 2016-02-03 12:52 采纳率: 100%
浏览 2072
已采纳

opencv 保存视频文件名称更改问题

cvCreateVideoWriter( const char* filename, int fourcc, double fps, CvSize frame_size,
int is_color CV_DEFAULT(1));
这是一个保存视频文件的函数 第一个参数是文件名 每次保存的文件名称为当前时间,自己写的代码如下:
char *getSystemTime()
{
char y[20], mon[5], d[5], h[5], mins[5], *temp;
char *temp;
char str[5]=".avi";
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime); // 需要给timeinfo赋值
printf("%4d-%02d-%02d-%02d:%02d:%02d\n", 1900 + timeinfo->tm_year, 1 + timeinfo->tm_mon, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
_itoa(1900 + timeinfo->tm_year, y, 10);
_itoa(1 + timeinfo->tm_mon, mon, 10);
_itoa(timeinfo->tm_mday, d, 10);
_itoa(timeinfo->tm_hour, h, 10);
_itoa(timeinfo->tm_min, mins, 10);
strcat(y, mon);
strcat(y, d);
strcat(y, h);
strcat(y, mins);
temp = strcat(y, str);
printf("%s\n", temp);
return temp;
}

int main(int argc, char** argv)
{
CvVideoWriter* video = NULL;
CvCapture* capture = cvCaptureFromCAM(0);
int n;
pFrame = cvQueryFrame(capture); //首先取得摄像头中的一帧
int fps = 32; //捕捉帧率

CvVideoWriter* writer = 0; //保存就加上这句
int isColol = 1;
int frameW = 640;
int frameH = 480;
video = cvCreateVideoWriter(getSystemTime(), CV_FOURCC('P', 'I', 'M', '1'), fps, cvSize(frameW, frameH), isColol);
if (video) //如果能创建CvVideoWriter对象则表明成功
{
cout << "VideoWriter has created." << endl;
}
//创建窗口
cvNamedWindow("video", 1);
if (argc == 1)
if (!(pCapture = cvCaptureFromCAM(0)))
{
MessageBox(NULL, TEXT("打开摄像头失败,请查看是否已连接摄像头"), TEXT("错误通知"), MB_OK);
return -2;
}
while (1)
{
pFrame = cvQueryFrame(capture); //从CvCapture中获得一帧
if (!pFrame)
{
cout << "Can not get frame from the capture." << endl;
break;
}
n = cvWriteFrame(video, pFrame); //判断是否写入成功,如果返回的是1,表示写入成功
cout << n << endl;
cvShowImage("video", pFrame); //显示视频内容的图片
}

别的不重要的就不写了,debug出来到 video = cvCreateVideoWriter(getSystemTime(), CV_FOURCC('P', 'I', 'M', '1'), fps, cvSize(frameW, frameH), isColol);
这句就报错了情况如下!
图片说明
图片说明
求大神帮助

  • 写回答

3条回答 默认 最新

  • threenewbee 2016-02-03 13:56
    关注

    假设你的文件名是类似20160203.avi

     char t[100];
    SYSTEMTIME st;
    GetSystemTime(&st);
    sprintf(t, "%04d%2d%2d.avi",  st.wYear,  st.wMonth,  st.wDay );
    video = cvCreateVideoWriter(t, CV_FOURCC('P', 'I', 'M', '1'), fps, cvSize(frameW, frameH), isColol);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题