FFmpeg常用API与示例(一)—— 工具库篇(av_log、AVDictionary、avio)
工具层
1.av_log
可以设置日志的级别,这个看看名字就明白了,也不用过多的解释。
- AV_LOG_PANIC
- AV_LOG_FATAL
- AV_LOG_ERROR
- AV_LOG_WARNING
- AV_LOG_INFO
- AV_LOG_VERBOSE
- AV_LOG_DEBUG
void test_log()
{/ av_register_all();AVFormatContext *pAVFmtCtx = NULL;pAVFmtCtx = avformat_alloc_context();printf("====================================\n");av_log(pAVFmtCtx, AV_LOG_PANIC, "Panic: Something went really wrong and we will crash now.\n");av_log(pAVFmtCtx, AV_LOG_FATAL, "Fatal: Something went wrong and recovery is not possible.\n");av_log(pAVFmtCtx, AV_LOG_ERROR, "Error: Something went wrong and cannot losslessly be recovered.\n");av_log(pAVFmtCtx, AV_LOG_WARNING, "Warning: This may or may not lead to problems.\n");av_log(pAVFmtCtx, AV_LOG_INFO, "Info: Standard information.\n");av_log(pAVFmtCtx, AV_LOG_VERBOSE, "Verbose: Detailed information.\n");av_log(pAVFmtCtx, AV_LOG_DEBUG, "Debug: Stuff which is only useful for libav* developers.\n");printf("====================================\n");avformat_free_context(pAVFmtCtx);
}
2.AVDictionary
- AVDictionary
- AVDictionaryEntry
- av_dict_set
- av_dict_count
- av_dict_get
- av_dict_free
void test_avdictionary()
{AVDictionary *d = NULL;AVDictionaryEntry *t = NULL;av_dict_set(&d, "name", "zhangsan", 0);av_dict_set(&d, "age", "22", 0);av_dict_set(&d, "gender", "man", 0);av_dict_set(&d, "email", "www@www.com", 0);// av_strdup()char *k = av_strdup("location");char *v = av_strdup("Beijing-China");av_dict_set(&d, k, v, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);printf("====================================\n");int dict_cnt = av_dict_count(d);printf("dict_count:%d\n", dict_cnt);printf("dict_element:\n");while (t = av_dict_get(d, "", t, AV_DICT_IGNORE_SUFFIX)){printf("key:%10s | value:%s\n", t->key, t->value);}t = av_dict_get(d, "email", t, AV_DICT_IGNORE_SUFFIX);printf("email is %s\n", t->value);printf("====================================\n");av_dict_free(&d);
}
3.AVParseUtil
- av_parse_video_size
- av_parse_video_rate
- av_parse_time
- av_parse_color
- av_parse_ratio
void test_parseutil()
{char input_str[100] = {0};printf("========= Parse Video Size =========\n");int output_w = 0;int output_h = 0;strcpy(input_str, "1920x1080");av_parse_video_size(&output_w, &output_h, input_str);printf("w:%4d | h:%4d\n", output_w, output_h);// strcpy(input_str,"vga");//640x480(4:3)// strcpy(input_str,"hd1080");//high definitionstrcpy(input_str, "pal"); // ntsc(N制720x480), pal(啪制720x576)av_parse_video_size(&output_w, &output_h, input_str);printf("w:%4d | h:%4d\n", output_w, output_h);printf("========= Parse Frame Rate =========\n");AVRational output_rational = {0, 0};strcpy(input_str, "15/1");av_parse_video_rate(&output_rational, input_str);printf("framerate:%d/%d\n", output_rational.num, output_rational.den);strcpy(input_str, "pal"); // fps:25/1av_parse_video_rate(&output_rational, input_str);printf("framerate:%d/%d\n", output_rational.num, output_rational.den);printf("=========== Parse Time =============\n");int64_t output_timeval; // 单位:微妙, 1S=1000MilliSeconds, 1MilliS=1000MacroSecondsstrcpy(input_str, "00:01:01");av_parse_time(&output_timeval, input_str, 1);printf("microseconds:%lld\n", output_timeval);printf("====================================\n");
}
协议层
协议操作:三大数据结构
- AVIOContext
- **URLContext **
- URLProtocol
协议(文件)操作的顶层结构是 AVIOContext,这个对象实现了带缓冲的读写操作;FFMPEG的输入对象 AVFormat 的 pb 字段指向一个 AVIOContext。
AVIOContext 的 opaque 实际指向一个 URLContext 对象,这个对象封装了协议对象及协议操作对象,其中 prot 指向具体的协议操作对象,priv_data 指向具体的协议对象。
URLProtocol 为协议操作对象,针对每种协议,会有一个这样的对象,每个协议操作对象和一个协议对象关联,比如,文件操作对象为 ff_file_protocol,它关联的结构体是FileContext。
1.avio 实战:打开本地文件或网络直播流
- avformat_network_init
- avformat_alloc_context
- avformat_open_input
- avformat_find_stream_info
int main_222929s(int argc, char *argv[]){/av_register_all();avformat_network_init();printf("hello,ffmpeg\n");AVFormatContext* pFormatCtx = NULL;AVInputFormat *piFmt = NULL;printf("hello,avformat_alloc_context\n");// Allocate the AVFormatContext:pFormatCtx = avformat_alloc_context();printf("hello,avformat_open_input\n");//打开本地文件或网络直播流//rtsp://127.0.0.1:8554/rtsp1//ande_10s.mp4if (avformat_open_input(&pFormatCtx, "rtsp://127.0.0.1:8554/rtsp1", piFmt, NULL) < 0) {printf("avformat open failed.\n");goto quit;}else {printf("open stream success!\n");}if (avformat_find_stream_info(pFormatCtx, NULL)<0){printf("av_find_stream_info error \n");goto quit;}else {printf("av_find_stream_info success \n");printf("******nb_streams=%d\n",pFormatCtx->nb_streams);}quit:avformat_free_context(pFormatCtx);avformat_close_input(&pFormatCtx);avformat_network_deinit();return 0;
}
2.avio 实战:自定义 AVIO
int read_func(void* ptr, uint8_t* buf, int buf_size)
{FILE* fp = (FILE*)ptr;size_t size = fread(buf, 1, buf_size, fp);int ret = size;printf("Read Bytes:%d\n", size);return ret;}int64_t seek_func(void *opaque, int64_t offset, int whence)
{int64_t ret;FILE *fp = (FILE*)opaque;if (whence == AVSEEK_SIZE) {return -1;}fseek(fp, offset, whence);return ftell(fp);}int main(int argc, char *argv[]){///av_register_all();printf("hello,ffmpeg\n");int ret = 0;FILE* fp = fopen("ande_10s.flv", "rb");int nBufferSize = 1024;unsigned char* pBuffer = (unsigned char*)malloc(nBufferSize);AVFormatContext* pFormatCtx = NULL;AVInputFormat *piFmt = NULL;printf("hello,avio_alloc_context\n");// Allocate the AVIOContext://请同学们自己揣摩AVIOContext* pIOCtx = avio_alloc_context(pBuffer, nBufferSize,0,fp,read_func,0,seek_func);printf("hello,avformat_alloc_context\n");// Allocate the AVFormatContext:pFormatCtx = avformat_alloc_context();// Set the IOContext:pFormatCtx->pb = pIOCtx;//关联,绑定pFormatCtx->flags = AVFMT_FLAG_CUSTOM_IO;printf("hello,avformat_open_input\n");//打开流if (avformat_open_input(&pFormatCtx, "", piFmt, NULL) < 0) {printf("avformat open failed.\n");goto quit;}else {printf("open stream success!\n");}if (avformat_find_stream_info(pFormatCtx, NULL)<0){printf("av_find_stream_info error \n");goto quit;}else {printf("av_find_stream_info success \n");printf("******nb_streams=%d\n",pFormatCtx->nb_streams);}quit:avformat_free_context(pFormatCtx);avformat_close_input(&pFormatCtx);free(pBuffer);return 0;
}
3.avio 实战:自定义数据来源
- av_file_map
- avformat_alloc_context
- av_malloc
- avio_alloc_context
- avformat_open_input
- avformat_find_stream_info
//自定义缓冲区
struct buffer_data {uint8_t *ptr;size_t size; ///< size left in the buffer
};//读取数据(回调函数)
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
{struct buffer_data *bd = (struct buffer_data *)opaque;buf_size = FFMIN(buf_size, bd->size);if (!buf_size)return AVERROR_EOF;printf("ptr:%p size:%d\n", bd->ptr, bd->size);/* copy internal buffer data to buf *//// 灵活应用[内存buf]:读取的是内存,比如:加密播放器,这里解密memcpy(buf, bd->ptr, buf_size);bd->ptr += buf_size;bd->size -= buf_size;return buf_size;
}int main(int argc, char *argv[])
{AVFormatContext *fmt_ctx = NULL;AVIOContext *avio_ctx = NULL;uint8_t *buffer = NULL, *avio_ctx_buffer = NULL;size_t buffer_size, avio_ctx_buffer_size = 4096;char *input_filename = NULL;int ret = 0;struct buffer_data bd = { 0 };printf("Hello,ffmpeg\n");if (argc != 2) {fprintf(stderr, "usage: %s input_file\n""API example program to show how to read from a custom buffer ""accessed through AVIOContext.\n", argv[0]);return 1;}input_filename = argv[1];/* slurp file content into buffer *////内存映射文件ret = av_file_map(input_filename, &buffer, &buffer_size, 0, NULL);if (ret < 0)goto end;printf("av_file_map,ok\n");/* fill opaque structure used by the AVIOContext read callback */bd.ptr = buffer;bd.size = buffer_size;/// 创建对象:AVFormatContextif (!(fmt_ctx = avformat_alloc_context())) {ret = AVERROR(ENOMEM);goto end;}printf("avformat_alloc_context,ok\n");/// 分配内存avio_ctx_buffer = av_malloc(avio_ctx_buffer_size);if (!avio_ctx_buffer) {ret = AVERROR(ENOMEM);goto end;}/// 创建对象:AVIOContext,注意参数avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size,0,&bd,&read_packet,NULL,NULL);if (!avio_ctx) {ret = AVERROR(ENOMEM);goto end;}fmt_ctx->pb = avio_ctx;printf("avio_alloc_context,ok\n");/// 打开输入流ret = avformat_open_input(&fmt_ctx, NULL, NULL, NULL);if (ret < 0) {fprintf(stderr, "Could not open input\n");goto end;}printf("avformat_open_input,ok\n");/// 查找流信息ret = avformat_find_stream_info(fmt_ctx, NULL);if (ret < 0) {fprintf(stderr, "Could not find stream information\n");goto end;}printf("avformat_find_stream_info,ok\n");printf("******nb_streams=%d\n",fmt_ctx->nb_streams);av_dump_format(fmt_ctx, 0, input_filename, 0);end:avformat_close_input(&fmt_ctx);/* note: the internal buffer could have changed, and be != avio_ctx_buffer */if (avio_ctx)av_freep(&avio_ctx->buffer);avio_context_free(&avio_ctx);/// 内存映射文件:解绑定av_file_unmap(buffer, buffer_size);if (ret < 0) {fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));return 1;}return 0;
}
相关文章:
FFmpeg常用API与示例(一)—— 工具库篇(av_log、AVDictionary、avio)
工具层 1.av_log 可以设置日志的级别,这个看看名字就明白了,也不用过多的解释。 AV_LOG_PANICAV_LOG_FATALAV_LOG_ERRORAV_LOG_WARNINGAV_LOG_INFOAV_LOG_VERBOSEAV_LOG_DEBUG void test_log() {/ av_register_all();AVFormatContext *pAVFmtCtx NU…...

日志的基本用法
目标 1. 掌握如何设置日志级别 2. 掌握如何设置日志格式 3. 掌握如何将日志信息输出到文件中 1. logging模块 Python中有一个标准库模块logging可以直接记录日志 1.1 基本用法 import logging logging.debug("这是一条调试信息") logging.info("这是一条…...

什么是页分裂、页合并?
数据组织方式 在InnoDB存储引擎中,表数据都是根据主键顺序组织存放的,这种存储方式的表称为索引组织表(index organized table IOT)。 行数据,都是存储在聚集索引的叶子节点上的。而我们之前也讲解过InnoDB的逻辑结构图: 在I…...

软件2班20240513
第三次作业 package com.yanyu;import java.sql.*; import java.util.ResourceBundle;public class JDBCTest01 {public static void main(String[] args) {ResourceBundle bundle ResourceBundle.getBundle("com/resources/db");// ctrl alt vString driver …...

嵌入式学习-时钟树
时钟树 时钟分类 时钟树框图 LSI与LSE HSI、HSE与PLL 系统时钟的产生 AHB、APBx的时钟配置 相关寄存器 寄存器部分的细节内容请参考手册。 相关库函数...

对博客系统基本功能进行自动化测试(Junit + Selenium)
环境搭建: 浏览器: 本次测试使用Chrome浏览器在jdk的bin目录下安装对应浏览器驱动(尽量选择与浏览器版本相近的驱动)chromedriver.storage.googleapis.com/index.htmlJunit依赖: <!-- https://mvnreposit…...

《换你来当爹》:AI驱动的养成游戏,探索虚拟亲子关系的新模式
AI技术如何重塑我们对游戏互动的认知 在人工智能技术的浪潮下,一款名为《换你来当爹》的AI养成游戏,以其创新的互动模式和个性化体验,吸引了游戏爱好者的目光。这款游戏利用了先进的LLM技术,通过AI实时生成剧情和图片,…...

在idea中使用vue
一、安装node.js 1、在node.js官网(下载 | Node.js 中文网)上下载适合自己电脑版本的node.js压缩包 2、下载完成后进行解压并安装,一定要记住自己的安装路径 一直点击next即可,这部选第一个 3、安装成功后,按住winR输入…...

Linux系统编程:进程控制
1.进程创建 1.1 fork函数 fork()通过复制调用进程来创建一个新进程。新进程称为子进程,是调用进程的精确副本 进程,但以下几点除外: 子进程有自己的PID,此PID与任何现有进程组的ID不匹配子进程的父进程ID…...

Android 异常开机半屏重启代码分析
Android 的稳定性是 Android 性能的一个重要指标,它也是 App 质量构建体系中最基本和最关键的一环;如果应用经常崩溃,或者关键功能不可用,那显然会对我们的留存产生重大影响所以为了保障应用的稳定性,我们首先应该树立…...

Kafka从0到消费者开发
安装ZK Index of /zookeeper/zookeeper-3.9.2 下载安装包 一定要下载-bin的,不带bin的是源码,没有编译的,无法执行。-bin的才可以执行。 解压 tar -zxvf apache-zookeeper-3.9.2-bin.tar.gz 备份配置 cp zoo_sample.cfg zoo_sample.cfg-b…...

01-项目功能,架构设计介绍
稻草快速开发平台 开发背景就是通过此项目介绍使用SpringBoot Vue3两大技术栈开发一个拥有动态权限、路由的前后端分离项目,此项目可以继续完善,成为一个模板为将来快速开发做铺垫。 实现功能 开发流程 通过命令构建前端项目在VSCode中开发ÿ…...

bvh 好用强大的播放器源码
目录 效果图: 显示旋转角度: 显示骨骼名称 下载链接: 可以显示骨骼名称,旋转角度,自适应大小,支持3维npz数据可视化 python实现,提供源代码,修改和完善很方便。 根据3维npz生成…...
安阳在线知识付费系统,培训机构如何进行课程体系的设置?
校外培训不管是从招生还是课程体系都是截然不同的,在课程体系设置上,不同的层次设计也就不同。课程体系设计在功能诉求上可以分为入门课、核心课、高利润课、种子课四个类别。下面为大家介绍一下。 1、入门课 “入门课”就是最易、最省、最少障碍的满足家…...

网络编程:服务器模型-并发服务器-多进程
并发服务器概念: 并发服务器同一时刻可以处理多个客户机的请求 设计思路: 并发服务器是在循环服务器基础上优化过来的 (1)每连接一个客户机,服务器立马创建子进程或者子线程来跟新的客户机通信 (accept之后…...
React 基础案例
React的特点: 1、声明式编程 2、组件化开发 3、多平台适配yuan 原生实现: <h2 class"title"></h2><button class"btn">改变文本</button><script>let msg "Hello World";const titleEl d…...

【Python探索之旅】选择结构(条件语句)
文章目录 条件结构: 1.1 if单分支结构 1.2 if-else 多分支结构 1.3 if-elif 多重结构: 完结撒花 前言 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块。 Python提供了顺序、选择、循环三…...

Recommender ~ Collaborative filtering
Using per-item features User j 预测 movie i: Cost Function: 仅求和用户投票过的电影。 常规规范化(usual normalization):1/2m 正则化项:阻止过拟合 在知晓X的前提下,如何学习w,b参数…...

我觉得POC应该贴近实际
今天我看到一位老师给我一份测试数据。 这是三个国产数据库。算是分布式的。其中有两个和我比较熟悉,但是这个数据看上去并不好。看上去第一个黄色的数据库数据是这里最好的了。但是即使如此,我相信大部分做数据库的人都知道。MySQL和PostgreSQL平时拿出…...

AI 情感聊天机器人工作之旅 —— 与复读机问题的相遇与别离
前言:先前在杭州的一家大模型公司从事海外闲聊机器人产品,目前已经离职,文章主要讨论在闲聊场景下遇到的“复读机”问题以及一些我个人的思考和解决方案。文章内部已经对相关公司和人员信息做了去敏,如仍涉及到机密等情况…...

wordpress后台更新后 前端没变化的解决方法
使用siteground主机的wordpress网站,会出现更新了网站内容和修改了php模板文件、js文件、css文件、图片文件后,网站没有变化的情况。 不熟悉siteground主机的新手,遇到这个问题,就很抓狂,明明是哪都没操作错误&#x…...
AtCoder 第409场初级竞赛 A~E题解
A Conflict 【题目链接】 原题链接:A - Conflict 【考点】 枚举 【题目大意】 找到是否有两人都想要的物品。 【解析】 遍历两端字符串,只有在同时为 o 时输出 Yes 并结束程序,否则输出 No。 【难度】 GESP三级 【代码参考】 #i…...
C++ 基础特性深度解析
目录 引言 一、命名空间(namespace) C 中的命名空间 与 C 语言的对比 二、缺省参数 C 中的缺省参数 与 C 语言的对比 三、引用(reference) C 中的引用 与 C 语言的对比 四、inline(内联函数…...

Rust 开发环境搭建
环境搭建 1、开发工具RustRover 或者vs code 2、Cygwin64 安装 https://cygwin.com/install.html 在工具终端执行: rustup toolchain install stable-x86_64-pc-windows-gnu rustup default stable-x86_64-pc-windows-gnu 2、Hello World fn main() { println…...
LangFlow技术架构分析
🔧 LangFlow 的可视化技术栈 前端节点编辑器 底层框架:基于 (一个现代化的 React 节点绘图库) 功能: 拖拽式构建 LangGraph 状态机 实时连线定义节点依赖关系 可视化调试循环和分支逻辑 与 LangGraph 的深…...

MySQL的pymysql操作
本章是MySQL的最后一章,MySQL到此完结,下一站Hadoop!!! 这章很简单,完整代码在最后,详细讲解之前python课程里面也有,感兴趣的可以往前找一下 一、查询操作 我们需要打开pycharm …...
高防服务器价格高原因分析
高防服务器的价格较高,主要是由于其特殊的防御机制、硬件配置、运营维护等多方面的综合成本。以下从技术、资源和服务三个维度详细解析高防服务器昂贵的原因: 一、硬件与技术投入 大带宽需求 DDoS攻击通过占用大量带宽资源瘫痪目标服务器,因此…...

【51单片机】4. 模块化编程与LCD1602Debug
1. 什么是模块化编程 传统编程会将所有函数放在main.c中,如果使用的模块多,一个文件内会有很多代码,不利于组织和管理 模块化编程则是将各个模块的代码放在不同的.c文件里,在.h文件里提供外部可调用函数声明,其他.c文…...

医疗AI模型可解释性编程研究:基于SHAP、LIME与Anchor
1 医疗树模型与可解释人工智能基础 医疗领域的人工智能应用正迅速从理论研究转向临床实践,在这一过程中,模型可解释性已成为确保AI系统被医疗专业人员接受和信任的关键因素。基于树模型的集成算法(如RandomForest、XGBoost、LightGBM)因其卓越的预测性能和相对良好的解释性…...
python打卡第47天
昨天代码中注意力热图的部分顺移至今天 知识点回顾: 热力图 作业:对比不同卷积层热图可视化的结果 def visualize_attention_map(model, test_loader, device, class_names, num_samples3):"""可视化模型的注意力热力图,展示模…...