opencv4.20截取视频

转载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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值