[笔记]OpenCV+FFmpeg+Qt实现视频编辑器之OpenCV视频lO接口

简介: [笔记]OpenCV+FFmpeg+Qt实现视频编辑器之OpenCV视频lO接口

一、OpenCV VideoCapture打开摄像头接口讲解和源码分析

VideoCapture

可以打开数据源(文件/摄像机/rtsp流)

以下两种方案打开:

  • bool open ( int index )
  • VideoCapture cap(index)

bool open ( int index )

VideoCapture cap(index)

二、OpenCV VideoCapture打开视频流接口讲解和源码分析

三、VideoCapture release关闭和空间释放源码分析

四、OpenCV read读取一帧视频接口讲解和源码分析

五、使用OpenCV VideoCapture播放视频示例

isOpen判断打开失败原因

1.opencv_ffmpeg.dll 没有放入bin

2. 尚未得知

注意:我使用的opencv-3.4.1/3.4.16 版本 无法打开MP4 我换avi格式 isOpen就成功了,怀疑是x264.dll 以及相关没有导致不支持mp4

读取Frame(解码+转rgb)

Read()

VideoCapture video;
Mat frame;
video.read(frame)

流形式

VideoCapture video;
Mat frame;
video >> frame;

解码+转化颜色

if (!video.grab())
{
  break;
}
//转换颜色格式
if (!video.retrieve(frame))
{
  break;
}

解码和转化颜色分割开来,类似利于实现解码一个线程和渲染一个线程的道理

源码

int main(int argc, char *argv[])
{
  VideoCapture video;
  cv::String strPath = ".\\res\\test.avi";
  video.open(strPath);
  if (!video.isOpened())
  {
    cout << "open video failed!" << endl;
    getchar();
    return -1;
  }
  cout << "open video success!" << endl;
  namedWindow("video");
  Mat frame;
  for (;;)
  {
    //if (!video.read(frame))
    //{
    //  break;
    //}
    //读帧,解码
    if (!video.grab())
    {
      break;
    }
    //转换颜色格式
    if (!video.retrieve(frame))
    {
      break;
    }
    if (frame.empty()) break;
    imshow("video", frame);
    waitKey(30);
  }
  getchar();
  return 0;
}

六、获取视频和相机的属性并分析获取视频属性的源码

获取视频、相机属性

  • CAP_PROP_FPS帧率
  • CAP_PROP_FRAME_COUNT总帧数
  • CAP_PROP_POS_FRAMES 播放帧的位置
  • CAP_PROP_FRAME_WIDTH
  • CAP_PROP_FRAME_HEIGHT

源码

int main(int argc, char *argv[])
{
  VideoCapture video;
  video.open(".\\res\\test.avi");
  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 sec is " << fcount / fps << endl;
  cout << "CAP_PROP_FRAME_WIDTH " << video.get(CAP_PROP_FRAME_WIDTH) << endl;
  cout << "CAP_PROP_FRAME_HEIGHT " << video.get(CAP_PROP_FRAME_HEIGHT) << endl;
  s = s / 2;
  int f = 0;
  for (;;)
  {
    video.read(frame);
    if (frame.empty()) break;
    imshow("video", frame);
    waitKey(s);
  }
  getchar();
  return 0;
}

七、使用opencv实现视频播放位置跳转

建议:使用帧数进行跳转;使用毫秒数跳转,需要先转为帧数,再进行帧数跳转

获得当前帧号

  • CAP PROP POS FRAMES
video.get( CAP PROP POS FRAMES);

设置跳转帧

video.set( CAP PROP POS FRAMES,0);//跳到 第一帧

源码

int main(int argc, char *argv[])
{
  VideoCapture video;
  video.open(".\\res\\test.avi");
  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 sec is " << fcount / fps << endl;
  cout << "CAP_PROP_FRAME_WIDTH " << video.get(CAP_PROP_FRAME_WIDTH) << endl;
  cout << "CAP_PROP_FRAME_HEIGHT " << video.get(CAP_PROP_FRAME_HEIGHT) << endl;
  s = s / 2;
  int f = 0;
  for (;;)
  {
    video.read(frame);
    if (frame.empty()) break;
    int cur = video.get(CAP_PROP_POS_FRAMES);
    if (cur > 90)
    {
      video.set(CAP_PROP_POS_FRAMES, 0);
      continue;
    }
    imshow("video", frame);
    waitKey(s);
  }
  getchar();
  return 0;
}

