当前位置: 首页 > news >正文

网站做中英文切换济南市建设监理有限公司网站

网站做中英文切换,济南市建设监理有限公司网站,专门做封面的网站,怎么才能把网站优化做好AVCaptureSession配置采集行为并协调从输入设备到采集输出的数据流。要执行实时音视频采集,需要实例化采集会话并添加适当的输入和输出。 AVCaptureSession:管理输入输出音视频流AVCaptureDevice:相机硬件的接口,用于控制硬件特性…

AVCaptureSession配置采集行为并协调从输入设备到采集输出的数据流。要执行实时音视频采集,需要实例化采集会话并添加适当的输入和输出。

  • AVCaptureSession:管理输入输出音视频流
  • AVCaptureDevice:相机硬件的接口,用于控制硬件特性,诸如镜头的位置(前后摄像头)、曝光、闪光灯等。
  • AVCaptureInput:配置输入设备,提供来自设备的数据
  • AVCaptureOutput:管理输出的音视频数据流
  • AVCaptureConnection:输入与输出的连接
  • AVCaptureVideoPreviewLayer:显示当前相机正在采集的状况
  • AVAssetWriter:将媒体数据写入到容器文件

初始化AVCaptureSession

- (AVCaptureSession *)captureSession {if (_captureSession == nil){_captureSession = [[AVCaptureSession alloc] init];if ([_captureSession canSetSessionPreset:AVCaptureSessionPresetHigh]) {_captureSession.sessionPreset = AVCaptureSessionPreset1280x720;}}return _captureSession;
}- (dispatch_queue_t)videoQueue {if (!_videoQueue) {_videoQueue = dispatch_queue_create("VideoCapture", DISPATCH_QUEUE_SERIAL);}return _videoQueue;
}

添加视频输入

- (AVCaptureDevice *)getCameraDeviceWithPosition:(AVCaptureDevicePosition )position {AVCaptureDeviceDiscoverySession *deviceDiscoverySession =  [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[AVCaptureDeviceTypeBuiltInWideAngleCamera] mediaType:AVMediaTypeVideo position:position];for (AVCaptureDevice *device in deviceDiscoverySession.devices) {if ([device position] == position) {return device;}}return nil;
}- (void)setupVideoInput {AVCaptureDevice *captureDevice = [self getCameraDeviceWithPosition:AVCaptureDevicePositionBack];if (!captureDevice){NSLog(@"captureDevice failed");return;}NSError *error = nil;self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&error];if (error) {NSLog(@"videoInput error:%@", error);return;}if ([self.captureSession canAddInput:self.videoInput]) {[self.captureSession addInput:self.videoInput];}
}

添加音频输入

- (void)setupAudioInput {AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];NSError *error = nil;self.audioInput = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&error];if (error) {NSLog(@"audioInput error:%@", error);return;}if ([self.captureSession canAddInput:self.audioInput]) {[self.captureSession addInput:self.audioInput];}
}

添加视频输出

- (void)setupVideoOutput {self.videoOutput = [[AVCaptureVideoDataOutput alloc] init];self.videoOutput.alwaysDiscardsLateVideoFrames = YES;[self.videoOutput setSampleBufferDelegate:self queue:self.videoQueue];if ([self.captureSession canAddOutput:self.videoOutput]) {[self.captureSession addOutput:self.videoOutput];}
}

添加音频输出

- (void)setupAudioOutput {self.audioOutput = [[AVCaptureAudioDataOutput alloc] init];[self.audioOutput setSampleBufferDelegate:self queue:self.videoQueue];if ([self.captureSession canAddOutput:self.audioOutput]) {[self.captureSession addOutput:self.audioOutput];}
}

设置视频预览

- (void)setupCaptureVideoPreviewLayer:(UIView *)previewView {_captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];CALayer *layer = previewView.layer;_captureVideoPreviewLayer.frame = previewView.bounds;_captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspect;_captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;_captureVideoPreviewLayer.connection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;[layer insertSublayer:_captureVideoPreviewLayer atIndex:0];
}

开始和结束采集会话

- (void)startSession {if (![self.captureSession isRunning]) {[self.captureSession startRunning];}
}- (void)stopSession{if ([self.captureSession isRunning]) {[self.captureSession stopRunning];}
}

初始化AVAssetWriter,将音视频保存到视频文件

