qt/ffmpeg 调试方法

本文详细介绍Qt中利用宏进行调试的方法,以及FFmpeg中如何使用av_strerror()获取错误信息和通过不同颜色字体区分调试信息等级的技巧。适用于音视频处理、软件开发领域的专业人士。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) 

qt/ffmpeg 调试方法

1. qt打印文件名, 函数名, 行号

使用内置宏:

/**
 * __FILE__, 代表当前文件名;
 * __FUNCTION__, 代表当前函数名;
 * __LINE__, 代表的当前行号;
 */
#include <QDebug>

#define MYDEBUG  qDebug() <<"[FILE:" <<__FILE__ <<",FUNC:" <<__FUNCTION__ <<",LINE:" <<__LINE__ <<"] "

MYDEBUG <<"还可以在后面添加打印哦";

2. ffmpeg 获取调试信息

在ffmpeg的许多函数中, 都会提供返回值供我们确认函数是否成功调用. 当调用失败时将会返回错误值供我们查看出错信息. 如下面的函数声明:
/**
 * 由于篇幅关系, 我们只看其中的一行.
 * @return 0 on success, a negative AVERROR on failure.
 */

int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options);

   由上可以见, 当函数调用成功时, 返回0; 调用失败时, 返回一个负数的AVERROR.

这个负数的AVERROR怎么使用呢? ffmpeg给我们提供了一个方法: av_strerror(), 用法如下:

/**
 * Put a description of the AVERROR code errnum in errbuf.
 * In case of failure the global variable errno is set to indicate the
 * error. Even in case of failure av_strerror() will print a generic
 * error message indicating the errnum provided to errbuf.
 *
 * @param errnum      error code to describe
 * @param errbuf      buffer to which description is written
 * @param errbuf_size the size in bytes of errbuf
 * @return 0 on success, a negative value if a description for errnum
 * cannot be found
 */
int av_strerror(int errnum, char *errbuf, size_t errbuf_size);

//========================================================================

#define ERR_STR_LEN 1024
char err_buf[ERR_STR_LEN];

int ret = -1;

ret = avformat_open_input(&pFormatCtx, file_path, NULL, NULL);
if(ret< 0 && !av_strerror(ret, err_buf, ERR_STR_LEN)){
    MYDEBUG <<err_buf;
    return -1;
}

3. ffmpeg通过打印不同颜色的字体来表示调试信息等级的不同

/**
 * Print no output.
 */
#define AV_LOG_QUIET    -8

/**
 * Something went really wrong and we will crash now.
 */
#define AV_LOG_PANIC     0

/**
 * Something went wrong and recovery is not possible.
 * For example, no header was found for a format which depends
 * on headers or an illegal combination of parameters is used.
 */
#define AV_LOG_FATAL     8

/**
 * Something went wrong and cannot losslessly be recovered.
 * However, not all future data is affected.
 */
#define AV_LOG_ERROR    16

/**
 * Something somehow does not look correct. This may or may not
 * lead to problems. An example would be the use of '-vstrict -2'.
 */
#define AV_LOG_WARNING  24

/**
 * Standard information.
 */
#define AV_LOG_INFO     32

/**
 * Detailed information.
 */
#define AV_LOG_VERBOSE  40

/**
 * Stuff which is only useful for libav* developers.
 */
#define AV_LOG_DEBUG    48

/**
 * Extremely verbose debugging, useful for libav* development.
 */
#define AV_LOG_TRACE    56

/**
 * Set the log level
 *
 * @see lavu_log_constants
 *
 * @param level Logging level
 */
void av_log_set_level(int level);

该函数用于设置log的等级, 若低于该等级则不会被显示出来.( 数值越小, 等级越高 )
示例如下:

#include <stdio.h>
#include <libavutil/log.h>

int main(int argc, char *argv[])
{
    av_log_set_level(AV_LOG_VERBOSE);

	av_log(NULL, AV_LOG_INFO, "I am AV_LOG_INFO, %d\n", AV_LOG_INFO);
    av_log(NULL, AV_LOG_VERBOSE, "I am AV_LOG_VERBOSE, %d\n", AV_LOG_VERBOSE);
    av_log(NULL, AV_LOG_DEBUG, "I am AV_LOG_DEBUG, %d\n", AV_LOG_DEBUG);

    return 0;
}

结果如下:
在这里插入图片描述
如果你是Linux终端的话, 那么不同的log等级会显示不同的颜色, 因为我这里是QT所以没有显示.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

安河桥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值