iOS音视频---使用AVFoundation(AVCapture+AVAssetWriter+AVPlayer)采集录制和播放音视频

首先了解下AVFoundation

AVFoundation框架是ios中很重要的框架,是苹果 OS X 系统和 iOS系统中用于处理基于时间的媒体数据的高级框架,其设计过程高度依赖多线程机制。所有与视频音频相关的软硬件控制都在这个框架里面。
AVFoundation是可以用它来播放和创建基于时间的视听媒体的几个框架之一,它提供了基于时间的视听数据的详细界别上的OC接口。可以用它来检查、创建、编辑、重新编码媒体文件。也可以从设备得到输入流和实时捕捉回放过程中操控视频,是用于处理基于时间的媒体数据的高级OC框架。充分利用了多核硬件的优势并大量使用block和Grand Central Dispatch(GCD)机制将复杂的计算进程放在后台线程运行。自动提供硬件加速操作,确保在大部分设备上应用程序能以最佳性能运行。

而AVAssetWriter和AVPlayer也是属于AVFoundation框架内的
Github代码地址

一、采集音视频

/**  负责输入和输出设备之间的数据传递  */
@property (strong, nonatomic) AVCaptureSession *captureSession;
/**  视频输入  */
@property (nonatomic, strong) AVCaptureDeviceInput *videoInput;
/**  视频输出  */
@property (nonatomic, strong) AVCaptureVideoDataOutput *videoOutput;
/**  声音输出  */
@property (nonatomic, strong) AVCaptureAudioDataOutput *audioOutput;
/**  预览图层  */
@property (strong, nonatomic) AVCaptureVideoPreviewLayer *captureVideoPreviewLayer;

这里使用这些类来采集音视频。

首先,初始化AVCaptureSession

- (AVCaptureSession *)captureSession
{
    if (_captureSession == nil)
    {
        _captureSession = [[AVCaptureSession alloc] init];
        
        if ([_captureSession canSetSessionPreset:AVCaptureSessionPresetHigh])
        {
            _captureSession.sessionPreset = AVCaptureSessionPresetHigh;
        }
    }
    
    return _captureSession;
}

初始化视频音频输入输出

/**
 *  设置视频输入
 */
- (void)setupVideo
{
    AVCaptureDevice *captureDevice = [self getCameraDeviceWithPosition:AVCaptureDevicePositionBack];
    
    if (!captureDevice)
    {
        NSLog(@"取得后置摄像头时出现问题.");
        
        return;
    }
    
    NSError *error = nil;
    
    AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:captureDevice error:&error];
    if (error)
    {
        NSLog(@"取得设备输入videoInput对象时出错,错误原因:%@", error);
        
        return;
    }
    
    //3、将设备输出添加到会话中
    if ([self.captureSession canAddInput:videoInput])
    {
        [self.captureSession addInput:videoInput];
    }
    
    self.videoOutput = [[AVCaptureVideoDataOutput alloc] init];
    
    self.videoOutput.alwaysDiscardsLateVideoFrames = NO; //立即丢弃旧帧,节省内存,默认YES
    
    [self.videoOutput setVideoSettings:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange] forKey:(id)kCVPixelBufferPixelFormatTypeKey]];
    
    [self.videoOutput setSampleBufferDelegate:self queue:self.videoQueue];
    
    if ([self.captureSession canAddOutput:self.videoOutput])
    {
        [self.captureSession addOutput:self.videoOutput];
    }
    
    AVCaptureConnection *connection = [self.videoOutput connectionWithMediaType:AVMediaTypeVideo];
    
    [connection setVideoOrientation:AVCaptureVideoOrientationPortrait];
    
    self.videoInput = videoInput;
}

/**
 *  设置音频录入
 */
- (void)setupAudio
{
    NSError *error = nil;
    AVCaptureDeviceInput *audioInput = [[AVCaptureDeviceInput alloc] initWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio] error:&error];
    if (error)
    {
        NSLog(@"取得设备输入audioInput对象时出错,错误原因:%@", error);
        
        return;
    }
    if ([self.captureSession canAddInput:audioInput])
    {
        [self.captureSession addInput:audioInput];
    }
    
    self.audioOutput = [[AVCaptureAudioDataOutput alloc] init];
    
    [self.audioOutput setSampleBufferDelegate:self queue:self.videoQueue];
    
    if([self.captureSession canAddOutput:self.audioOutput])
    {
        [self.captureSession addOutput:self.audioOutput];
    }
}

设置预览图层

/**
 *  设置预览layer
 */
- (void)setupCaptureVideoPreviewLayer
{
    _captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
    
    _captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspect;           //填充模式
    
    [_captureVideoPreviewLayer setFrame:self.superView.bounds];
    
    [self.superView.layer addSublayer:_captureVideoPreviewLayer];
}

