FFMpeg結(jié)構(gòu)體分析:AVFormatContext

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本篇博客在雷神的結(jié)構(gòu)體介紹基礎(chǔ)上按自己的喜好整理的 后面根據(jù)自己工作中所需有所增改 AVStream 存儲每一個視...
    石丘閱讀 2,615評論 1 10
  • 現(xiàn)狀:現(xiàn)在視頻直播非常的火,所以在視頻直播開發(fā)中,使用的對視頻進行遍解碼的框架顯得尤為重要了,其實,這種框架蠻多的...
    ZHANG_GO閱讀 3,279評論 0 2
  • 教程一:視頻截圖(Tutorial 01: Making Screencaps) 首先我們需要了解視頻文件的一些基...
    90后的思維閱讀 4,988評論 0 3
  • 聽著樓下的喧鬧,門外的交談,感嘆想要守住一份寧靜的不易??傆X得,與別人相比,自己終究是“老了”!不知道從什么時候起...
    桃蛋兒桃美人兒閱讀 416評論 0 0
  • 以為自己聽的歌多,看了幾檔音樂節(jié)目,才知道其實有很多的歌都沒聽過;以為自己看的書多,加了簡書,才知道并非那么回事。...
    世涂花開閱讀 1,550評論 2 1

友情鏈接更多精彩內(nèi)容