八、通过VideoWrite的open创建视频文件并分析源码

VideoWriter

open

CV_WRAP virtual bool open(
  const String& filename, 
  int fourcc, 
  double fps,
  Size frameSize,
  bool isColor = true
);

create

static Ptr<IVideoWriter> create(
  const String& filename, 
  int fourcc, 
  double fps,
  Size frameSize, 
  bool isColor = true
);

fourcc格式

九、通过VideoWrite的write创建视频文件并分析源码

VideoWriter

write

CV_WRAP virtual void write(const Mat& image)

writeFrame

十、以h264格式录制并预览摄像机视频代码演示

摄像头打开失败的几个原因

openH264dll少了

openh264开源地址

源码

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
  VideoCapture cam(0);
  if (!cam.isOpened())
  {
    cout << "cam open failed!" << endl;
    getchar();
    return -1;
  }
  cout << "cam open success!" << endl;
  namedWindow("cam");
  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 -1;
  }
  cout << "VideoWriter open success!" << endl;
  for (;;)
  {
    cam.read(img);
    if (img.empty())break;
    imshow("cam", img);
    vw.write(img);
    if (waitKey(5) == 'q') break;
  }
  waitKey(0);
  return 0;
}

十一、使用ffmpeg工具实现音频抽取、剪切和与视频

使用ffmpeg工具抽取音频

ffmpeg.exe -i 1.avi -vn 1.mp3

-i表示源

-vn表示不转换视频

使用ffmpeg剪切音频

ffmpeg -ss 0:0:30 -t 0:0:20 -i input.mp -c copy output.mp

使用ffmpeg工具音视频合并

ffmpeg.exe -i 1.mp3 -i 1.mp4 -c copyout.mp

=========================================

要源码的请私密


相关文章
|
28天前
|
计算机视觉 数据格式
使用opencv在Qt控件上播放mp4文件
使用opencv在Qt控件上播放mp4文件
29 2
|
2月前
|
Web App开发 编解码 安全
视频会议技术 入门探究:WebRTC、Qt与FFmpeg在视频编解码中的应用
视频会议技术 入门探究:WebRTC、Qt与FFmpeg在视频编解码中的应用
168 4
|
2月前
|
存储 编解码 数据处理
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码(三)
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码
37 0
|
2月前
|
存储 编解码 数据处理
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码(二)
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码
41 0
|
2月前
|
存储 编解码 调度
剖析ffmpeg视频解码播放:时间戳的处理
剖析ffmpeg视频解码播放:时间戳的处理
54 0
|
2月前
|
编解码 算法 vr&ar
深度剖析FFmpeg视频解码后的帧处理到Qt显示 从AVFrame到QImage的转换(二)
深度剖析FFmpeg视频解码后的帧处理到Qt显示 从AVFrame到QImage的转换
33 1
|
2月前
|
存储 编解码 算法
深度剖析FFmpeg视频解码后的帧处理到Qt显示 从AVFrame到QImage的转换(一)
深度剖析FFmpeg视频解码后的帧处理到Qt显示 从AVFrame到QImage的转换
66 1
|
2月前
|
存储 算法 编译器
【ffmpeg 到Qt的图片格式转换】精彩的像素:深入解析 AVFrame 到 QImage 的转换
【ffmpeg 到Qt的图片格式转换】精彩的像素:深入解析 AVFrame 到 QImage 的转换
49 0
|
2月前
|
设计模式 存储 缓存
【ffmpeg C++ 播放器优化实战】优化你的视频播放器:使用策略模式和单例模式进行视频优化
【ffmpeg C++ 播放器优化实战】优化你的视频播放器:使用策略模式和单例模式进行视频优化
58 0
|
2月前
|
存储 缓存 编解码
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码(一)
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码
46 0

推荐镜像

更多