FFmpeg常用API与示例(一)—— 工具库篇(av_log、AVDictionary、avio)

2024-05-11 09:36

本文主要是介绍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)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/979164

相关文章

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机

Python中Markdown库的使用示例详解

《Python中Markdown库的使用示例详解》Markdown库是一个用于处理Markdown文本的Python工具,这篇文章主要为大家详细介绍了Markdown库的具体使用,感兴趣的... 目录一、背景二、什么是 Markdown 库三、如何安装这个库四、库函数使用方法1. markdown.mark