当前位置:   article > 正文

Opencv读取写入视频_opencv: ffmpeg: tag 0x47504a4d/'mjpg' is not suppo

opencv: ffmpeg: tag 0x47504a4d/'mjpg' is not supported with codec id 7 and f

描述

调用opencv库来读取写入视频

语言:C++
视频格式:MP4

代码

1. 读取视频

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
int main()
{
	cv::VideoCapture capture;
    cv::Mat frame;
    frame= capture.open("/Users/admin/Desktop/CodeBase/c++_try/1.mp4");// 视频路径
    
    if(!capture.isOpened())
    {
        printf("can not open ...\n");
        return -1;
    }
    while (capture.read(frame))
    {
        int width = frame.size().width;
        int height = frame.size().height;
        
        cv::imshow("origin", frame);
        cv::waitKey(100);
        
    }
    capture.release();
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

2. 写入视频

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
int main()
{
	cv::VideoCapture capture;
    cv::Mat frame;
    frame= capture.open("/Users/admin/Desktop/CodeBase/c++_try/4.mp4");
    double fps = capture.get(cv::CAP_PROP_FPS);
    cv::VideoWriter writer("/Users/admin/Desktop/CodeBase/c++_try/out.mp4", cv::VideoWriter::fourcc('m','p','4','v'), fps, cv::Size(1280, 720));
       
    if(!capture.isOpened())
    {
        printf("can not open ...\n");
        return -1;
    }
    while (capture.read(frame))
    {
        int width = frame.size().width;
        int height = frame.size().height;
        
        writer << frame;
        cv::imshow("origin", frame);
        cv::waitKey(100);
        
    }
    capture.release();
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

关于视频格式

代码中有一句话,定义了一个cv::VideoWriter负责写入视频

cv::VideoWriter writer("/Users/admin/Desktop/CodeBase/c++_try/out.mp4", cv::VideoWriter::fourcc('m','p','4','v'), fps, cv::Size(1280, 720));
  • 1

参数也好理解:路径、编码格式、帧率、图像大小

  • 我找了一些资料:

    cv::VideoWriter::fourcc(‘I’, ‘4’, ‘2’, ‘0’),该参数是YUV编码类型,文件名后缀为.avi
    cv::VideoWriter::fourcc(‘P’, ‘I’, ‘M’, ‘I’),该参数是MPEG-1编码类型,文件名后缀为.avi
    cv::VideoWriter::fourcc(‘X’, ‘V’, ‘I’, ‘D’),该参数是MPEG-4编码类型,文件名后缀为.avi
    cv::VideoWriter::fourcc(‘T’, ‘H’, ‘E’, ‘O’),该参数是Ogg Vorbis,文件名后缀为.ogv
    cv::VideoWriter::fourcc(‘F’, ‘L’, ‘V’, ‘1’),该参数是Flash视频,文件名后缀为.flv

我还看到有人是这么写的
cv::VideoWriter::fourcc(‘M’, ‘J’, ‘P’, ‘G’),文件名后缀是.avi

报错

在执行代码时,提示过几次错误

OpenCV: FFMPEG: tag 0x00000898/'???' is not found (format 'avi / AVI (Audio Video Interleaved)
  • 1

OpenCV: FFMPEG: tag 0x47504a4d/'MJPG' is not supported with codec id 7 and format 'mp4 / MP4
  • 1

最后排查,都是上面提到的视频编码格式的问题cv::VideoWriter::fourcc,写代码时请注意你要的视频后缀需要和编码格式对应

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/484793
推荐阅读
相关标签
  

闽ICP备14008679号