然后开始采集
采集的音视频数据回调代理方法:

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    
    @autoreleasepool
    {
        //视频
        if (connection == [self.videoOutput connectionWithMediaType:AVMediaTypeVideo])
        {
            @synchronized(self)
            {
                if (self.captureBlock) {
                    
                    self.captureBlock(sampleBuffer, AVMediaTypeVideo);
                }
            }
        }
        
        //音频
        if (connection == [self.audioOutput connectionWithMediaType:AVMediaTypeAudio])
        {
            @synchronized(self)
            {
                if (self.captureBlock) {
                    
                    self.captureBlock(sampleBuffer, AVMediaTypeAudio);
                }
            }
        }
    }
}

这里将采集的数据传递出去,进行录制处理

二、录制音视频

/**  写入音视频  */
@property (nonatomic, strong) AVAssetWriter *assetWriter;
/**  写入视频输出  */
@property (nonatomic, strong) AVAssetWriterInput *assetWriterVideoInput;
/**  写入音频输出  */
@property (nonatomic, strong) AVAssetWriterInput *assetWriterAudioInput;

这里使用AVAssetWriter来录制音视频

首先初始化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 };
    CGFloat width = kScreenWidth;
    CGFloat height = kScreenHeight;
    
    //视频属性
    NSDictionary *videoCompressionSettings = @{ AVVideoCodecKey : AVVideoCodecTypeH264,
                                                AVVideoWidthKey : @(width * 2),
                                                AVVideoHeightKey : @(height * 2),
                                                AVVideoScalingModeKey : AVVideoScalingModeResizeAspectFill,
                                                AVVideoCompressionPropertiesKey : compressionProperties };
    
    _assetWriterVideoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoCompressionSettings];
    //expectsMediaDataInRealTime 必须设为yes,需要从capture session 实时获取数据
    _assetWriterVideoInput.expectsMediaDataInRealTime = YES;
    
    // 音频设置
    NSDictionary *audioCompressionSettings = @{ AVEncoderBitRatePerChannelKey : @(28000),
                                                AVFormatIDKey : @(kAudioFormatMPEG4AAC),
                                                AVNumberOfChannelsKey : @(1),
                                                AVSampleRateKey : @(22050) };
    
    _assetWriterAudioInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings: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;
}

然后写入数据

/**
 *  开始写入数据
 */
- (void)appendSampleBuffer:(CMSampleBufferRef)sampleBuffer ofMediaType:(NSString *)mediaType
{
    if (sampleBuffer == NULL)
    {
        NSLog(@"empty sampleBuffer");
        return;
    }
    
    @autoreleasepool
    {
        if (!self.canWrite && mediaType == AVMediaTypeVideo && self.assetWriter && self.assetWriter.status != AVAssetWriterStatusWriting)
        {
            
            [self.assetWriter startWriting];
            [self.assetWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp(sampleBuffer)];
            self.canWrite = YES;
        }
        
        //写入视频数据
        if (mediaType == AVMediaTypeVideo && self.assetWriterVideoInput.readyForMoreMediaData)
        {
            if (![self.assetWriterVideoInput appendSampleBuffer:sampleBuffer])
            {
                @synchronized (self)
                {
                    [self stopVideoRecorder];
                }
            }
        }
        
        //写入音频数据
        if (mediaType == AVMediaTypeAudio && self.assetWriterAudioInput.readyForMoreMediaData)
        {
            if (![self.assetWriterAudioInput appendSampleBuffer:sampleBuffer])
            {
                @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;
        }];
    }
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [weakSelf saveVideo];
        
        [weakSelf previewVideoAfterShoot];
    });
}

三、保存并播放音视频

在保存前,需要先将录制的音视频数据合成

- (void)cropWithVideoUrlStr:(NSURL *)videoUrl completion:(void (^)(NSURL *outputURL, Float64 videoDuration, BOOL isSuccess))completionHandle
{
    AVURLAsset *asset =[[AVURLAsset alloc] initWithURL:videoUrl options:nil];
    
    //获取视频总时长
    Float64 endTime = CMTimeGetSeconds(asset.duration);
    
    if (endTime > 10)
    {
        endTime = 10.0f;
    }
    
    Float64 startTime = 0;
    
    NSString *outputFilePath = [self createVideoFilePath];
    
    NSURL *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
    
    NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];
    
    if ([compatiblePresets containsObject:AVAssetExportPresetMediumQuality])
    {
        
        AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
                                               initWithAsset:asset presetName:AVAssetExportPresetPassthrough];
        
        NSURL *outputURL = outputFileUrl;
        
        exportSession.outputURL = outputURL;
        exportSession.outputFileType = AVFileTypeMPEG4;
        exportSession.shouldOptimizeForNetworkUse = YES;
        
        CMTime start = CMTimeMakeWithSeconds(startTime, asset.duration.timescale);
        CMTime duration = CMTimeMakeWithSeconds(endTime - startTime,asset.duration.timescale);
        CMTimeRange range = CMTimeRangeMake(start, duration);
        exportSession.timeRange = range;
        
        [exportSession exportAsynchronouslyWithCompletionHandler:^{
            switch ([exportSession status]) {
                case AVAssetExportSessionStatusFailed:
                {
                    NSLog(@"合成失败:%@", [[exportSession error] description]);
                    completionHandle(outputURL, endTime, NO);
                }
                    break;
                case AVAssetExportSessionStatusCancelled:
                {
                    completionHandle(outputURL, endTime, NO);
                }
                    break;
                case AVAssetExportSessionStatusCompleted:
                {
                    completionHandle(outputURL, endTime, YES);
                }
                    break;
                default:
                {
                    completionHandle(outputURL, endTime, NO);
                } break;
            }
        }];
    }
}

