FFmpeg-iOS獲取攝像頭麥克風(fēng)

FFmpeg_allluckly.cn.png

Mac編譯ffmpeg獲取FFmpeg-iOS
ffmpeg的H.264解碼
FFmpeg-iOS推流器的簡(jiǎn)單封裝

今天咱來(lái)講講在iOS 平臺(tái)上利用ffmpeg獲取到攝像頭和麥克風(fēng)鸟蟹,代碼很少溪食,后面再加上iOS 自帶的獲取攝像頭的例子;

FFmpeg獲取攝像頭麥克風(fēng)

  • 首先導(dǎo)入必要的頭文件
#include <stdio.h>
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavdevice/avdevice.h>
    
#ifdef __cplusplus
};
#endif

具體代碼簡(jiǎn)單封裝了一下西雀,如下:

- (void)showDevice{
    avdevice_register_all();
    AVFormatContext *pFormatCtx = avformat_alloc_context();
    AVDictionary* options = NULL;
    av_dict_set(&options,"list_devices","true",0);
    AVInputFormat *iformat = av_find_input_format("avfoundation");
    printf("==AVFoundation Device Info===\n");
    avformat_open_input(&pFormatCtx,"",iformat,&options);
    printf("=============================\n");
    if(avformat_open_input(&pFormatCtx,"0",iformat,NULL)!=0){
        printf("Couldn't open input stream.\n");
        return ;
    }
    
}

運(yùn)行一下可以看到日志區(qū)域的打印信息如下:

==AVFoundation Device Info===
2017-07-20 16:59:36.325150+0800 LBffmpegDemo[2040:821433] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-07-20 16:59:36.326529+0800 LBffmpegDemo[2040:821433] [MC] Reading from public effective user settings.
[AVFoundation input device @ 0x145d0100] AVFoundation video devices:
[AVFoundation input device @ 0x145d0100] [0] Back Camera
[AVFoundation input device @ 0x145d0100] [1] Front Camera
[AVFoundation input device @ 0x145d0100] AVFoundation audio devices:
[AVFoundation input device @ 0x145d0100] [0] iPhone 麥克風(fēng)
=============================
[avfoundation @ 0x153ef800] Selected framerate (29.970030) is not supported by the device
[avfoundation @ 0x153ef800] Supported modes:
[avfoundation @ 0x153ef800]   192x144@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   192x144@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   352x288@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   352x288@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   480x360@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   480x360@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   640x480@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   640x480@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   960x540@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   960x540@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   1280x720@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   1280x720@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   1280x720@[1.000000 60.000000]fps
[avfoundation @ 0x153ef800]   1280x720@[1.000000 60.000000]fps
[avfoundation @ 0x153ef800]   1920x1080@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   1920x1080@[1.000000 30.000000]fps
[avfoundation @ 0x153ef800]   2592x1936@[1.000000 20.000000]fps
[avfoundation @ 0x153ef800]   2592x1936@[1.000000 20.000000]fps
[avfoundation @ 0x153ef800]   3264x2448@[1.000000 20.000000]fps
[avfoundation @ 0x153ef800]   3264x2448@[1.000000 20.000000]fps
Couldn't open input stream.

顯然獲取到了我們的設(shè)備顶籽,前后攝像頭厨钻,和麥克風(fēng);下面我們看看系統(tǒng)自帶的獲取攝像頭的例子:

iOS系統(tǒng)自帶獲取攝像頭

  • 首先導(dǎo)入必須的頭文件
#import <AVFoundation/AVFoundation.h>
#import <UIKit/UIKit.h>
  • 然后是一些全局的屬性
@property(nonatomic, strong) AVCaptureSession                *captureSession;
@property(nonatomic, strong) AVCaptureDevice                 *captureDevice;
@property(nonatomic, strong) AVCaptureDeviceInput            *captureDeviceInput;
@property(nonatomic, strong) AVCaptureVideoDataOutput        *captureVideoDataOutput;
@property(nonatomic, assign) CGSize                          videoSize;
@property(nonatomic, strong) AVCaptureConnection             *videoCaptureConnection;
@property(nonatomic, strong) AVCaptureVideoPreviewLayer      *previewLayer;

  • 最后是簡(jiǎn)單封裝的代碼
