当前位置: 首页 > news >正文

[ffmpeg] 音视频编码

本文主要梳理 ffmpeg 中音视频编码的常用函数

API调用

常用 API

const AVCodec *avcodec_find_encoder(enum AVCodecID id);
AVCodecContext *avcodec_alloc_context3(const AVCodec *codec);
void avcodec_free_context(AVCodecContext **avctx);
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options); 
int avcodec_close(AVCodecContext *avctx); 
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame);
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);

class

static const AVClass av_codec_context_class = {.class_name              = "AVCodecContext",.item_name               = context_to_name,.option                  = avcodec_options,.version                 = LIBAVUTIL_VERSION_INT,.log_level_offset_offset = offsetof(AVCodecContext, log_level_offset),.child_next              = codec_child_next,.child_class_iterate     = codec_child_class_iterate,.category                = AV_CLASS_CATEGORY_ENCODER,.get_category            = get_category,
};

初始化和销毁

avcodec_find_encoder 找到编码器
avcodec_alloc_context3 创建 context 结构体
视频:设置编码器参数,码率,宽高,时间基,帧率,GOP,视频格式,编码器ID
avcodec_open2 打开编码器
avcodec_close 关闭编码器
avcodec_free_context 销毁上下文

编码

avcodec_send_frame
avcodec_receive_packet
avcodec_receive_packet 传出来的 packet 使用结束需要调用 av_packet_unref 让 packet 的引用计数减一,不然会有内存泄漏

demo

m_vc = avcodec_alloc_context3(codec);
m_vc->bit_rate = m_vBitrate;
m_vc->width = m_outWidth;
m_vc->height = m_outHeight;
// 时间基数
m_vc->time_base = { 1, m_outFPS };
m_vc->framerate = { m_outFPS, 1 };m_vc->gop_size = 50;
m_vc->max_b_frames = 0;m_vc->pix_fmt = AV_PIX_FMT_YUV420P;
m_vc->codec_id = AV_CODEC_ID_H264;
av_opt_set(m_vc->priv_data, "preset", "superfast", 0);
m_vc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;int ret = avcodec_open2(m_vc, codec, NULL);
ret = avcodec_send_frame(m_vc, m_yuv);
p = av_packet_alloc();
ret = avcodec_receive_packet(m_vc, p);
if (ret != 0 || p->size <= 0)
{av_packet_free(&p);return NULL;
}
//xxx
av_packet_unref(p);if (m_vc)
{avcodec_close(m_vc);avcodec_free_context(&m_vc);
}

其他

codec.h 所有API

const AVCodec *av_codec_iterate(void **opaque);
const AVCodec *avcodec_find_decoder(enum AVCodecID id);
const AVCodec *avcodec_find_decoder_by_name(const char *name);
const AVCodec *avcodec_find_encoder(enum AVCodecID id);
const AVCodec *avcodec_find_encoder_by_name(const char *name);
int av_codec_is_encoder(const AVCodec *codec);
int av_codec_is_decoder(const AVCodec *codec);
const char *av_get_profile_name(const AVCodec *codec, int profile);
const AVCodecHWConfig *avcodec_get_hw_config(const AVCodec *codec, int index);

avcodec.h 所有API

unsigned avcodec_version(void);
const char *avcodec_configuration(void);
const char *avcodec_license(void);
AVCodecContext *avcodec_alloc_context3(const AVCodec *codec);
void avcodec_free_context(AVCodecContext **avctx);
const AVClass *avcodec_get_class(void);
const AVClass *avcodec_get_frame_class(void);
const AVClass *avcodec_get_subtitle_rect_class(void); 
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec); 
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par);
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options); 
int avcodec_close(AVCodecContext *avctx); 
void avsubtitle_free(AVSubtitle *sub); 
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags); 
int avcodec_default_get_encode_buffer(AVCodecContext *s, AVPacket *pkt, int flags);
void avcodec_align_dimensions(AVCodecContext *s, int *width, int *height);
void avcodec_align_dimensions2(AVCodecContext *s, int *width, int *height, int linesize_align[AV_NUM_DATA_POINTERS]);
int avcodec_enum_to_chroma_pos(int *xpos, int *ypos, enum AVChromaLocation pos);
enum AVChromaLocation avcodec_chroma_pos_to_enum(int xpos, int ypos);
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, AVPacket *avpkt);
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt);
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame);
int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame);
int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);
int avcodec_get_hw_frames_parameters(AVCodecContext *avctx, AVBufferRef *device_ref, enum AVPixelFormat hw_pix_fmt, AVBufferRef **out_frames_ref);
const AVCodecParser *av_parser_iterate(void **opaque);
AVCodecParserContext *av_parser_init(int codec_id);
int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int64_t pts, int64_t dts, int64_t pos);
void av_parser_close(AVCodecParserContext *s);
int avcodec_encode_subtitle(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVSubtitle *sub);
unsigned int avcodec_pix_fmt_to_codec_tag(enum AVPixelFormat pix_fmt);
enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(const enum AVPixelFormat *pix_fmt_list, enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr);
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat * fmt);
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode);
int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2),void *arg, int *ret, int count, int size);
int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count);
int avcodec_fill_audio_frame(AVFrame *frame, int nb_channels, enum AVSampleFormat sample_fmt, const uint8_t *buf, int buf_size, int align);
void avcodec_flush_buffers(AVCodecContext *avctx);
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes);
void av_fast_padded_malloc(void *ptr, unsigned int *size, size_t min_size);
void av_fast_padded_mallocz(void *ptr, unsigned int *size, size_t min_size);
int avcodec_is_open(AVCodecContext *s);