然后再将视频保存到手机相册

使用Photos框架保存

/**
 保存视频
 */
- (void)saveVideo
{
    [self cropWithVideoUrlStr:self.videoURL completion:^(NSURL *videoUrl, Float64 videoDuration, BOOL isSuccess) {
        
        if (isSuccess)
        {
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            
            NSString * assetCollectionName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
            
            if (assetCollectionName == nil)
            {
                assetCollectionName = @"视频相册";
            }
            
            __block NSString *blockAssetCollectionName = assetCollectionName;
            
            __block NSURL *blockVideoUrl = videoUrl;
            
            PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                NSError *error = nil;
                __block NSString *assetId = nil;
                __block NSString *assetCollectionId = nil;
                
                // 保存视频到【Camera Roll】(相机胶卷)
                [library performChangesAndWait:^{
                    
                    assetId = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:blockVideoUrl].placeholderForCreatedAsset.localIdentifier;
                    
                } error:&error];
                
                NSLog(@"error1: %@", error);
                
                // 获取曾经创建过的自定义视频相册名字
                PHAssetCollection *createdAssetCollection = nil;
                PHFetchResult <PHAssetCollection*> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
                for (PHAssetCollection *assetCollection in assetCollections)
                {
                    if ([assetCollection.localizedTitle isEqualToString:blockAssetCollectionName])
                    {
                        createdAssetCollection = assetCollection;
                        break;
                    }
                }
                
                //如果这个自定义框架没有创建过
                if (createdAssetCollection == nil)
                {
                    //创建新的[自定义的 Album](相簿\相册)
                    [library performChangesAndWait:^{
                        
                        assetCollectionId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:blockAssetCollectionName].placeholderForCreatedAssetCollection.localIdentifier;
                        
                    } error:&error];
                    
                    NSLog(@"error2: %@", error);
                    
                    //抓取刚创建完的视频相册对象
                    createdAssetCollection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[assetCollectionId] options:nil].firstObject;
                    
                }
                
                // 将【Camera Roll】(相机胶卷)的视频 添加到【自定义Album】(相簿\相册)中
                [library performChangesAndWait:^{
                    PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdAssetCollection];
                    
                    [request addAssets:[PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil]];
                    
                } error:&error];
                
                NSLog(@"error3: %@", error);
                
            });
        }
        else
        {
            NSLog(@"保存视频失败!");
            
            [[NSFileManager defaultManager] removeItemAtURL:self.videoURL error:nil];
            
            self.videoURL = nil;
            
            [[NSFileManager defaultManager] removeItemAtURL:videoUrl error:nil];
        }
    }];
}

然后就可以去播放了

这里使用AVPlayer来播放

/**  视频预览View  */
@property (strong, nonatomic) UIView *videoPreviewContainerView;
/**  播放器  */
@property (strong, nonatomic) AVPlayer *player;
- (void)previewVideoAfterShoot
{
    if (self.videoURL == nil || self.videoPreviewContainerView != nil)
    {
        return;
    }
    
    AVURLAsset *asset = [AVURLAsset assetWithURL:self.videoURL];
    
    // 初始化AVPlayer
    self.videoPreviewContainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    
    self.videoPreviewContainerView.backgroundColor = [UIColor blackColor];
    
    AVPlayerItem * playerItem = [AVPlayerItem playerItemWithAsset:asset];
    
    self.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
    
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    
    playerLayer.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
    
    [self.videoPreviewContainerView.layer addSublayer:playerLayer];
    
    // 其余UI布局设置
    [self.view addSubview:self.videoPreviewContainerView];
    [self.view bringSubviewToFront:self.videoPreviewContainerView];
    
    // 重复播放预览视频
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playVideoFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];
    
    // 开始播放
    [self.player play];
}

Github代码地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,635评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,543评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,083评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,640评论 1 296
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,640评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,262评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,833评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,736评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,280评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,369评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,503评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,185评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,870评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,340评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,460评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,909评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,512评论 2 359

推荐阅读更多精彩内容