- (void)setUpWriter {if (self.videoURL == nil) {return;}self.assetWriter = [AVAssetWriter assetWriterWithURL:self.videoURL fileType:AVFileTypeMPEG4 error:nil];NSInteger numPixels = kScreenWidth * kScreenHeight;CGFloat bitsPerPixel = 12.0;NSInteger bitsPerSecond = numPixels * bitsPerPixel;NSDictionary *compressionProperties = @{ AVVideoAverageBitRateKey : @(bitsPerSecond),AVVideoExpectedSourceFrameRateKey : @(15),AVVideoMaxKeyFrameIntervalKey : @(15),AVVideoProfileLevelKey : AVVideoProfileLevelH264BaselineAutoLevel };self.videoCompressionSettings = @{ AVVideoCodecKey : AVVideoCodecTypeH264,AVVideoWidthKey : @(width * 2),AVVideoHeightKey : @(height * 2),AVVideoScalingModeKey : AVVideoScalingModeResizeAspect,AVVideoCompressionPropertiesKey : compressionProperties };_assetWriterVideoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:self.videoCompressionSettings];_assetWriterVideoInput.expectsMediaDataInRealTime = YES;self.audioCompressionSettings = @{ AVEncoderBitRatePerChannelKey : @(28000),AVFormatIDKey : @(kAudioFormatMPEG4AAC),AVNumberOfChannelsKey : @(1),AVSampleRateKey : @(22050) };_assetWriterAudioInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:self.audioCompressionSettings];_assetWriterAudioInput.expectsMediaDataInRealTime = YES;if ([_assetWriter canAddInput:_assetWriterVideoInput]){[_assetWriter addInput:_assetWriterVideoInput];}else{NSLog(@"AssetWriter videoInput append Failed");}if ([_assetWriter canAddInput:_assetWriterAudioInput]){[_assetWriter addInput:_assetWriterAudioInput];}else{NSLog(@"AssetWriter audioInput Append Failed");}_canWrite = NO;
}

AVCaptureVideoDataOutputSampleBufferDelegate和AVCaptureAudioDataOutputSampleBufferDelegate音视频处理

#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate|AVCaptureAudioDataOutputSampleBufferDelegate
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {@autoreleasepool{if (connection == [self.videoOutput connectionWithMediaType:AVMediaTypeVideo]) {@synchronized(self){[self appendSampleBuffer:sampleBuffer ofMediaType:AVMediaTypeVideo];}}if (connection == [self.audioOutput connectionWithMediaType:AVMediaTypeAudio]) {@synchronized(self) {[self appendSampleBuffer:sampleBuffer ofMediaType:AVMediaTypeAudio];}}}
}- (void)appendSampleBuffer:(CMSampleBufferRef)sampleBuffer ofMediaType:(NSString *)mediaType {if (sampleBuffer == NULL){NSLog(@"empty sampleBuffer");return;}@autoreleasepool{if (!self.canWrite && mediaType == AVMediaTypeVideo){[self.assetWriter startWriting];[self.assetWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)];self.canWrite = YES;}if (mediaType == AVMediaTypeVideo){if (self.assetWriterVideoInput.readyForMoreMediaData){BOOL success = [self.assetWriterVideoInput appendSampleBuffer:sampleBuffer];if (!success){NSLog(@"assetWriterVideoInput appendSampleBuffer fail");@synchronized (self){[self stopVideoRecorder];}}}}if (mediaType == AVMediaTypeAudio){if (self.assetWriterAudioInput.readyForMoreMediaData){BOOL success = [self.assetWriterAudioInput appendSampleBuffer:sampleBuffer];if (!success){NSLog(@"assetWriterAudioInput appendSampleBuffer fail");@synchronized (self){[self stopVideoRecorder];}}}}}
}

停止视频录制

- (void)stopVideoRecorder {__weak __typeof(self)weakSelf = self;if(_assetWriter && _assetWriter.status == AVAssetWriterStatusWriting) {[_assetWriter finishWritingWithCompletionHandler:^{weakSelf.canWrite = NO;weakSelf.assetWriter = nil;weakSelf.assetWriterAudioInput = nil;weakSelf.assetWriterVideoInput = nil;}];}
}

http://www.yayakq.cn/news/20505/

相关文章:

  • 英文网站设计多少钱网站建设部门
  • 宝安区住房和建设局网站wordpress yzipicc
  • 西宁市网站建设高端网络平台建站流程
  • 页眉做的好的网站网站中新颖的功能
  • 做网站代理工作安全吗网站建设找单
  • windows 2008 网站wordpress 模板使用
  • 网站策划书撰写流程东莞网站建设公司服务平台
  • 梭子手做鱼网站家具设计师
  • 网站开发注意事项泸州网站建设多少钱
  • 网站开发的条件电商会学着做网站呢
  • 个人网站设计论文ppt怎样做3d动画短视频网站
  • 如何自建网站做外贸wordpress 首页跳转
  • 网站建设优化公司企业网站推广的方法
  • 网站建设调查回访表qq降龙是哪个公司开发的
  • 提供零基础网站建设教学网站建设方案设计书
  • 山东新华电脑学院学网站开发深圳域名空间
  • 网站开发公司气氛wordpress用户冻结
  • 帝国网站做图片轮播服装 多语言 网站源码
  • 如何做网站弹窗广告自学网站建设多久
  • 上海制作网站公司哪家好运城网站建设求职简历
  • 电子商务网站建设 以为例做分析报表的网站
  • 商标设计网站哪个好全国企业营业执照查询
  • 网站模板购买 优帮云衡水高端网站建设
  • 公司网站找谁做网站建设合同 文库
  • 怎么创造一个网站金山网站建设公司
  • 1717做网站建设银行网站用户名更改
  • 河源哪里做网站泰安市人才服务平台
  • 广东省住房建设厅网站6wordpress创建新页面
  • 遵义网站建设哪家好新乡做企业网站的公司
  • 织梦cms 学校网站模板城乡建设部网站施工员证书查询