相关文章:

[ffmpeg] 音视频编码

本文主要梳理 ffmpeg 中音视频编码的常用函数 API调用 常用 API const AVCodec *avcodec_find_encoder(enum AVCodecID id); AVCodecContext *avcodec_alloc_context3(const AVCodec *codec); void avcodec_free_context(AVCodecContext **avctx); int avcodec_open2(AVCode…...

springboot+redis+缓存

整合 添加依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId> </dependency> 连接redis&#xff0c;配置yml文件 主机 端口号 数据库是哪一个 密码 配置类 p…...

关于http的206状态码和416状态码的意义、断点续传以及CORS使用Access-Control-Allow-Origin来允许跨域请求

一、关于http的206状态码和416状态码的意义及断点续传 HTTP 2xx范围内的状态码表明客户端发送的请求已经被服务器接受并且被成功处理了,HTTP/1.1 206状态码表示客户端通过发送范围请求头Range抓取到了资源的部分数据&#xff0c;一般用来解决大文件下载问题&#xff0c;一般CDN…...

SOMEIP_ETS_114: SD_Entries_Length_wrong_combined

测试目的&#xff1a; 验证DUT能够拒绝一个包含两个正确条目但条目数组长度不正确的SubscribeEventgroup消息&#xff0c;并以SubscribeEventgroupNAck作为响应。 描述 本测试用例旨在确保DUT遵循SOME/IP协议&#xff0c;当接收到一个条目数组长度与实际条目数量不匹配的Sub…...

SQL:DATEDIFF函数

DATEDIFF函数是用于计算两个日期之间的时间间隔的函数&#xff0c;它在不同的编程语言和数据库系统中都有广泛的应用。以下是对DATEDIFF函数的详细解析&#xff1a; 一、函数用途 DATEDIFF函数的主要用途是计算两个日期之间的时间间隔&#xff0c;这个间隔可以是年、季度、月…...

MATLAB 可视化基础:绘图命令与应用

目录 1. 绘制子图1.1基本绘图命令1.2. 使用 subplot 函数1.3. 绘图类型 2.MATLAB 可视化进阶(以下代码均居于以上代码的数据定义上实现)2.1. 极坐标图2.3. 隐函数的绘制 3.总结 在数据分析和科学计算中&#xff0c;数据可视化是理解和解释结果的关键工具。今天&#xff0c;我将…...

掌握 Python 异常处理的实战技巧:从基础到高级应用20240918

掌握 Python 异常处理的实战技巧&#xff1a;从基础到高级应用 引言 在 Python 编程中&#xff0c;异常处理是保障代码稳健性和可靠性的关键要素之一。无论是在网络请求、资源访问&#xff0c;还是复杂的业务逻辑中&#xff0c;异常处理都不可或缺。本文将从 Python 异常的基…...

One API 部署与配置指南

技术文档&#xff1a;One API 部署与配置指南 概述 One API 是一个多功能的 API 管理平台&#xff0c;支持自定义设置、用户管理、多种登录注册方式、主题切换等。本文档提供了详细的部署和配置指南&#xff0c;帮助用户快速搭建和使用 One API。 部署 基于 Docker 部署 D…...

国密起步7:BouncyCastle使用SM4自定义格式加解密C#版

初级代码游戏的专栏介绍与文章目录-CSDN博客 我的github&#xff1a;codetoys&#xff0c;所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。 这些代码大部分以Linux为目标但部分代码是纯C的&#xff0c;可以在任何平台上使用。 github源码指引的指引-CSDN博…...

Qt优秀开源项目之二十三:QSimpleUpdater

QSimpleUpdater是开源的自动升级模块&#xff0c;用于检测、下载和安装更新。 github地址&#xff1a;https://github.com/alex-spataru/QSimpleUpdater QSimpleUpdater目前Star不多&#xff08;911个&#xff09;&#xff0c;但已在很多开源项目看到其身影&#xff0c;比如Not…...

使用 Nmap 进行 SSL/TLS 加密套件枚举

1. Nmap 简介 Nmap&#xff08;Network Mapper&#xff09;是一个开源的网络探测和安全审计工具。它广泛用于扫描网络并发现设备、端口及服务&#xff0c;同时也支持多种脚本来进行更高级的安全扫描。Nmap 的 -sV 参数可以用于探测开放端口上的服务及版本信息&#xff0c;配合…...

探索 Python 的火焰:Fire 库的神秘力量

文章目录 &#x1f525; 探索 Python 的火焰&#xff1a;Fire 库的神秘力量第一部分&#xff1a;背景介绍第二部分&#xff1a;Fire 库是什么&#xff1f;第三部分&#xff1a;如何安装 Fire&#xff1f;第四部分&#xff1a;简单库函数使用方法第五部分&#xff1a;场景应用第…...

【Day14-单例设计模式动态代理】

单例设计模式 什么是设计模式&#xff08;Design pattern&#xff09; ? 一个问题通常有n种解法&#xff0c;其中肯定有一种解法是最优的&#xff0c;这个最优的解法被人总结出来了&#xff0c;称之为设计模式。设计模式有20多种&#xff0c;对应20多种软件开发中会遇到的问题…...

代码随想录训练营Day7 | 454.四数相加II | 383. 赎金信 | 15. 三数之和 | 18. 四数之和

代码随想录 (programmercarl.com) Leetcode 454. 四数相加 II 题目描述 给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) &#xff0c;使得 A[i] B[j] C[k] D[l] 0。 为了使问题简单化&#xff0c;所有的 A, B, C, D 具有相同的长度 N&#…...

