FFMpeg結(jié)構(gòu)體分析:AVFormatContext
typedef struct AVFormatContext :
分析常用的
/**
* 輸入文件的格式
* 在解復(fù)用是出現(xiàn),由avformat_open_input()設(shè)置.
*/
struct AVInputFormat *iformat;
/**
* 輸出文件的格式
*
* 復(fù)用的時候出現(xiàn), 必須在avformat_write_header()之前由用戶設(shè)置.
*/
struct AVOutputFormat *oformat;
/**
* AVFormatContext.streams的個數(shù).
*
* Set by avformat_new_stream(),不能被隨意修改.
*/
unsigned int nb_streams;
/**
* 輸入文件的所有碼流信息.新的碼流信息可由
* avformat_new_stream()生成。
*
* - demuxing: streams are created by libavformat in avformat_open_input().
* If AVFMTCTX_NOHEADER is set in ctx_flags, then new streams may also
* appear in av_read_frame().
* - muxing: streams are created by the user before avformat_write_header().
*
* 通過avformat_free_context()釋放內(nèi)存.
*/
AVStream **streams;
/**
* 輸入輸出文件名
*
* - demuxing: set by avformat_open_input()
* - muxing: may be set by the caller before avformat_write_header()
*/
char filename[1024];
/**
* 碼流的時長, 也是利用 AV_TIME_BASE fractional
* seconds表征. 一般情況下由
* AVStream values 推導(dǎo)得出.
*
* Demuxing only, set by libavformat.
*/
int64_t duration;
/**
* Forced video codec_id.
* Demuxing: Set by user.
*/
enum AVCodecID video_codec_id;
/**
* Forced audio codec_id.
* Demuxing: Set by user.
*/
enum AVCodecID audio_codec_id;
/**
* Forced subtitle(字幕) codec_id.
* Demuxing: Set by user.
*/
enum AVCodecID subtitle_codec_id;
/**
* Metadata that applies to the whole file.
*
* - demuxing: set by libavformat in avformat_open_input()
* - muxing: may be set by the caller before avformat_write_header()
*
* Freed by libavformat in avformat_free_context().
*/
AVDictionary *metadata;
/**
* 幀率 in
* avformat_find_stream_info().
* Demuxing only, set by the caller before avformat_find_stream_info().
*/
int fps_probe_size;
/**
* Forced video codec.
* This allows forcing a specific decoder, even when there are multiple with
* the same codec_id.
* Demuxing: Set by user via av_format_set_video_codec (NO direct access).
*/
AVCodec *video_codec;
/**
* Forced audio codec.
* This allows forcing a specific decoder, even when there are multiple with
* the same codec_id.
* Demuxing: Set by user via av_format_set_audio_codec (NO direct access).
*/
AVCodec *audio_codec;
/**
* Forced subtitle codec.
* This allows forcing a specific decoder, even when there are multiple with
* the same codec_id.
* Demuxing: Set by user via av_format_set_subtitle_codec (NO direct access).
*/
AVCodec *subtitle_codec;
/**
* Forced data codec.
* This allows forcing a specific decoder, even when there are multiple with
* the same codec_id.
* Demuxing: Set by user via av_format_set_data_codec (NO direct access).
*/
AVCodec *data_codec;
struct AVInputFormat *iformat:輸入數(shù)據(jù)的封裝格式
AVIOContext *pb:輸入數(shù)據(jù)的緩存
unsigned int nb_streams:音視頻流的個數(shù)
AVStream **streams:音視頻流
char filename[1024]:文件名
int64_t duration:時長(單位:微秒us,轉(zhuǎn)換為秒需要除以1000000)
int bit_rate:比特率(單位bps,轉(zhuǎn)換為kbps需要除以1000)
AVDictionary *metadata:元數(shù)據(jù)
視頻的時長可以轉(zhuǎn)換成HH:MM:SS的形式,示例代碼如下:
[cpp] view plain copy
AVFormatContext *pFormatCtx;
CString timelong;
...
//duration是以微秒為單位
//轉(zhuǎn)換成hh:mm:ss形式
int tns, thh, tmm, tss;
tns = (pFormatCtx->duration)/1000000;
thh = tns / 3600;
tmm = (tns % 3600) / 60;
tss = (tns % 60);
timelong.Format("%02d:%02d:%02d",thh,tmm,tss);
視頻的原數(shù)據(jù)(metadata)信息可以通過AVDictionary獲取。元數(shù)據(jù)存儲在AVDictionaryEntry結(jié)構(gòu)體中,如下所示
[cpp] view plain copy
typedef struct AVDictionaryEntry {
char *key;
char *value;
} AVDictionaryEntry;
每一條元數(shù)據(jù)分為key和value兩個屬性。
在ffmpeg中通過av_dict_get()函數(shù)獲得視頻的原數(shù)據(jù)。
下列代碼顯示了獲取元數(shù)據(jù)并存入meta字符串變量的過程,注意每一條key和value之間有一個"\t:",value之后有一個"\r\n"
[cpp] view plain copy
//MetaData------------------------------------------------------------
//從AVDictionary獲得
//需要用到AVDictionaryEntry對象
//CString author,copyright,description;
CString meta=NULL,key,value;
AVDictionaryEntry m = NULL;
//不用一個一個找出來
/ m=av_dict_get(pFormatCtx->metadata,"author",m,0);
author.Format("作者:%s",m->value);
m=av_dict_get(pFormatCtx->metadata,"copyright",m,0);
copyright.Format("版權(quán):%s",m->value);
m=av_dict_get(pFormatCtx->metadata,"description",m,0);
description.Format("描述:%s",m->value);
*/
//使用循環(huán)讀出
//(需要讀取的數(shù)據(jù),字段名稱,前一條字段(循環(huán)時使用),參數(shù))
while(m=av_dict_get(pFormatCtx->metadata,"",m,AV_DICT_IGNORE_SUFFIX)){
key.Format(m->key);
value.Format(m->value);
meta+=key+"\t:"+value+"\r\n" ;
}