三哥的技術專欄,轉載請註明出處!!!
分析了一段時間的ffmpeg源代碼,是時候寫個筆記做個總結了。所以決定寫一系列關於分析FFmpeg架構及API使用的筆記,現在計劃涵蓋的內容包括從打開一個媒體文件(access)到demux,然後到decode,最後render出來這整個過程,循序漸進,當然也包括轉碼過程, 其中每個過程我都會盡量仔細分析並對源代碼註釋,並給出相應的實例代碼。希望能夠讓剛接觸ffmpeg的朋友獲得一些幫助,因爲ffmpeg方面的資料確實很少,自己學習的時候也走了不少彎路。
FFmpeg版本2.1, 我的操作環境是Ubuntu12.04 X64, GCC4.6.1.
ffmpeg是一套功能非常不可思議的開源的多媒體框架,而且開發速度很快,如果感興趣可以加入到ffmpeg開發的郵件列表,甚至爲ffmpeg做出自己的貢獻。
首先可以去ffmpeg的官網下載最新版的ffmpeg,我這裏的版本是2.1,因爲ffmpeg是開源的,依賴於gcc,所以最好在linux下編譯和開發,如果實在想用windows,那麼也可以在windows上安裝cygwin(一個仿linux的環境),不過筆者還是建議在linux下編譯和開發。編譯和安裝都很簡單,這裏還是廢話一下,執行.configure來產生Makefile,然後執行make;makeinstall安裝ffmpeg庫到系統,之後就可以進行ffmpeg的開發了,當然知識想調用ffmpeg的SDK的話可以不用下載源碼,僅僅下載SDK(包括ffmpeg的幾個庫和相應的include文件).
下面一段代碼非常簡單,僅僅幾行就可以讀出媒體文件的信息,這段程序主要是用了ffmpeg中的libavformat庫下面先貼出程序的執行效果
點擊(此處)摺疊或打開
-
#include <libavformat/avformat.h>
-
//#include <libswscale/swscale.h>
-
-
-
static ShowUseage(const char* p)
-
{
-
printf("you must specify an input file as: %s youfile.avi!!!\n", p);
-
}
-
-
int main(int argc, char* argv[])
-
{
-
AVFormatContext *pFormatContext = NULL;
-
if(argc<2)
-
{
-
ShowUseage(argv[0]);
-
return -1;
-
}
-
char *filepath = argv[1];
-
av_register_all();
-
//pFormatContext = avformat_alloc_context();
-
if(avformat_open_input(&pFormatContext,filepath,NULL,NULL)!=0) //Open the media file and read the header
-
{
-
printf("Can not open the media file you specified!\n");
-
return -1;
-
}
-
printf("****************file information*****************\n");
-
av_dump_format(pFormatContext,0,filepath,0); //dump input information to the stdio
-
printf("*************************************************\n");
-
avformat_close_input(&pFormatContext);
-
return 0;
- }