FFmpeg【SDK01】日志和字典的使用

简介: FFmpeg中日志功能的使用方法,包括日志级别的设置和AVDictionary的基本操作,同时展示了字符串解析函数如av_parse_video_size、av_parse_video_rate和av_parse_time的应用。
#include <iostream>
#define __STDC_CONSTANT_MACROS

extern "C"
{
    #include <libavutil\log.h>
    #include <libavformat\avformat.h>
    #include <libavutil\parseutils.h>
}
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avformat.lib")

// No1. ffmpeg日志使用
void testLog()
{
    // av_register_all();    FFmpeg4.0 以前需要注册所有的输入输出设备
    AVFormatContext *pAVFmtCtx = NULL;
    pAVFmtCtx = avformat_alloc_context();        

    printf("====================================\n");
    /*
        1. av_log 第一个参数avcl, 在解码器中调用可以指向AVCodecContext结构体指针
                                 在格式化上下文中调用可以指向AVFormatContext结构体指针
                                 在FFmpeg内部调用可以设置为NULL
        2. 日志等级默认 >= AV_LOG_INFO
    */

    int avLogLevel = av_log_get_level();    // 获取日志等级
    av_log(pAVFmtCtx, AV_LOG_WARNING, "log default level: %d\n", avLogLevel);

    av_log_set_level(AV_LOG_DEBUG);    // 设置日志等级为Debug

    av_log(pAVFmtCtx, AV_LOG_PANIC, "灾难性的: Something went really wrong and we will crash now.\n");
    av_log(pAVFmtCtx, AV_LOG_FATAL, "失败: Something went wrong and recovery is not possible.\n");
    av_log(pAVFmtCtx, AV_LOG_ERROR, "错误: Something went wrong and cannot losslessly be recovered.\n");
    av_log(pAVFmtCtx, AV_LOG_WARNING, "警告: This may or may not lead to problems.\n");
    av_log(pAVFmtCtx, AV_LOG_INFO, "信息: Standard information.\n");
    av_log(pAVFmtCtx, AV_LOG_VERBOSE, "细节描述: Detailed information.\n");
    av_log(pAVFmtCtx, AV_LOG_DEBUG, "调试: Stuff which is only useful for libav* developers.\n");
    printf("====================================\n");

    avformat_free_context(pAVFmtCtx);
}

