首页 > 系统相关 >ubuntu c语言 opencv实现h265 编码

ubuntu c语言 opencv实现h265 编码

时间:2024-03-09 14:12:09浏览次数:24  
标签:编码 opencv h265 frame OpenCV codec fprintf stderr ubuntu

在Ubuntu上使用C语言和OpenCV实现H.265编码,你可以遵循以下步骤:
安装依赖:
首先确保你的系统已经安装了Ubuntu最新版本,并更新所有包列表。
安装FFmpeg,因为OpenCV使用FFmpeg来处理视频编码。可以使用以下命令安装:

复制
sudo apt update
sudo apt install ffmpeg

安装OpenCV:
OpenCV库本身不直接支持H.265编码,但是它可以通过FFmpeg进行编码。你可以从OpenCV官方网站获取源码并编译安装,或者使用包管理器安装。
使用包管理器安装OpenCV:

复制
sudo apt update
sudo apt install libopencv-dev

编写C语言代码:
利用OpenCV的视频处理功能,读取视频帧,然后使用FFmpeg的API进行H.265编码
下面是一个简单的示例代码框架,展示如何使用OpenCV和FFmpeg进行视频编码:

#include <opencv2/opencv.hpp>
#include <ffmpeg/libavcodec/avcodec.h>
#include <ffmpeg/libavformat/avformat.h>
#include <ffmpeg/libavutil/imgutils.h>

int main() {
    // 初始化FFmpeg的编码器
    AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_HEVC);
    if (!codec) {
        fprintf(stderr, " Codec not found \n");
        return -1;
    }
    
    // 打开编码器
    AVCodecContext *codecCtx = avcodec_alloc_context3(codec);
    if (!codecCtx) {
        fprintf(stderr, "could not alloc avcodec context\n");
        return -1;
    }
    
    // 设置编码器参数
    avcodec_get_context_defaults3(codecCtx, codec);
    
    // 打开视频流
    AVFormatContext *formatCtx = NULL;
    if (avformat_alloc_output_context2(&formatCtx, NULL, "mpeg", "output_video") < 0) {
        fprintf(stderr, "Could not create output format context\n");
        return -1;
    }
    
    // 添加流
    AVStream *videoStream = avformat_new_stream(formatCtx, codec);
    if (!videoStream) {
        fprintf(stderr, "Could not create video stream\n");
        return -1;
    }
    
    // 将编码器参数应用到流
    avcodec_parameters_from_context(videoStream->codecpar, codecCtx);
    
    // 打开编码器
    if (avcodec_open2(codecCtx, codec, NULL) < 0) {
        fprintf(stderr, "Could not open codec\n");
        return -1;
    }
    
    // 准备编码
    AVPacket *packet = av_packet_alloc();
    AVFrame *frame = av_frame_alloc();
    int frame_width = 640;
    int frame_height = 480;
    int ret;
    
    // 设置帧参数
    frame->format = codecCtx->pix_fmt;
    frame->width = frame_width;
    frame->height = frame_height;
    frame->pts = 0;
    
    //  alloc the frame data
    ret = av_frame_get_buffer(frame, 32);
    if (ret < 0) {
        fprintf(stderr, "Could not allocate the frame data\n");
        return -1;
    }
    
    // 创建一个用来写入视频的文件流
    AVOutputFormat *outputFormat = NULL;
    outputFormat = av_guess_format("mpeg", NULL, NULL);
    if (!outputFormat) {
        fprintf(stderr, "Could not guess output format

  

 

标签:编码,opencv,h265,frame,OpenCV,codec,fprintf,stderr,ubuntu
From: https://www.cnblogs.com/qiynet/p/18062642

相关文章

  • 04_Ubuntu常用命令第一部分
    Ubuntu常用命令第一部分1.ls命令:功能:查看文件信息Ubuntu里面以“.”开头的文件为隐藏文件-a显示所有文件-l显示文件的所有信息第一列参数代表的含义:代表文件类型d:表示目录文件-:表示普通文件P:管理文件l:链接文件b:块设备文件C:字符设备文件S:套接字文件......
  • 01_Ubuntu启用root用户
    Ubuntu启用root用户1命令行的组成:topeet:当前操作用户Ubuntu:代表主机名~:当前目录名$:代表不是root用户:代表root用户权限2为什么要启用root用户?我们使用Ubuntu系统主要用来做嵌入式开发,不是linux运维,没有必要对root用户过于敏感。系统的权限都要为我们嵌入式开发人......
  • ubuntu22.04编译创龙T113-i mini的SDK
    ubuntu版本22.04.11.解压安装包拷贝sdk并解压出来,注意安装包较大请预留好硬盘空间2.预安装编译应用先安装如下应用,在编译过程中需要使用到的依赖sudoaptinstallbuild-essentialcmakeflexbisonu-boot-toolsopenssllibssl-devtexinfo3.安装和更换python2编译使......
  • ubuntu虚拟机克隆后需要进行的修改
    在虚拟化中,如果采用机器克隆的情况,使用同一个主机名和系统id会造成机器冲突,需要进行主机名修改和机器id重置1.修改系统IP地址信息新版本使用netplan配置主机IP地址ubuntu@ubuntu:~$cat/etc/netplan/00-installer-config.yaml#Thisisthenetworkconfigwrittenby'......
  • ubuntu移除snap组件
    移除ubuntu系统中的snap软件1.查看snap软件信息#使用snaplist查看系统中的snap软件root@ubuntu:~#snaplistNameVersionRevTrackingPublisherNotescore20202401112182latest/stablecanonical✓baselxd5.0.3-9a1d9042......
  • Ubuntu安装Redis
    Ubuntu安装RedisRedis在Ubuntu上是一个开源的内存型数据库,提供了快速、可扩展的键值存储。它使用ANSIC语言编写,支持网络,可以基于内存,也可以持久化。Redis不仅是一个key-value存储系统,还支持丰富的数据类型,如String、list、set、zset、hash等。这使得Redis能够适应不同场景下的存......
  • ubuntu 22.04 安装samba服务
    1.安装软件sudoaptinstallsambasamba-common如果出现类似错误:dpkg:处理软件包samba-common-bin(--configure)时出错参考如下处理:sudosumv/var/lib/dpkg/info/var/lib/dpkg/info_bakmkdir/var/lib/dpkg/infoapt-getupdate&&apt-get-finstallmv/var/l......
  • ubuntu 安装 ros
    Ubuntu16.04sudosh-c'echo"debhttp://packages.ros.org/ros/ubuntu$(lsb_release-sc)main">/etc/apt/sources.list.d/ros-latest.list'sudoapt-keyadv--keyserver'hkp://keyserver.ubuntu.com:80'--recv-keyC1CF6E31E6......
  • C++中OpenCV、Armadillo矩阵数据格式的转换方式
      本文介绍在C++语言中,矩阵库Armadillo的mat、vec格式数据与计算机视觉库OpenCV的Mat格式数据相互转换的方法。  在C++语言的矩阵库Armadillo与计算机视觉库OpenCV中,都有矩阵格式的数据类型;而这两个库在运行能力方面各有千秋,因此实际应用过程中,难免会遇到需要将二者的矩阵格......
  • ubuntu 修改环境变量
    (转自百度AI生成,仅作参考) 在Ubuntu中,可以通过编辑~/.bashrc文件来修改路径。打开终端(Terminal)应用程序。输入以下命令来编辑.bashrc文件:nano~/.bashrc使用键盘上的方向键将光标移动到要添加或更新的行之前。如果没有特定需求,则直接按Ctrl+V进入插入模式。根据需要添加......