OpenCV视频I/O(18)视频写入类VideoWriter之初始化 VideoWriter 对象的函数open()的使用

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

初始化或重新初始化视频编写器。

该方法打开视频编写器。参数与构造函数 VideoWriter::VideoWriter 中的相同。

cv::VideoWriter::open() 函数用于初始化 VideoWriter 对象,使其能够将视频帧写入文件或视频流。

函数原型1

cpp 复制代码
virtual bool cv::VideoWriter::open
(
	const String & 	filename,
	int 	fourcc,
	double 	fps,	
	Size 	frameSize,	
	bool 	isColor = true 
)		

参数1

  • 参数 filename:输出视频文件的路径或名称。
  • 参数 fourcc:四字符代码(FourCC code),用于指定视频编码器。
  • 参数fps:视频的帧率(每秒帧数)。
  • 参数frameSize:视频帧的大小(宽度和高度)。
  • 参数isColor:布尔值,表示视频是否为彩色,默认值为 true(彩色视频)。

函数原型2

这是一个重载的成员函数,提供方便。它与上述函数的不同仅在于接受的参数。

cpp 复制代码
bool cv::VideoWriter::open
(
	const String & 	filename,
	int 	apiPreference,
	int 	fourcc,
	double 	fps,
	Size 	frameSize,
	bool 	isColor = true 
)		

函数原型3

这是一个重载的成员函数,提供方便。它与上述函数的不同仅在于接受的参数。

cpp 复制代码
bool cv::VideoWriter::open	 
(
	const String & 	filename,
	int 	fourcc,
	double 	fps,
	const Size & 	frameSize,
	const std::vector< int > & 	params 
)	

函数原型4

cpp 复制代码
bool cv::VideoWriter::open	
(
	const String & 	filename,
	int 	apiPreference,
	int 	fourcc,
	double 	fps,
	const Size & 	frameSize,
	const std::vector< int > & 	params 
)		

代码示例

cpp 复制代码
#include <iostream>
#include <opencv2/opencv.hpp>
#include <fstream>


int main()
{
    // 设置视频的宽度和高度
    int frameWidth  = 640;
    int frameHeight = 480;

    // 设置视频编码器的 FourCC 代码
    // 使用 XVID 编码器作为替代方案
    int fourcc = cv::VideoWriter::fourcc( 'X', 'V', 'I', 'D' );

    // 创建 VideoWriter 对象
    cv::VideoWriter writer;

    // 初始化 VideoWriter 对象
    bool isOpened = writer.open( "output.avi", fourcc, 25, cv::Size( frameWidth, frameHeight ), true );

    if ( !isOpened )
    {
        std::cerr << "Failed to initialize the video writer." << std::endl;
        return -1;
    }

    // 创建一个示例帧
    cv::Mat frame = cv::Mat::zeros( frameHeight, frameWidth, CV_8UC3 );

    // 写入一帧到视频文件
    writer.write( frame );

    // 检查视频文件是否存在
    std::ifstream file( "output.avi" );
    if ( file.good() )
    {
        std::cout << "Video file created successfully." << std::endl;
    }
    else
    {
        std::cerr << "Failed to create video file." << std::endl;
    }

    // 关闭文件流
    file.close();

    // 释放资源
    writer.release();

    return 0;
}

运行结果

bash 复制代码
Video file created successfully.
相关推荐
shandianchengzi3 小时前
【记录】Windows11笔记本插入光驱和CD只转盘,没有盘符,不能读取
音视频·音频·媒体·cd·光驱·光盘·dvd
FlightYe1 天前
音视频修炼之容器与协议(六):HTTP 协议
android·linux·网络·网络协议·算法·http·音视频
JosieBook1 天前
【鸿蒙】把一个 Qt 桌面视频编辑器“搬“上鸿蒙 PC:Olive 移植适配全记录
qt·音视频·harmonyos
Ryan-Zhong1 天前
AI视频切片 API 怎么用?长视频拆条、高光识别与自动分发接口实测
服务器·数据库·音视频
ck0135792 天前
视频如何添加背景音乐?音视频合并与音频混合的区别
人工智能·音视频·音频·实时音视频·视频
zhanlang02172 天前
HTML 零基础入门到企业实战【第八章 8.6】HTML5 本地存储、拖拽 API、视频音频新特性
html·音视频·html5
晓得迷路了2 天前
栗子前端技术周刊第 155 期 - 2026 HTML 现状调查报告、Solid 2.0...
前端·javascript·webpack
玖米GEO运营王总监3 天前
成都AI数字人视频怎么批量生产?流程与合规标注
大数据·人工智能·音视频
八月的雨季 最後的冰吻4 天前
音视频会议梳理
音视频
合尘猫5 天前
用 6GB 显卡搭短视频产线:MiniMax H3 + ffmpeg 全流程实录(55 秒片 / 40 分钟)
人工智能·音视频