美文网首页
视频录制功能(FileOutput/AVAssetWriter)

视频录制功能(FileOutput/AVAssetWriter)

作者: 豆浆油条cc | 来源:发表于2018-04-08 13:07 被阅读47次

    FileOutput(封装前后摄像头,闪光灯,分辨率设置,焦距,设置帧率,调节ISO,光感度 0.0-1.0,横竖屏切换)

    由于公司需求 视频录制需要自定义视频码率 使用AVCaptureFileOutput只能实现视频帧率更改
    所以更改项目功能 使用AVAssetWriter来实现

    基本思路

    使用

    AVCaptureVideoDataOutput//视频输
    AVCaptureAudioDataOutput//音频输出
    AVCaptureVideoDataOutputSampleBufferDelegate
    AVCaptureAudioDataOutputSampleBufferDelegate
    

    在代理(参考 http://blog.imwcl.com/2016/05/25/iOS%E5%BC%80%E5%8F%91%E8%BF%9B%E9%98%B6%20-%20%E7%94%A8AVFoundation%E8%87%AA%E5%AE%9A%E4%B9%89%E8%A7%86%E9%A2%91%E5%BD%95%E5%88%B6%E5%8A%9F%E8%83%BD/)

    逻辑思路
    根据代理获取CMSampleBufferRef 获取视频帧,通过AVAssetWriter媒体写入对象,更改视频码率编码

     videoCompressionProps = [NSDictionary dictionaryWithObjectsAndKeys:
                                             [NSNumber numberWithInteger:FrameRate], AVVideoMaxKeyFrameIntervalKey,
                                             [NSNumber numberWithInteger:BitRate], AVVideoAverageBitRateKey,
                                             AVVideoH264EntropyModeCABAC,AVVideoH264EntropyModeKey,
                                             nil];
        
        videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                         AVVideoCodecH264,AVVideoCodecKey,
                         videoCompressionProps, AVVideoCompressionPropertiesKey,
                         AVVideoScalingModeResizeAspectFill,AVVideoScalingModeKey,
                         [NSNumber numberWithInteger:videoWight],AVVideoWidthKey,
                         [NSNumber numberWithInteger:videoHeight],AVVideoHeightKey,
                         nil];
    
        //初始化视频写入类
        _videoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
        //表明输入是否应该调整其处理为实时数据源的数据
        _videoInput.expectsMediaDataInRealTime = YES;
        //将视频输入源加入
        [_writer addInput:_videoInput];
    

    通过获取CMSampleBufferRef的时间节点 调整timeOffset

    CMSampleBufferGetPresentationTimeStamp
    

    开始不断的进行拼接视频帧数据

     //获取开始写入的CMTime
     CMTime startTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
    //开始写入
     [_writer startWriting];
     [_writer startSessionAtSourceTime:startTime];
    

    下面是具体封装的demo

    https://github.com/qw9685/Two-methods-of-video-recording
    

    相关文章

      网友评论

          本文标题:视频录制功能(FileOutput/AVAssetWriter)

          本文链接:https://www.haomeiwen.com/subject/gnvphftx.html