// No2. ffmpeg字典基本使用
void testDictionary()
{
    AVDictionary *dict = NULL;                        // 字典
    AVDictionaryEntry *dictEntry = NULL;            // 字典条目

    av_dict_set(&dict, "name", "zhangsan", 0);        // 第四个参数位置(0默认在头部插入)
    av_dict_set(&dict, "age", "22", 0);
    av_dict_set(&dict, "gender", "man", 0);
    av_dict_set(&dict, "email", "www@www.com", 0);

    // 第二种添加方式 av_strdup()
    char *k = av_strdup("location");    // duplication: 复制
    char *v = av_strdup("Beijing-China");
    av_dict_set(&dict, k, v, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
    // AV_DICT_DONT_STRDUP_KEY 有重复的key不复制(但是在av_dict_free(&d)之前不能修改或释放key,可能会出现未定义)

    printf("====================================\n");
    int dict_cnt = av_dict_count(dict);            // 获取字典个数
    printf("dict_count:%d\n", dict_cnt);

    // 遍历dict            AV_DICT_IGNORE_SUFFIX(忽略后缀)
    printf("dict_element:\n");
    while (dictEntry = av_dict_get(dict, "", dictEntry, AV_DICT_IGNORE_SUFFIX)) {
        printf("key:%10s  |  value:%s\n", dictEntry->key, dictEntry->value);
    }

    // 根据key获取value
    dictEntry = av_dict_get(dict, "email", NULL, AV_DICT_IGNORE_SUFFIX);
    printf("email is %s\n", dictEntry->value);
    printf("====================================\n");
    av_dict_free(&dict);        // 传入二级指针释放内存
}

// No3.ffmpeg字符串解析基本使用
void testParseUtil()
{
    char input_str[100] = { 0 };
    printf("========= Parse Video Size =========\n");        // 根据字符串解析出 Video Size
    int output_w = 0;
    int output_h = 0;
    strcpy(input_str, "1920x1080");                            // 16 : 9
    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 definition    高清
    strcpy(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");        // 根据分数字符串解析出 num/den 的结构
    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/1
    av_parse_video_rate(&output_rational, input_str);
    printf("framerate:%d/%d\n", output_rational.num, output_rational.den);    


    printf("=========== Parse Time =============\n");        // 根据时间字符串解析出 us 值
    int64_t output_timeval;                //单位:us, 1s=1000MilliSeconds, 1ms=1000MacroSeconds
    strcpy(input_str, "00:01:01");
    av_parse_time(&output_timeval, input_str, 1);
    printf("microseconds:%lld\n", output_timeval);
    printf("====================================\n");
}
相关文章
|
28天前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
4天前
|
人工智能 Rust Java
10月更文挑战赛火热启动,坚持热爱坚持创作!
开发者社区10月更文挑战,寻找热爱技术内容创作的你,欢迎来创作!
442 17
|
7天前
|
JSON 自然语言处理 数据管理
阿里云百炼产品月刊【2024年9月】
阿里云百炼产品月刊【2024年9月】,涵盖本月产品和功能发布、活动,应用实践等内容,帮助您快速了解阿里云百炼产品的最新动态。
阿里云百炼产品月刊【2024年9月】
|
20天前
|
存储 关系型数据库 分布式数据库
GraphRAG:基于PolarDB+通义千问+LangChain的知识图谱+大模型最佳实践
本文介绍了如何使用PolarDB、通义千问和LangChain搭建GraphRAG系统,结合知识图谱和向量检索提升问答质量。通过实例展示了单独使用向量检索和图检索的局限性,并通过图+向量联合搜索增强了问答准确性。PolarDB支持AGE图引擎和pgvector插件,实现图数据和向量数据的统一存储与检索,提升了RAG系统的性能和效果。
|
7天前
|
Linux 虚拟化 开发者
一键将CentOs的yum源更换为国内阿里yum源
一键将CentOs的yum源更换为国内阿里yum源
381 2
|
22天前
|
人工智能 IDE 程序员
期盼已久!通义灵码 AI 程序员开启邀测,全流程开发仅用几分钟
在云栖大会上,阿里云云原生应用平台负责人丁宇宣布,「通义灵码」完成全面升级,并正式发布 AI 程序员。
|
24天前
|
机器学习/深度学习 算法 大数据
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
2024“华为杯”数学建模竞赛,对ABCDEF每个题进行详细的分析,涵盖风电场功率优化、WLAN网络吞吐量、磁性元件损耗建模、地理环境问题、高速公路应急车道启用和X射线脉冲星建模等多领域问题,解析了问题类型、专业和技能的需要。
2600 22
【BetterBench博士】2024 “华为杯”第二十一届中国研究生数学建模竞赛 选题分析
|
6天前
|
存储 人工智能 搜索推荐
数据治理,是时候打破刻板印象了
瓴羊智能数据建设与治理产品Datapin全面升级,可演进扩展的数据架构体系为企业数据治理预留发展空间,推出敏捷版用以解决企业数据量不大但需构建数据的场景问题,基于大模型打造的DataAgent更是为企业用好数据资产提供了便利。
289 2
|
4天前
|
编译器 C#
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
106 65
|
24天前
|
机器学习/深度学习 算法 数据可视化
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码
2024年中国研究生数学建模竞赛C题聚焦磁性元件磁芯损耗建模。题目背景介绍了电能变换技术的发展与应用,强调磁性元件在功率变换器中的重要性。磁芯损耗受多种因素影响,现有模型难以精确预测。题目要求通过数据分析建立高精度磁芯损耗模型。具体任务包括励磁波形分类、修正斯坦麦茨方程、分析影响因素、构建预测模型及优化设计条件。涉及数据预处理、特征提取、机器学习及优化算法等技术。适合电气、材料、计算机等多个专业学生参与。
1582 17
【BetterBench博士】2024年中国研究生数学建模竞赛 C题:数据驱动下磁性元件的磁芯损耗建模 问题分析、数学模型、python 代码