ffmpeg parse_loglevel(argc, argv, options)函数解析

来源:互联网 发布:mac os 百度云盘 编辑:程序博客网 时间:2024/06/05 14:29

接下来看看 parse_loglevel(argc, argv, options)这个函数,从函数名字来看就是解析“loglevel”,看看注释是怎么样的

/**
 * Find the '-loglevel' option in the command line args and apply it.
 */

翻译过来就是发现 “-loglevel”参数在命令中,并且执行运用它。


这个函数在cmdutils.c也就是在公共命令集,不再任何包中,这个也就为了特定的运用环境而编写的。这样理解:cmdutils.c不是ffmpeg的API一部分,而是属于具体的项目。

看看函数的源码部分:


void parse_loglevel(int argc, char **argv, const OptionDef *options){    int idx = locate_option(argc, argv, options, "loglevel");(1) //定位 输入的命令参数项中有没有 “loglevel”    if (!idx)                                                    //如果没有则看看有没有“v”        idx = locate_option(argc, argv, options, "v");    if (idx && argv[idx + 1])        opt_loglevel("loglevel", argv[idx + 1]);(2)}
看看 locate_option函数,注释是这样的:Return index of option opt in argv or 0 if not found.

现在又有一个数据结构需要去探讨:OptionDef这个数据结构,其定义在cmdutils.h中,也就是说该数据结构为本项目而写,不是ffmpeg的数据结构

typedef struct OptionDef {

constchar*name; // option的名字

intflags; //option的标志

#defineHAS_ARG 0x0001 //命令含有参数选项的标致

#defineOPT_BOOL 0x0002 布尔型数据标志

#defineOPT_EXPERT 0x0004

#defineOPT_STRING 0x0008 字符串标志

#defineOPT_VIDEO 0x0010 视频标志

#defineOPT_AUDIO 0x0020 音频标志

#defineOPT_INT 0x0080 输入的标志

#defineOPT_FLOAT 0x0100 浮点点型标志

#defineOPT_SUBTITLE 0x0200 字幕的标志

#defineOPT_INT64 0x0400

#defineOPT_EXIT 0x0800 退出的标志

#defineOPT_DATA 0x1000 数据标志

#defineOPT_PERFILE 0x2000 /*the option is per-file (currently ffmpeg-only).

impliedby OPT_OFFSET or OPT_SPEC */

#defineOPT_OFFSET 0x4000 /*option is specified as an offset in a passed optctx*/

#defineOPT_SPEC 0x8000 /*option is to be stored in an array of SpecifierOpt.

ImpliesOPT_OFFSET. Next element after the offset is

anintcontaining element count in the array. */

#defineOPT_TIME 0x10000 时间标志

#defineOPT_DOUBLE 0x20000

#defineOPT_INPUT 0x40000 输出标志

#defineOPT_OUTPUT 0x80000 输入标志

union{

void*dst_ptr;

int(*func_arg)(void*, constchar*, constchar*);

size_toff;

}u;一个联合体

constchar*help;帮助

constchar*argname;参数名字

}OptionDef;

看的是不是有点崩溃?去掉无关的宏定义这个数据结构成这样了:

typedef struct OptionDef {   constchar*name;               // option的名字   intflags;                      //option的标志    union{       void*dst_ptr;       int(*func_arg)(void*, constchar*, constchar*);       size_toff;   }u;一个联合体   constchar*help;帮助   constchar*argname;参数名字}OptionDef;

这样就好看多了实际上就四个数据项

   现在回过头去看看parse_loglevel函数,最后如果有loglevel或者“v”那么就要执行 opt_loglevel函数了该函数的注释是这样的 :Set the libav* libraries log level.

看一看该函数的源码吧:

int opt_loglevel(const char *opt, const char *arg){    const struct { const char *name; int level; } log_levels[] = {        { "quiet"  , AV_LOG_QUIET   },        { "panic"  , AV_LOG_PANIC   },        { "fatal"  , AV_LOG_FATAL   },        { "error"  , AV_LOG_ERROR   },        { "warning", AV_LOG_WARNING },        { "info"   , AV_LOG_INFO    },        { "verbose", AV_LOG_VERBOSE },        { "debug"  , AV_LOG_DEBUG   },    };    char *tail;    int level;    int i;    for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {        if (!strcmp(log_levels[i].name, arg)) {            av_log_set_level(log_levels[i].level);(做了半天干这个事情)            return 0;        }    }    level = strtol(arg, &tail, 10);    if (*tail) { //“参数匹配不上的结果”对应像“ffmpeg -v ddddd”的命令        av_log(NULL, AV_LOG_FATAL, "Invalid loglevel \"%s\". "               "Possible levels are numbers or:\n", arg);        for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++)            av_log(NULL, AV_LOG_FATAL, "\"%s\"\n", log_levels[i].name);        exit_program(1);    }    av_log_set_level(level);(也就是干这个事情)    return 0;}

分析之后为了给一个直观的感受敲两行命令:

(1)ffmpeg -v info  执行的结果:

ffmpeg version 0.8.9-6:0.8.9-0ubuntu0.13.10.1, Copyright (c) 2000-2013 the Libav developers
  built on Nov  9 2013 19:09:46 with gcc 4.8.1
*** THIS PROGRAM IS DEPRECATED ***
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...


Use -h to get full help or, even better, run 'man ffmpeg'

(2)ffmpeg -v ddddd(也就是乱敲) 执行的结果:

Invalid loglevel "ddd". Possible levels are numbers or:
"quiet"
"panic"
"fatal"
"error"
"warning"
"info"
"verbose"
"debug"

好了,这一部分可以结束了!

补充:在linux终端输入 man ffmpeg 

然后找到 Generic options 


       These options are shared amongst the av* tools.
该解释为:这些命令选项在共享工具库中


下面就是。。。。

         -loglevel loglevel | -v loglevel
           Set the logging level used by the library.  loglevel is a number or
           a string containing one of the following values:


           quiet
           panic
           fatal
           error
           warning
           info
           verbose
           debug


           By default the program logs to stderr, if coloring is supported by
           the terminal, colors are used to mark errors and warnings. Log
           coloring can be disabled setting the environment variable
           AV_LOG_FORCE_NOCOLOR or NO_COLOR, or can be forced setting the
           environment variable AV_LOG_FORCE_COLOR.  The use of the
           environment variable NO_COLOR is deprecated and will be dropped in
           a following Libav version.


0 0