- (void)getMovieDevice:(UIView *)view{
    self.captureSession = [[AVCaptureSession alloc] init];
    //    captureSession.sessionPreset = AVCaptureSessionPresetMedium;
    self.captureSession.sessionPreset = AVCaptureSessionPreset1920x1080;
    
    self.videoSize = [self getVideoSize:self.captureSession.sessionPreset];
    
    self.captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    
    NSError *error = nil;
    self.captureDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:self.captureDevice error:&error];
    
    if([self.captureSession canAddInput:self.captureDeviceInput])
        [self.captureSession addInput:self.captureDeviceInput];
    else
        NSLog(@"Error: %@", error);
    
    dispatch_queue_t queue = dispatch_queue_create("myEncoderH264Queue", NULL);
    
    self.captureVideoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
    [self.captureVideoDataOutput setSampleBufferDelegate:self queue:queue];
    
#if encodeModel
    // nv12
    NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
                              [NSNumber numberWithUnsignedInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange],
                              kCVPixelBufferPixelFormatTypeKey,
                              nil];
#else
    // 32bgra
    NSDictionary *settings = [[NSDictionary alloc] initWithObjectsAndKeys:
                              [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA],
                              kCVPixelBufferPixelFormatTypeKey,
                              nil];
#endif
    
    self.captureVideoDataOutput.videoSettings = settings;
    self.captureVideoDataOutput.alwaysDiscardsLateVideoFrames = YES;
    
    if ([self.captureSession canAddOutput:self.captureVideoDataOutput]) {
        [self.captureSession addOutput:self.captureVideoDataOutput];
    }
    
    // 保存Connection南捂,用于在SampleBufferDelegate中判斷數(shù)據(jù)來(lái)源(是Video/Audio?)
    self.videoCaptureConnection = [self.captureVideoDataOutput connectionWithMediaType:AVMediaTypeVideo];
    
#pragma mark -- AVCaptureVideoPreviewLayer init
    self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
    self.previewLayer.frame = view.layer.bounds;
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; // 設(shè)置預(yù)覽時(shí)的視頻縮放方式
    [[self.previewLayer connection] setVideoOrientation:AVCaptureVideoOrientationPortrait]; // 設(shè)置視頻的朝向
    [self.captureSession startRunning];
    [view.layer addSublayer:self.previewLayer];
}

- (CGSize)getVideoSize:(NSString *)sessionPreset {
    CGSize size = CGSizeZero;
    if ([sessionPreset isEqualToString:AVCaptureSessionPresetMedium]) {
        size = CGSizeMake(480, 360);
    } else if ([sessionPreset isEqualToString:AVCaptureSessionPreset1920x1080]) {
        size = CGSizeMake(1920, 1080);
    } else if ([sessionPreset isEqualToString:AVCaptureSessionPreset1280x720]) {
        size = CGSizeMake(1280, 720);
    } else if ([sessionPreset isEqualToString:AVCaptureSessionPreset640x480]) {
        size = CGSizeMake(640, 480);
    }
    
    return size;
}

#pragma mark --  AVCaptureVideo(Audio)DataOutputSampleBufferDelegate method
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    
    // 這里的sampleBuffer就是采集到的數(shù)據(jù)了旧找,但它是Video還是Audio的數(shù)據(jù)溺健,得根據(jù)connection來(lái)判斷
    if (connection == self.videoCaptureConnection) {
        
        // Video
        //        NSLog(@"在這里獲得video sampleBuffer,做進(jìn)一步處理(編碼H.264)");
        
        
#if encodeModel
        // encode
        
#else
        CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
        
        //        int pixelFormat = CVPixelBufferGetPixelFormatType(pixelBuffer);
        //        switch (pixelFormat) {
        //            case kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange:
        //                NSLog(@"Capture pixel format=NV12");
        //                break;
        //            case kCVPixelFormatType_422YpCbCr8:
        //                NSLog(@"Capture pixel format=UYUY422");
        //                break;
        //            default:
        //                NSLog(@"Capture pixel format=RGB32");
        //                break;
        //        }
        
        CVPixelBufferLockBaseAddress(pixelBuffer, 0);
        
        // render
        [openglView render:pixelBuffer];
        
        CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
#endif
    }
    //    else if (connection == _audioConnection) {
    //        
    //        // Audio
    //        NSLog(@"這里獲得audio sampleBuffer钮蛛,做進(jìn)一步處理(編碼AAC)");
    //    }
    
}

