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

#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");
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
FFmpeg是一套开源的跨平台音视频处理工具库,提供了音视频编解码、格式转换、过滤器等功能,广泛应用于多媒体开发领域。FFmpeg SDK则是基于FFmpeg的开发包,为开发者提供了更加友好的接口和封装,简化了音视频处理的开发流程。 使用FFmpeg SDK,开发者可以轻松地实现音视频文件的读取、写入、编解码等操作。它支持众多音视频格式,包括但不限于MP4、AVI、FLV、MP3、AAC等,可以进行格式之间的转换和编码参数的调整。此外,FFmpeg SDK还支持多种常用编解码库,如x264、x265、libvpx、libaom等,使得开发者能够根据需要选用合适的编码方式。 FFmpeg SDK提供了一套完善的API文档,方便开发者查找和应用各种功能。开发者可以根据文档中的示例代码,快速上手并进行开发。同时,FFmpeg SDK还支持多线程和硬件加速等特性,可以提高音视频处理的效率和性能。 除了基本的音视频处理功能,FFmpeg SDK还支持视频截图、音频剪切、视频拼接等操作,满足了更多实际需求。而且,FFmpeg SDK的开源特性使得开发者可以根据自己的需要进行二次开发和优化,使其更加适用于自己的项目。 总之,FFmpeg SDK是一款强大的音视频处理开发包,提供了丰富的功能和灵活的接口,方便开发者进行各种音视频处理任务。无论是开发媒体播放器、视频编辑器还是实时流媒体处理,使用FFmpeg SDK都能有效简化开发流程,并获得高效的处理能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

石小浪♪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值