1、OpenCV打开摄像头
2、打开视频流文件
3、VideoCapture关闭和释放资源
4、读取一帧视频
5、获取视频、相机属性
播放时间 秒 = 总帧数/帧率
6、设置视频的播放进度
7、打开视频代码演示
VideoCapture video;
video.open("1.mp4");
if (!video.isOpened())
{
cout << "open video failed!" << endl;
getchar();
return -1;
}
cout << "open video success!" << endl;
namedWindow("video");
Mat frame;
int fps = video.get(CAP_PROP_FPS); // 获取帧率
int s = 30;
if (fps!=0)
{
s = 1000 / fps;
}
cout << "fps is " << fps << endl;
int fcount = video.get(CAP_PROP_FRAME_COUNT);
cout << "total frame is " << fcount << endl;
cout << "total second is " << fcount / fps;
for (;;)
{
//if (!video.read(frame))
//{
// break;
//}
//读帧,解码
if (!video.grab())
{
break;
}
//转换颜色格式
if (!video.retrieve(frame))
{
break;
}
int cur = video.get(CAP_PROP_POS_FRAMES); // 获取视频当前属性
if (cur > fps * 3)
{
video.set(CAP_PROP_POS_FRAMES, 0); // 3秒后回到开始位置
continue;
}
imshow("video", frame);
waitKey(s);
}
getchar();
8、VideoWriter 创建视频文件
操作完成之后,需要主动关闭;
9、VideoWriter
VideoCapture cam(0);
namedWindow("cam");
if (!cam.isOpened())
{
cout << "cam open failed!" << endl;
getchar();
return -1;
}
cout << "cam open success!" << endl;
Mat img;
VideoWriter vw;
int fps = cam.get(CAP_PROP_FPS);
if (fps <= 0)fps = 25;
vw.open("out.avi", VideoWriter::fourcc('X', '2', '6', '4'), fps,
Size(cam.get(CAP_PROP_FRAME_WIDTH), cam.get(CAP_PROP_FRAME_HEIGHT)));
if (!vw.isOpened())
{
cout << "VideoWriter open failed!" << endl;
getchar();
return -2;
}
cout << "VideoWriter open success!" << endl;
while (1)
{
cam.read(img);
if (img.empty())break;
imshow("cam", img);
vw.write(img);
if (waitKey(5) == 'q')break;
}
waitKey(0);
10、FFmpeg工具的使用
1)抽取音频
2)剪切音频
3)音频合并