C++和OpenGL实现3D游戏编程【目录】

欢迎来到zhooyu的专栏。 个人主页&#xff1a;【zhooyu】 文章专栏&#xff1a;【OpenGL实现3D游戏编程】 贝塞尔曲面演示&#xff1a; 贝塞尔曲面演示zhooyu 本专栏内容&#xff1a; 我们从游戏的角度出发&#xff0c;用C去了解一下游戏中的功能都是怎么实现的。这一切还是要…...

03-Mac系统PyCharm主题设置

目录 1. 打开PyCharm窗口 2. Mac左上角点击PyCharm&#xff0c;点击Settings 3. 点击第一项Appearance& Behavior 4. 点击Appearance 5. 找到Theme进行设置 1. 打开PyCharm窗口 2. Mac左上角点击PyCharm&#xff0c;点击Settings 3. 点击第一项Appearance& Behavi…...

Java并发的四大定律

每一个进入 Java 并发世界的人&#xff0c;都会不可避免地面临一系列问题&#xff1a;线程安全、并发控制、锁&#xff0c;以及共享资源。这些概念复杂又抽象&#xff0c;往往让人无从下手。幸运的是&#xff0c;业界早已总结出一些法则&#xff0c;这些法则为我们处理并发问题…...

java项目之基于springboot的贸易行业crm系统(源码+文档)

风定落花生&#xff0c;歌声逐流水&#xff0c;大家好我是风歌&#xff0c;混迹在java圈的辛苦码农。今天要和大家聊的是一款基于springboot的基于springboot的贸易行业crm系统。项目源码以及部署相关请联系风歌&#xff0c;文末附上联系信息 。 项目简介&#xff1a; 基于sp…...

General OCR Theory: Towards OCR-2.0 via a Unified End-to-end Model

摘要 传统的OCR系统&#xff08;OCR-1.0&#xff09;越来越无法满足人们对智能处理人造光学字符的需求。在本文中&#xff0c;我们将所有人造光学信号&#xff08;例如&#xff0c;普通文本、数学/分子公式、表格、图表、乐谱&#xff0c;甚至是几何形状&#xff09;统称为“字…...

二十种编程语言庆祝中秋节

二十种编程语言庆祝中秋节 文章目录 二十种编程语言庆祝中秋节中秋快乐&#xff01;家人们 &#x1f973;一 Python二 C三 C四 Java五 C#六 Perl七 Go八 Asp九 PHP十 JavaScript十一 JavaScript HTML十二 Visual Basic十三 早期 VB十四 Visual C十五 Delphi十六 Shell十七 Cobo…...

