ffmpeg指令行功能強大,本文簡單介紹一下指令行解析過程。
指令行解析函數如下
我們以下面指令行為例
ffmpeg -y -ss 4 -i 1.ts -vframes 1 -f image2 -s 640x360 out.jpg
在ffmpeg_opt.c檔案中通過兩個函數split_commandline、parse_optgroup解析指令行參數并儲存在OptionParseContext結構體中。
typedef struct OptionParseContext {
OptionGroup global_opts;
OptionGroupList *groups;
int nb_groups;
/* parsing state */
OptionGroup cur_group;
} OptionParseContext;
OptionParseContext中global_opts儲存全局參數,比如-y參數。
nb_groups表示有多少個OptionGroupList選項組清單,資料儲存在groups數組中。
按輸入參數還是輸出參數進行分組,groups[0]儲存輸出參數選項組清單,
groups[1]儲存輸入參數選項組清單。
typedef struct OptionGroupList {
const OptionGroupDef *group_def;
OptionGroup *groups;
int nb_groups;
} OptionGroupList;
選項組清單儲存一系列選項組,選項組個數儲存在nb_groups字段,groups是選項組數組首位址。
從上面調試截圖可以看出針對舉例的指令行,輸出和輸入選項組都隻有一個。
typedef struct OptionGroup {
const OptionGroupDef *group_def;
const char *arg;
Option *opts;
int nb_opts;
AVDictionary *codec_opts;
AVDictionary *format_opts;
AVDictionary *sws_dict;
AVDictionary *swr_opts;
} OptionGroup;
選項組包含若幹選項,每一個選項都是儲存在Option結構體中。
輸出選項組包含3個選項,
從key/value值可以看出和我們在指令行裡設定的輸出參數一樣, -vframes 1 -f image2 -s 640x360。
輸出選項組包含1個選項,
從key/value值可以看出和我們在指令行裡設定的輸入參數一樣,-ss 4。
typedef struct Option {
const OptionDef *opt;
const char *key;
const char *val;
} Option;
typedef struct OptionDef {
const char *name;
int flags;
#define HAS_ARG 0x0001
#define OPT_BOOL 0x0002
#define OPT_EXPERT 0x0004
#define OPT_STRING 0x0008
#define OPT_VIDEO 0x0010
#define OPT_AUDIO 0x0020
#define OPT_INT 0x0080
#define OPT_FLOAT 0x0100
#define OPT_SUBTITLE 0x0200
#define OPT_INT64 0x0400
#define OPT_EXIT 0x0800
#define OPT_DATA 0x1000
#define OPT_PERFILE 0x2000 /* the option is per-file (currently ffmpeg-only).
implied by OPT_OFFSET or OPT_SPEC */
#define OPT_OFFSET 0x4000 /* option is specified as an offset in a passed optctx */
#define OPT_SPEC 0x8000 /* option is to be stored in an array of SpecifierOpt.
Implies OPT_OFFSET. Next element after the offset is
an int containing element count in the array. */
#define OPT_TIME 0x10000
#define OPT_DOUBLE 0x20000
#define OPT_INPUT 0x40000
#define OPT_OUTPUT 0x80000
union {
void *dst_ptr;
int (*func_arg)(void *, const char *, const char *);
size_t off;
} u;
const char *help;
const char *argname;
} OptionDef;
選項主要資料儲存在OptionDef結構體中。name是選項名,flags是選項标志(比如選項值類型、是否包含參數、輸入參數還是輸出參數、視訊參數還是音頻參數還是字幕參數等),聯合體u中可以包含三種資料之一,
dst_ptr | 全局控制變量的位址 |
func_arg | 選項對應的處理函數 |
off | OptionsContext結構體中對應的字段 |