音視頻之視頻采集

視頻采集

AVFoundation 完成視頻采集,做個筆記吧.

初始化頁面布局

UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = @"音視頻采集";
[self.view addSubview:titleLabel];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view).offset(30);
    make.centerX.equalTo(self.view);
}];

UIButton *beginInputVideo = [UIButton buttonWithType:UIButtonTypeCustom];
[beginInputVideo setTitle:@"開始視頻采集" forState:UIControlStateNormal];
[beginInputVideo setTitle:@"停止視頻采集" forState:UIControlStateSelected];
beginInputVideo.backgroundColor = [UIColor orangeColor];
[beginInputVideo addTarget:self action:@selector(beginInputVideoClcik:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:beginInputVideo];
[beginInputVideo mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(titleLabel.mas_bottom).offset(20);
    make.centerX.equalTo(titleLabel);
    make.width.equalTo(@150);
}];

self.captureVideoPreviewView = [[UIView alloc] init];
self.captureVideoPreviewView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.captureVideoPreviewView];
[self.captureVideoPreviewView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(beginInputVideo.mas_bottom).offset(20);
    make.left.right.bottom.equalTo(self.view);
}];

懶加載需要用的對象

/** 圖像sesson */ 
@property (nonatomic, strong) AVCaptureSession *captureSession;

/** 采集數(shù)據(jù)用的相機設(shè)備 */
@property (nonatomic, strong) AVCaptureDevice *inputCamera;

/** 輸入 */
@property (nonatomic, strong) AVCaptureDeviceInput *captureDeviceInput;

/** 輸出 */
@property (nonatomic, strong) AVCaptureVideoDataOutput *captureVideoDataOutput;

/** 相機采集頁面的layer */
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;

/** 相機采集頁面View */
@property (nonatomic, strong) UIView *captureVideoPreviewView;

下面是懶加載的代碼

- (AVCaptureSession *)captureSession {
    if (!_captureSession) {
        _captureSession = [[AVCaptureSession alloc] init];
        // 設(shè)置session顯示分辨率, 用于設(shè)置output輸出流的bitrate或者說畫面質(zhì)量
        [_captureSession setSessionPreset:AVCaptureSessionPresetHigh];
        /*
         AVCaptureSessionPresetHigh  質(zhì)量最高
         AVCaptureSessionPresetPhoto Photo模式不能輸出視頻
         AVCaptureSessionPresetMedium 中等質(zhì)量
         AVCaptureSessionPresetLow 最低質(zhì)量
         AVCaptureSessionPreset320x240 320x240大刑泶伞(不支持IPhone)
         AVCaptureSessionPreset352x288 (支持iPhone)
         AVCaptureSessionPreset640x480(支持iPhone)等等...
         */
        if ([_captureSession canAddInput:self.captureDeviceInput]) {
            [_captureSession addInput:self.captureDeviceInput];
        }
        if ([_captureSession canAddOutput:self.captureVideoDataOutput]) {
            [_captureSession addOutput:self.captureVideoDataOutput];
        }
    }
    return _captureSession;
}
- (AVCaptureDevice *)inputCamera {
    if (!_inputCamera) {
        NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
        for (AVCaptureDevice *device in devices) {
            if ([device position] == AVCaptureDevicePositionBack) {
                _inputCamera = device;
            }
        }
    }
    return _inputCamera;
}

- (AVCaptureDeviceInput *)captureDeviceInput {
    if (!_captureDeviceInput) {
        NSError *error;
        _captureDeviceInput = [[AVCaptureDeviceInput alloc] initWithDevice:self.inputCamera error:&error];
        NSLog(@"%@", error);
    }
    return _captureDeviceInput;
}