Ubuntu系统优化下的LiuJuan20260223Zimage高性能部署

Ubuntu系统优化下的LiuJuan20260223Zimage高性能部署 本文基于Ubuntu 22.04 LTS系统测试&#xff0c;适用于NVIDIA GPU环境 1. 环境准备与系统优化 在开始部署LiuJuan20260223Zimage之前&#xff0c;我们先对Ubuntu系统进行一些基础优化&#xff0c;这些调整能让后续的模型运行…...

5分钟搞定电脑风扇噪音!FanControl超详细配置指南让你告别“飞机起飞“

5分钟搞定电脑风扇噪音&#xff01;FanControl超详细配置指南让你告别"飞机起飞" 【免费下载链接】FanControl.Releases This is the release repository for Fan Control, a highly customizable fan controlling software for Windows. 项目地址: https://gitcod…...

告别996!我用Qoder AI编程平台,一天搞定全栈电商项目(附保姆级实战流程)

从零到上线&#xff1a;Qoder AI全栈电商项目实战手记 凌晨三点的显示器蓝光里&#xff0c;我第17次调试购物车接口时&#xff0c;咖啡杯底黏着的便签写着"再熬三天就能交付"。这个典型的程序员996场景&#xff0c;在上个月使用Qoder开发新电商平台时被彻底颠覆——从…...

无需配置环境!MinerU镜像一键部署,即刻体验智能文档解析

无需配置环境&#xff01;MinerU镜像一键部署&#xff0c;即刻体验智能文档解析 1. 为什么选择智能文档解析&#xff1f; 在日常办公和学习中&#xff0c;我们经常需要处理各种文档资料&#xff1a;PDF报告、扫描合同、学术论文、财务报表等。传统方式要么需要手动输入&#…...

基于zlmediakit的RTSP流媒体服务器嵌入式开发指南

1. 为什么选择zlmediakit作为嵌入式RTSP服务器 第一次接触流媒体开发时&#xff0c;我试过用FFmpeg直接搭建服务&#xff0c;结果被复杂的协议栈和线程管理折腾得够呛。后来发现zlmediakit这个宝藏项目&#xff0c;它把RTSP/RTMP/HTTP-FLV等协议封装得特别友好&#xff0c;特别…...

告别手动更新!用Python+Pandas快速解析通达信tnf文件,构建本地股票代码库

用PythonPandas高效解析通达信TNF文件&#xff1a;打造自动化股票代码库 每次手动更新股票代码库时&#xff0c;那些重复性操作总让我想起学生时代抄写课文的场景——机械、耗时且容易出错。作为量化研究员&#xff0c;我们真正需要的是把时间花在策略优化上&#xff0c;而不是…...

告别杀后台!深度评测Ba-KeepAlive-U:这款UniAppX安卓保活插件到底有多强?(附多机型测试结果)

Ba-KeepAlive-U技术解析&#xff1a;如何为UniAppX应用实现跨机型保活方案 在移动应用开发领域&#xff0c;后台进程存活率一直是困扰开发者的技术难题。尤其对于需要持续运行定位、即时通讯或数据同步功能的应用&#xff0c;系统资源管理策略导致的"杀后台"现象直接…...

ai辅助硬件设计:让快马智能解析并生成db9接口与mcu连接的完整原理图与代码

在硬件开发中&#xff0c;DB9接口的设计与连接是个常见但容易出错的环节。最近我在一个嵌入式项目里需要实现STM32与DB9接口的RS-232通信&#xff0c;发现传统设计流程存在几个痛点&#xff1a; 引脚定义容易混淆 DB9公头和母头的引脚定义是相反的&#xff0c;比如母头的2号引脚…...

【Feign】⭐️ 混合编码实战:SpringFormEncoder 同时支持 MultipartFile 与 @RequestBody 参数传递

1. 混合编码场景下的Feign实战痛点 最近在重构微服务项目时&#xff0c;遇到个特别典型的场景&#xff1a;服务A需要调用服务B的接口&#xff0c;其中有些接口要上传Excel文件&#xff08;MultipartFile类型&#xff09;&#xff0c;另一些接口又要传递复杂的JSON对象&#xf…...

Java中如何实现Excel跨工作表数据复制

本文介绍了如何在Java程序中有效地复制Excel工作表中的数据。许多Java开发人员需要将数据从一个工作表复制到另一个工作表。本文提供了一个代码示例来帮助您解决这个问题。核心是如何在Java中有效地复制Excel工作表中特定区域的数据。下面的例子是使用Java库&#xff08;具体的…...