FFmpeg中实现对多媒体信息的获取与打印av_dump_format
生活随笔
收集整理的這篇文章主要介紹了
FFmpeg中实现对多媒体信息的获取与打印av_dump_format
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
獲取mediainfo
首先調用av_register_all將所有的編碼器和解碼器注冊好
來看下具體的注冊實現(xiàn)
void av_register_all(void) {ff_thread_once(&av_format_next_init, av_format_init_next); } // 看下av_format_init_next的實現(xiàn) // 可以看到函數(shù)內部是實現(xiàn)了將編碼器和解碼器使用指針進行串聯(lián)的操作 // 如果有輸出或者輸入列表,將輸出或輸入列表也進行串聯(lián) static void av_format_init_next(void) {AVOutputFormat *prevout = NULL, *out;AVInputFormat *previn = NULL, *in;ff_mutex_lock(&avpriv_register_devices_mutex);// 編碼器串聯(lián)for (int i = 0; (out = (AVOutputFormat*)muxer_list[i]); i++) {if (prevout)prevout->next = out;prevout = out;}//實現(xiàn)輸出列表的串聯(lián)if (outdev_list) {for (int i = 0; (out = (AVOutputFormat*)outdev_list[i]); i++) {if (prevout)prevout->next = out;prevout = out;}}//實現(xiàn)解碼器的串聯(lián)for (int i = 0; (in = (AVInputFormat*)demuxer_list[i]); i++) {if (previn)previn->next = in;previn = in;}//實現(xiàn)輸入列表的串聯(lián)if (indev_list) {for (int i = 0; (in = (AVInputFormat*)indev_list[i]); i++) {if (previn)previn->next = in;previn = in;}}ff_mutex_unlock(&avpriv_register_devices_mutex); }調用avformat_open_input
調用 avformat_open_input根據(jù)輸入的音視頻內容構造 AVFormatContext結構體,函數(shù)功能如下:
/**打開一個流,并讀取頭部信息* Open an input stream and read the header. The codecs are not opened.* The stream must be closed with avformat_close_input().* ps可以用戶提供,如果為NULL該函數(shù)會進行內存申請* @param ps Pointer to user-supplied AVFormatContext (allocated by avformat_alloc_context).* May be a pointer to NULL, in which case an AVFormatContext is allocated by this* function and written into ps.* Note that a user-supplied AVFormatContext will be freed on failure.指向要打開的多媒體文件* @param url URL of the stream to open.不指定,使用自動偵測* @param fmt If non-NULL, this parameter forces a specific input format.* Otherwise the format is autodetected.* @param options A dictionary filled with AVFormatContext and demuxer-private options.* On return this parameter will be destroyed and replaced with a dict containing* options that were not found. May be NULL.** @return 0 on success, a negative AVERROR on failure.** @note If you want to use custom IO, preallocate the format context and set its pb field.*/ int avformat_open_input(AVFormatContext **ps, const char *url, ff_const59 AVInputFormat *fmt, AVDictionary **options);調用av_dump_format打印文件信息
/*** Print detailed information about the input or output format, such as* duration, bitrate, streams, container, programs, metadata, side data,* codec and time base.** @param ic the context to analyze* @param index index of the stream to dump information about* @param url the URL to print, such as source or destination file* @param is_output Select whether the specified context is an input(0) or output(1)*/ void av_dump_format(AVFormatContext *ic,int index,const char *url,int is_output);調用av_dump_format(pFmtCtx, 0, pSrcName, 0);實現(xiàn)對基本信息的打印,這里為了方便觀看,可以自己實現(xiàn)一個函數(shù),對多媒體數(shù)據(jù)進行打印。
這為了更好理解媒體信息記錄的格式,自己實現(xiàn)了一個簡單的打印接口,僅用于參考
// // Created by andrew on 2020/11/8. //#include <iostream> #include <string> extern "C" { #include <libavformat/avformat.h> #include <libavutil/log.h> #include <libavutil/dict.h> } using namespace std;// 聲明一下 因為原函數(shù)是在.c中聲明的結構體 struct AVDictionary {int count;AVDictionaryEntry *elems; };// 打印媒體信息的簡單示例 void my_av_input_dump_format(AVFormatContext *ic, int index,const char *url, int is_output){string pSrt = (is_output != 0) ? "output":"input";cout << "音視頻輸入輸出類型:" << pSrt <<endl;cout << "index = " << index << endl;if(is_output){cout << "fromat name:" << ic->oformat->name << endl;}elsecout << "fromat name:" << ic->iformat->name << endl;// urlcout << "from :" << url << endl;//ic->metadataint i = 0;i = ic->metadata->count;for (i = 0; i < ic->metadata->count; i++){cout << " " <<ic->metadata->elems[i].key << ":" <<ic->metadata->elems[i].value << endl;} }int main(int argc, char *argv[]){// 設置日志等級av_log_set_level(AV_LOG_DEBUG);if(argc< 2){av_log(NULL, AV_LOG_ERROR, "you should input media file!\n");return -1;} // 所有獲取音視頻信息都要首先注冊av_register_all();int errCode = -1;AVFormatContext *pFmtCtx = NULL;const char *pSrcName = argv[1];if((errCode = avformat_open_input(&pFmtCtx, pSrcName, NULL, NULL)) < 0){av_log(NULL, AV_LOG_ERROR, "avformat open input failed.\n");exit(1);}//官方接口 // av_dump_format(pFmtCtx, 0, pSrcName, 0);my_av_input_dump_format(pFmtCtx, 0, pSrcName, 0);//釋放資源avformat_close_input(&pFmtCtx);return 0; }執(zhí)行輸出結果:
音視頻輸入輸出類型:input index = 0 fromat name:mov,mp4,m4a,3gp,3g2,mj2 from :/work/test/test.mp4major_brand:isomminor_version:512compatible_brands:isomiso2avc1mp41track:0artist:段奧娟album:comment:163 key(Don't modify):ZWxeTBkln0EQUjdDVUZQXrJzMh33POt0FgWTvjgge2X8BzXmyZaXb9C8+H2VGrdLG7XRTMrkXzzfV9VNH7sp0KlFimbjkVbsWksXY5YrzqFNXeJX1gvrBWCV+m3aYddkvy0HxucdcxCoCrYsrnzxL97sgxi0M2VHh6PREC3j6Uz4hfWkIMGhul9aszAuzEvbUUIQXSZRHgpkVW3g3oTEwqY5CexOWMIgIZAjlFIMxafAnGU8ujxA+ufq/l/r+dQMW9OmRQVt2n4Gz1t83TrPZg==title:元気滿分encoder:Lavf57.71.100 [AVIOContext @ 0x55bf28f2d080] Statistics: 200364 bytes read, 0 seeks libgcov profiling error:/work/ffmpeg_doc/cmake-build-debug-coverage/src/CMakeFiles/ffmpeg_mediainfo.dir/ffmpeg_avformat/ffmpeg_mediainfo.cpp.gcda:overwriting an existing profile data with a different timestamp總結
以上是生活随笔為你收集整理的FFmpeg中实现对多媒体信息的获取与打印av_dump_format的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FFmpeg--av_register_
- 下一篇: 使用FFmpeg实现抽取多媒体文件的音频