转载https://blog.csdn.net/M_Alan_walker/article/details/88841827
opencv4.20中,以下代码
float fps = capture.get(CAP_PROP_FPS);
long framewidth = capture.get(CAP_PROP_FRAME_WIDTH);
long framehigh = capture.get(CAP_PROP_FRAME_HEIGHT);
float framecount = capture.get(CAP_PROP_FRAME_COUNT);
和
VideoWriter writer("/home/cidi/Desktop/image/new_stream0.avi",('D', 'I', 'V', 'X'), fps, Size(framewidth, framehigh));
替换以下代码中的相应片段。
opencv截取视频。
#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
//思路很简单 总秒数 = 小时*3600 + 分钟*60 + 秒
int calc_second(char* temp)
{
int flag=1,second=0;
if(temp == NULL)
return 1;
while(*temp != '\0')
{
int count=((*temp++)-48)*10 + (*(temp++)-48);
if(flag==1)
second += count*3600;
if(flag==2)
second += count*60;
if(flag==3)
second += count;
++flag;
++temp;
}
return second;
}
int process(VideoCapture& capture,int start_second,int end_second)
{
float fps = capture.get(CV_CAP_PROP_FPS);
long framewidth = capture.get(CV_CAP_PROP_FRAME_WIDTH);
long framehigh = capture.get(CV_CAP_PROP_FRAME_HEIGHT);
float framecount = capture.get(CV_CAP_PROP_FRAME_COUNT);
cout<<"the fps= "<<fps<<endl;
cout<<"the framecount="<<framecount<<endl;
cout<<"the framewidth= "<<framewidth<<endl;
cout<<"the framehight= "<<framehigh<<endl;
//秒转换成帧 fps = 每秒的帧数
int start_frame = start_second * fps;
int end_frame = end_second * fps;
//开始写入
VideoWriter writer("/home/cidi/Desktop/image/new_stream0.avi",CV_FOURCC('D','I','V','X'), fps,Size(framewidth,framehigh));
int i=0;
while(1)
{
Mat frame;
capture>>frame;
i++;
if(frame.empty())
break;
if(i>=start_frame && i<=end_frame)
{
imshow("now frame",frame);
writer<<frame;
waitKey(30);
}
if(i > end_frame)
break;
}
return 0;
}
//输入:截取的时间段 例如00:01:00 00:03:00 截取这段视频的第一分钟到第三分钟的内容
int main(int argc,char** argv)
{
//system("/home/cidi/Desktop/image/shipin.sh"); 一个脚本 转换视频格式的
//打开源视频文件
VideoCapture capture("/home/cidi/Desktop/image/ha.avi");
if(!capture.isOpened())
{
cout<<"open the video fail!"<<endl;
return 0;
}
//截取前需要先将输入的char类型时间转换成int型的时间
int start_second = calc_second(argv[1]);
nt end_second = calc_second(argv[2]);
//开始截取
return process(capture,start_second,end_second);
}
————————————————
版权声明:本文为CSDN博主「XuSheng.Mrs」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/M_Alan_walker/article/details/88841827