- (AVCaptureVideoDataOutput *)captureVideoDataOutput {
    if (!_captureVideoDataOutput) {
        _captureVideoDataOutput = [[AVCaptureVideoDataOutput alloc] init];
        _captureVideoDataOutput.alwaysDiscardsLateVideoFrames = YES;
        [_captureVideoDataOutput setAlwaysDiscardsLateVideoFrames:NO];
        [_captureVideoDataOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
        [_captureVideoDataOutput setSampleBufferDelegate:self queue:encodeQueue];
        AVCaptureConnection *captureConnection = [_captureVideoDataOutput connectionWithMediaType:AVMediaTypeVideo];
        [captureConnection setVideoOrientation:AVCaptureVideoOrientationPortrait | AVCaptureVideoOrientationLandscapeRight | AVCaptureVideoOrientationLandscapeLeft];
    }
    return _captureVideoDataOutput;
}

- (AVCaptureVideoPreviewLayer *)captureVideoPreviewLayer {
    if (!_captureVideoPreviewLayer) {
        _captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
        /*
         AVLayerVideoGravityResizeAspect        // 適合層范圍內(nèi)(完全顯示,可能有黑邊)
         AVLayerVideoGravityResizeAspectFill    // 填充層邊界(平鋪,不拉伸,但是會被裁剪,內(nèi)容可能顯示不全)
         AVLayerVideoGravityResize              // 拉伸填充層邊界(拉伸填充視圖)
         */
        [_captureVideoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];
    }
    return _captureVideoPreviewLayer;
}

開始視頻采集

- (void)beginInputVideo {

    self.captureVideoPreviewLayer.frame = self.captureVideoPreviewView.bounds;
    
    self.captureVideoPreviewLayer.backgroundColor = [UIColor blackColor].CGColor;
    
    [self.captureVideoPreviewView.layer addSublayer:self.captureVideoPreviewLayer];
    
    [self.captureSession startRunning];
    
}

采集到視頻數(shù)據(jù)的回調(diào)

/**
 采集到數(shù)據(jù)的回調(diào)

 @param output outPut對象
 @param sampleBuffer 數(shù)據(jù)
 @param connection 鏈接
 */
- (void)captureOutput:(AVCaptureOutput *)output didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
    NSLog(@"%@", sampleBuffer);
}

注: 如發(fā)現(xiàn)問題,請留言.謝謝.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末梅屉,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子鳞贷,更是在濱河造成了極大的恐慌坯汤,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,294評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件搀愧,死亡現(xiàn)場離奇詭異惰聂,居然都是意外死亡,警方通過查閱死者的電腦和手機妈橄,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,493評論 3 385
  • 文/潘曉璐 我一進店門庶近,熙熙樓的掌柜王于貴愁眉苦臉地迎上來翁脆,“玉大人眷蚓,你說我怎么就攤上這事》捶” “怎么了沙热?”我有些...
    開封第一講書人閱讀 157,790評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長罢缸。 經(jīng)常有香客問我篙贸,道長,這世上最難降的妖魔是什么枫疆? 我笑而不...
    開封第一講書人閱讀 56,595評論 1 284
  • 正文 為了忘掉前任爵川,我火速辦了婚禮,結(jié)果婚禮上息楔,老公的妹妹穿的比我還像新娘寝贡。我一直安慰自己,他們只是感情好值依,可當(dāng)我...
    茶點故事閱讀 65,718評論 6 386
  • 文/花漫 我一把揭開白布圃泡。 她就那樣靜靜地躺著,像睡著了一般愿险。 火紅的嫁衣襯著肌膚如雪颇蜡。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,906評論 1 290
  • 那天,我揣著相機與錄音风秤,去河邊找鬼鳖目。 笑死,一個胖子當(dāng)著我的面吹牛缤弦,可吹牛的內(nèi)容都是我干的疑苔。 我是一名探鬼主播,決...
    沈念sama閱讀 39,053評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼甸鸟,長吁一口氣:“原來是場噩夢啊……” “哼惦费!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起抢韭,我...
    開封第一講書人閱讀 37,797評論 0 268
  • 序言:老撾萬榮一對情侶失蹤薪贫,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后刻恭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體瞧省,經(jīng)...
    沈念sama閱讀 44,250評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,570評論 2 327
  • 正文 我和宋清朗相戀三年鳍贾,在試婚紗的時候發(fā)現(xiàn)自己被綠了鞍匾。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,711評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡骑科,死狀恐怖橡淑,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情咆爽,我是刑警寧澤梁棠,帶...
    沈念sama閱讀 34,388評論 4 332
  • 正文 年R本政府宣布,位于F島的核電站斗埂,受9級特大地震影響符糊,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜呛凶,卻給世界環(huán)境...
    茶點故事閱讀 40,018評論 3 316
  • 文/蒙蒙 一男娄、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧漾稀,春花似錦模闲、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,796評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至缕贡,卻和暖如春翁授,著一層夾襖步出監(jiān)牢的瞬間拣播,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,023評論 1 266
  • 我被黑心中介騙來泰國打工收擦, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留贮配,地道東北人。 一個月前我還...
    沈念sama閱讀 46,461評論 2 360
  • 正文 我出身青樓塞赂,卻偏偏與公主長得像泪勒,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子宴猾,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,595評論 2 350

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