LBffmpegDemo下載地址

到此iOS平臺(tái)獲取攝像頭告一段落鞭缭,有時(shí)間再慢慢寫(xiě)FFmpeg在iOS平臺(tái)的一些其他的使用方法;有對(duì)ffmpeg感興趣的朋友可以關(guān)注我魏颓!??

我的博客即將搬運(yùn)同步至騰訊云+社區(qū)岭辣,邀請(qǐng)大家一同入駐:https://cloud.tencent.com/developer/support-plan?invite_code=3i16zjhqnn0gw

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市琼开,隨后出現(xiàn)的幾起案子易结,更是在濱河造成了極大的恐慌,老刑警劉巖柜候,帶你破解...
    沈念sama閱讀 221,576評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件搞动,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡渣刷,警方通過(guò)查閱死者的電腦和手機(jī)鹦肿,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)辅柴,“玉大人箩溃,你說(shuō)我怎么就攤上這事÷掂郑” “怎么了涣旨?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,017評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)股冗。 經(jīng)常有香客問(wèn)我霹陡,道長(zhǎng),這世上最難降的妖魔是什么止状? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,626評(píng)論 1 296
  • 正文 為了忘掉前任烹棉,我火速辦了婚禮,結(jié)果婚禮上怯疤,老公的妹妹穿的比我還像新娘浆洗。我一直安慰自己,他們只是感情好集峦,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,625評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布伏社。 她就那樣靜靜地躺著抠刺,像睡著了一般。 火紅的嫁衣襯著肌膚如雪洛口。 梳的紋絲不亂的頭發(fā)上矫付,一...
    開(kāi)封第一講書(shū)人閱讀 52,255評(píng)論 1 308
  • 那天凯沪,我揣著相機(jī)與錄音第焰,去河邊找鬼。 笑死妨马,一個(gè)胖子當(dāng)著我的面吹牛挺举,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播烘跺,決...
    沈念sama閱讀 40,825評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼湘纵,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了滤淳?” 一聲冷哼從身側(cè)響起梧喷,我...
    開(kāi)封第一講書(shū)人閱讀 39,729評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎脖咐,沒(méi)想到半個(gè)月后铺敌,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,271評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡屁擅,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,363評(píng)論 3 340
  • 正文 我和宋清朗相戀三年偿凭,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片派歌。...
    茶點(diǎn)故事閱讀 40,498評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡弯囊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出胶果,到底是詐尸還是另有隱情匾嘱,我是刑警寧澤,帶...
    沈念sama閱讀 36,183評(píng)論 5 350
  • 正文 年R本政府宣布早抠,位于F島的核電站霎烙,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏贝或。R本人自食惡果不足惜吼过,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,867評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望咪奖。 院中可真熱鬧盗忱,春花似錦、人聲如沸羊赵。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,338評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至闲昭,卻和暖如春罐寨,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背序矩。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,458評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工鸯绿, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人簸淀。 一個(gè)月前我還...
    沈念sama閱讀 48,906評(píng)論 3 376
  • 正文 我出身青樓瓶蝴,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親租幕。 傳聞我的和親對(duì)象是個(gè)殘疾皇子舷手,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,507評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件劲绪、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,119評(píng)論 4 61
  • 7月31日男窟,上午調(diào)控中心黨支部開(kāi)展主題黨課活動(dòng)。全體黨員圍坐在會(huì)議室贾富,一起重溫入黨誓詞歉眷,并學(xué)習(xí)并集體誦讀黨...
    祐伽睿曉閱讀 594評(píng)論 0 0
  • 四天心香*中道禪舞工作坊之后,今天是學(xué)習(xí)古琴的日子祷安,老師教了四個(gè)新指法姥芥,感覺(jué)自己在練習(xí)的時(shí)候,緊張汇鞭、手指僵...
    苓聲閱讀 320評(píng)論 0 0
  • 早上房東家來(lái)的熊孩子一直鬧騰個(gè)不停 加上昨天晚上洗澡洗的很晚 種種原因雜交凉唐。。我們不可避免的起晚了…… 第一站:...
    王2p小天使閱讀 214評(píng)論 0 0