解決 ffmpeg 在 avformat_alloc_output_context2 或者 av_guess_format獲取失敗的問題:
avformat_alloc_output_context2 本質上是調用了 av_guess_format,這個函數會從所編譯的ffmpeg庫支持的muxer中查找與文件名有關聯的container類型。如:
char *filename = “test.mp4”;
AVFormatContext *oc;
avformat_alloc_output_context2(&oc,NULL, NULL, filename);
將會在所支持的muxer中查找MP4類型的容器,而當查找不到時,傳入的 AVFormatContext *oc將不能被初始化,出現以上情況,可能是以下原因:
1、 未調用 av_register_all(); 初始化ffmpeg。
2、 編譯ffmpeg的時候未將所需要的muxer給包含進來,這需要重新編譯ffmpeg。如需要MP4類型的muxer則在編譯時加上 --enable-muxer= MP4包含MP4,或者 --enable-muxers 包含所有ffmpeg支持的類型(這樣編譯出來的avformat會比較大),然後重新編譯ffmpeg就OK了。