美文网首页
AVAssetWriter 和 AVAssetWriterInp

AVAssetWriter 和 AVAssetWriterInp

作者: 秋叶红90 | 来源:发表于2020-05-09 16:35 被阅读0次

    AVAssetWriter 可以添加 AVAssetWriterInput
    AVAssetWriterInput 分文好几种类型
    可以直接 接受buffer 来录制成视频和声音文件
    多段视频就是这个道理
    配置如下

    /**
     *  设置写入视频属性
     */
    - (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;
    }
    
    

    相关文章

      网友评论

          本文标题:AVAssetWriter 和 AVAssetWriterInp

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