ffmpeg 结合 opencv 显示ps流文件

存储的ps 流文件如何显示

使用ffmpeg 和 opencv 做demo

结合opencv 和 ffmpeg 显示ps文件

// showps.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <opencv2/opencv.hpp>extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
#ifdef _WIN32
#pragma comment(lib,"avutil.lib")
#pragma comment(lib,"avcodec.lib")
#pragma comment(lib,"avformat.lib")
#pragma comment(lib,"swscale.lib")
#endif
#ifdef _DEBUG
#pragma comment(lib,"opencv_world490d.lib")
#else
#pragma comment(lib,"opencv_world490.lib")
#endif#define AV_ERROR_REPORT char buffer[64];\
av_strerror(ret, &buffer[0], 64);\
std::cerr << "Error sending packet to decoder: " << buffer << std::endl
int main() {// Initialize FFmpeg// Open video fileAVFormatContext* format_context = avformat_alloc_context();const AVInputFormat* input_format = av_find_input_format("mpeg");if (!input_format) {std::cerr << "Failed to find input format" << std::endl;return -1;}format_context->iformat = input_format;if (avformat_open_input(&format_context, "o1.ps", nullptr, nullptr) != 0) {std::cerr << "Failed to open input file" << std::endl;return -1;}if (avformat_find_stream_info(format_context, nullptr) < 0) {std::cerr << "Failed to find stream info" << std::endl;avformat_close_input(&format_context);return -1;}// Find video streamint video_stream_index = -1;for (unsigned int i = 0; i < format_context->nb_streams; ++i) {if (format_context->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {video_stream_index = i;break;}}video_stream_index = 0;if (video_stream_index == -1) {std::cerr << "No video stream found" << std::endl;avformat_close_input(&format_context);return -1;}// Find decoder for the streamconst AVCodec* codec = avcodec_find_decoder(format_context->streams[video_stream_index]->codecpar->codec_id);if (!codec) {std::cerr << "Failed to find decoder for video stream" << std::endl;avformat_close_input(&format_context);return -1;}// Create codec context for the decoderAVCodecContext* codec_context = avcodec_alloc_context3(codec);if (!codec_context) {std::cerr << "Failed to allocate codec context" << std::endl;avformat_close_input(&format_context);return -1;}if (avcodec_parameters_to_context(codec_context, format_context->streams[video_stream_index]->codecpar) < 0) {std::cerr << "Failed to copy codec parameters to codec context" << std::endl;avcodec_free_context(&codec_context);avformat_close_input(&format_context);return -1;}if (avcodec_open2(codec_context, codec, nullptr) < 0) {std::cerr << "Failed to open codec" << std::endl;avcodec_free_context(&codec_context);avformat_close_input(&format_context);return -1;}// Allocate video frame and initialize SwsContextAVFrame* frame = av_frame_alloc();if (!frame) {std::cerr << "Failed to allocate frame" << std::endl;avcodec_free_context(&codec_context);avformat_close_input(&format_context);return -1;}struct SwsContext* sws_context = sws_getContext(codec_context->width, codec_context->height, codec_context->pix_fmt,codec_context->width, codec_context->height, AV_PIX_FMT_BGR24,SWS_BILINEAR, nullptr, nullptr, nullptr);if (!sws_context) {std::cerr << "Failed to initialize SwsContext" << std::endl;av_frame_free(&frame);avcodec_free_context(&codec_context);avformat_close_input(&format_context);return -1;}// Read frames from the stream and display using OpenCVAVPacket packet;while (av_read_frame(format_context, &packet) >= 0) {if (packet.stream_index == video_stream_index) {// Decode the frameint ret = avcodec_send_packet(codec_context, &packet);if (ret < 0) {AV_ERROR_REPORT;break;}while (ret >= 0) {ret = avcodec_receive_frame(codec_context, frame);if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)break;else if (ret < 0) {AV_ERROR_REPORT;break;}// Convert frame to BGRAVFrame* bgr_frame = av_frame_alloc();if (!bgr_frame) {std::cerr << "Failed to allocate BGR frame" << std::endl;break;}bgr_frame->width = codec_context->width;bgr_frame->height = codec_context->height;bgr_frame->format = AV_PIX_FMT_BGR24;av_frame_get_buffer(bgr_frame, 0);sws_scale(sws_context, frame->data, frame->linesize, 0, codec_context->height,bgr_frame->data, bgr_frame->linesize);// Convert AVFrame to OpenCV Matcv::Mat bgrMat(codec_context->height, codec_context->width, CV_8UC3, bgr_frame->data[0]);// Display using OpenCVcv::imshow("Frame", bgrMat);cv::waitKey(1);// Free resourcesav_frame_free(&bgr_frame);}}av_packet_unref(&packet);}// Free resourcesav_frame_free(&frame);sws_freeContext(sws_context);avcodec_free_context(&codec_context);avformat_close_input(&format_context);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/55131.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【openwrt-21.02】T750 openwrt switch划分VLAN之后网口插拔状态异常问题分析及解决方案

Openwrt版本 NAME="OpenWrt" VERSION="21.02-SNAPSHOT" ID="openwrt" ID_LIKE="lede openwrt" PRETTY_NAME="OpenWrt 21.02-SNAPSHOT" VERSION_ID="21.02-snapshot" HOME_URL="https://openwrt.org/" …

第100+26步 ChatGPT学习:概率校准 Bayesian Binning into Quantiles

基于Python 3.9版本演示 一、写在前面 最近看了一篇在Lancet子刊《eClinicalMedicine》上发表的机器学习分类的文章&#xff1a;《Development of a novel dementia risk prediction model in the general population: A large, longitudinal, population-based machine-learn…

【2】图像视频的加载和显示

文章目录 【2】图像视频的加载和显示一、代码在哪写二、创建和显示窗口&#xff08;一&#xff09;导入OpenCV的包cv2&#xff08;二&#xff09;创建窗口&#xff08;三&#xff09;更改窗口大小 & 显示窗口&#xff08;四&#xff09;等待用户输入补充&#xff1a;ord()函…

Gateway和VirtualService

在 Istio 服务网格中&#xff0c;Gateway 和 VirtualService 是两个关键的配置对象&#xff0c;它们分别用于定义入站流量的接入点和路由规则。下面详细介绍这两个配置对象的功能及其相互关系。 Gateway Gateway 是 Istio 中用于定义入站流量接入点的配置对象。它描述了外部流…

正则表达式的使用规则

1.介绍 正则表达式&#xff08;Regular Expression&#xff09;是一种强大的文本模式匹配工具&#xff0c;它使用单个字符串来描述、匹配一系列符合某个句法规则的字符串搜索模式。正则表达式可以用于文本搜索、文本替换等场景。 2.基本语法 正则表达式由普通字符&#xff0…

vue框架学习-- 父子页面 参数方法调用

一、父组件向子组件传递参数 在Vue中&#xff0c;父组件向子组件传递参数是一种非常常见的通信方式&#xff0c;这通常通过props来实现。props允许父组件向子组件传递数据&#xff0c;并且这些数据是单向的&#xff0c;即子组件不能直接修改由父组件传递的数据。但是&#xff…

【Unity踩坑】Unity更新Google Play结算库

一、问题描述&#xff1a; 在Google Play上提交了app bundle后&#xff0c;提示如下错误。 我使用的是Unity 2022.01.20f1&#xff0c;看来用的Play结算库版本是4.0 查了一下文档&#xff0c;Google Play结算库的维护周期是两年。现在需要更新到至少6.0。 二、更新过程 1. 下…

【视频目标分割-2024CVPR】Putting the Object Back into Video Object Segmentation

Cutie 系列文章目录1 摘要2 引言2.1背景和难点2.2 解决方案2.3 成果 3 相关方法3.1 基于记忆的VOS3.2对象级推理3.3 自动视频分割 4 工作方法4.1 overview4.2 对象变换器4.2.1 overview4.2.2 Foreground-Background Masked Attention4.2.3 Positional Embeddings 4.3 Object Me…

cpp,git,unity学习

c#中的? 1. 空值类型&#xff08;Nullable Types&#xff09; ? 可以用于值类型&#xff08;例如 int、bool 等&#xff09;&#xff0c;使它们可以接受 null。通常&#xff0c;值类型不能为 null&#xff0c;但是通过 ? 可以表示它们是可空的。 int? number null; // …

使用 SSH 连接 Docker 服务器:IntelliJ IDEA 高效配置与操作指南

使用 SSH 连接 Docker 服务器&#xff1a;IntelliJ IDEA 高效配置与操作指南 本文详细介绍了如何在 2375 端口未开放的情况下&#xff0c;通过 SSH 连接 Docker 服务器并在 Idea 中进行开发。通过修改用户权限、生成密钥对以及配置 SSH 访问&#xff0c;用户可以安全地远程操作…

NASA数据集:ATLAS/ICESat-2 L3B 每日和每月网格化海冰自由面高度,第 4 版

目录 简介 摘要 代码 引用 网址推荐 0代码在线构建地图应用 机器学习 ATLAS/ICESat-2 L3B Daily and Monthly Gridded Sea Ice Freeboard, Version 4 简介 ATLAS/ICESat-2 L3B Daily and Monthly Gridded Sea Ice Freeboard, Version 4数据是由NASA的ATLAS&#xff08…

Ubuntu 系统崩了,如何把数据拷下来

问题描述&#xff1a; Linux系统中安装输入法后&#xff0c;重启后&#xff0c;导致系统无法进入&#xff0c;进入 recovery mode下的resume 也启动不了&#xff0c;所以决定将需要的东西复制到U盘 解决方案&#xff1a; 1.重启ubuntu&#xff0c;随即点按Esc进入grub菜单&am…

OpenStack Yoga版安装笔记(十五)Horizon安装

1、官方文档 OpenStack Installation Guidehttps://docs.openstack.org/install-guide/ 本次安装是在Ubuntu 22.04上进行&#xff0c;基本按照OpenStack Installation Guide顺序执行&#xff0c;主要内容包括&#xff1a; 环境安装 &#xff08;已完成&#xff09;OpenStack…

HarmonyOS/OpenHarmony Audio 实现音频录制及播放功能

关键词&#xff1a;audio、音频录制、音频播放、权限申请、文件管理 在app的开发过程中时常会遇见一些需要播放一段音频或进行语音录制的场景&#xff0c;那么本期将介绍如何利用鸿蒙 audio 模块实现音频写入和播放的功能。本次依赖的是 ohos.multimedia.audio 音频管理模块&am…

AI日常绘画【国庆海报】:盛世迎华诞,Flux国庆节海报制作教程

大家好我是极可菌&#xff01;&#xff01;&#xff01; 马上就要到祖国母亲的节日了&#xff0c;想想心里都美滋滋的&#xff0c;终于可以放松一下了。相信AI绘画关于国庆主题肯定也会精彩纷呈吧&#xff0c;今天和大家分享几组关于国庆海报的制作教程。 本文使用基于Flux的相…

windows 驱动实例分析系列-定时日志的COM驱动

本文章的前置文章为: windows 驱动编写原则 windows COM驱动 案例 windows COM驱动的I/O处理 在前面的设计中,主要是对windows提供的VirtualSerial源代码的讲解,但是那个驱动其实是一个空壳驱动,用于学习的,在I/O处理中,也讲述了serial I/O处理的本质,接下来会将这些…

Springboo通过http请求下载文件到服务器

这个方法将直接处理从URL下载数据并将其保存到文件的整个过程。下面是一个这样的方法示例&#xff1a; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection…

【EXCEL数据处理】000009 案列 EXCEL单元格数字格式。文本型数字格式和常规型数字格式的区别

前言&#xff1a;哈喽&#xff0c;大家好&#xff0c;今天给大家分享一篇文章&#xff01;创作不易&#xff0c;如果能帮助到大家或者给大家一些灵感和启发&#xff0c;欢迎收藏关注哦 &#x1f495; 目录 【EXCEL数据处理】000009 案列 EXCEL单元格数字格式。文本型数字格式和…

python实用脚本(二):删除xml标签下的指定类别

介绍 在目标检测中&#xff0c;有些时候会遇到标注好的类别不想要了的情况&#xff0c;这时我们可以运行下面的代码来批量删除不需要的类别节省时间。 代码实现&#xff1a; import argparseimport xml.etree.ElementTree as ET import osclasses [thin_smoke]def GetImgNam…

如何使用SCCMSecrets识别SCCM策略中潜在的安全问题

关于SCCMSecrets SCCMSecrets是一款针对SCCM策略的安全扫描与检测工具&#xff0c;该工具旨在提供一种有关 SCCM 策略的全面安全检测方法。 该工具可以从各种权限级别执行&#xff0c;并将尝试发现与策略分发相关的潜在错误配置。除了分发点上托管的包脚本外&#xff0c;它还将…