標籤:style blog code http ext color
一、編譯針對iOS平台的ffmpeg庫(kxmovie)
近期有一個項目,需要播放各種格式的音頻、視頻以及網路攝影機即時監控的視頻流資料,經過多種折騰之後,最後選擇了kxmovie,kxmovie項目已經整合了ffmpeg和簡單的播放器,具體可以參考kxmovie首頁:https://github.com/kolyvan/kxmovie 編譯kxmovie很簡單,已經支援iOS 6.1 和 armv7s,一次成功,編譯過程沒出現什麼問題:
git clone git://github.com/kolyvan/kxmovie.git
cd kxmovie
git submodule update --init
rake
二、使用kxmovie1.把kxmovie/output檔案夾下檔案添加到工程2.添加架構:MediaPlayer, CoreAudio, AudioToolbox, Accelerate, QuartzCore, OpenGLES and libz.dylib,libiconv.dylib3.添加lib庫:libkxmovie.a, libavcodec.a, libavformat.a, libavutil.a, libswscale.a, libswresample.a4.播放視頻:
ViewController *vc; vc = [KxMovieViewController movieViewControllerWithContentPath:path parameters:nil]; [self presentViewController:vc animated:YES completion:nil];
5.具體使用參考demo工程:KxMovieExample三、碰到的問題播放本地視頻和網路視頻正常,播放網路攝影機即時監控視頻流(h264)的時候出現錯誤:
[rtsp @ 0x906cc00] UDP timeout, retrying with TCP
[rtsp @ 0x906cc00] Nonmatching transport in server reply
[rtsp @ 0x906cc00] Could not find codec parameters for stream 0 (Video: h264): unspecified size
Consider increasing the value for the ‘analyzeduration‘ and ‘probesize‘ options
Couldn‘t find stream information
跟蹤代碼,錯誤是在avformat_find_stream_info擷取流資訊失敗的時候的時候觸發。
if(avformat_find_stream_info(pFormatCtx,NULL) < 0) {
av_log(NULL, AV_LOG_ERROR, "Couldn‘t find stream information\n");
goto initError;
}
經過幾天的摸索,最終確定是網路的問題(在模擬器播放一直出錯,在3G網路下能播放),具體原因估計是rstp視頻流,程式預設採用udp傳輸或者組播,導致在私人網路視頻流不能正常傳輸。解決方案,把視頻流的傳輸模式強製成tcp傳輸:
……
// Open video file
pFormatCtx = avformat_alloc_context();
//有三種傳輸方式:tcp udp_multicast udp,強制採用tcp傳輸
AVDictionary* options = NULL;
av_dict_set(&options, "rtsp_transport", "tcp", 0);
if(avformat_open_input(&pFormatCtx, [moviePathcStringUsingEncoding:NSASCIIStringEncoding], NULL,&options) != 0) {
av_log(NULL, AV_LOG_ERROR, "Couldn‘t open file\n");
goto initError;
}
// Retrieve stream information
if(avformat_find_stream_info(pFormatCtx,NULL) < 0) {
av_log(NULL, AV_LOG_ERROR, "Couldn‘t find stream information\n");
goto initError;
}……