捕获仅在预览框架中显示的视频iOS

时间:2017-12-07 11:54:52

标签: ios objective-c iphone avfoundation avasset

enter image description here

嗨,我正在使用AVFoundation框架工作相机。现在视频录制正常但我想在屏幕中录制仅视频预览区域。像instagram这样的东西。请帮助,如果有人有想法 - 我尝试使用以下代码执行此操作,但未按要求获取视频 -

AVAsset *asset = [AVAsset assetWithURL:inputURL];

   AVAssetTrack *assetVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] lastObject];


    CGFloat sideLength = _previewView.frame.size.height;
    CGSize originalSize = assetVideoTrack.naturalSize;

    CGFloat scale;

    if (originalSize.width < originalSize.height) {
        scale = sideLength / originalSize.width;
    } else {
        scale = sideLength / originalSize.height;
    }

    CGSize scaledSize = CGSizeMake(originalSize.width * scale, originalSize.height * scale);

    CGPoint topLeft = CGPointMake(sideLength * .5 - scaledSize.width * .5, sideLength  * .5 - scaledSize.height * .5);



    AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:assetVideoTrack];

    CGAffineTransform orientationTransform = assetVideoTrack.preferredTransform;



    if (orientationTransform.tx == originalSize.width || orientationTransform.tx == originalSize.height) {
        orientationTransform.tx = sideLength;
    }

    if (orientationTransform.ty == originalSize.width || orientationTransform.ty == originalSize.height) {
        orientationTransform.ty = sideLength;
    }



    CGAffineTransform transform = CGAffineTransformConcat(CGAffineTransformConcat(CGAffineTransformMakeScale(scale, scale),  CGAffineTransformMakeTranslation(topLeft.x, topLeft.y)), orientationTransform);

    [layerInstruction setTransform:transform atTime:kCMTimeZero];



    AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];

    instruction.layerInstructions = @[layerInstruction];
    instruction.timeRange = assetVideoTrack.timeRange;



    AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];

    videoComposition.renderSize = CGSizeMake(sideLength, sideLength);
    videoComposition.renderScale = 1.0;
    videoComposition.frameDuration = CMTimeMake(1, 30);

    videoComposition.instructions = @[instruction];

    // export
    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality] ;
 //   exporter.videoComposition = videoComposition;
    exporter.outputURL=outputURL;
    exporter.outputFileType=AVFileTypeQuickTimeMovie;

    [exporter exportAsynchronouslyWithCompletionHandler:^(void){

        switch(exporter.status) {
            case AVAssetExportSessionStatusCompleted:
                 completion(exporter);
                NSLog(@"file exported successfully");
                break;
            default:
                NSLog(@"file did not export successfully");
        }
    }];

预览图层代码 -

@property(非原子,弱)IBOutlet PreviewView * previewView;

AVCaptureVideoPreviewLayer *previewLayer = (AVCaptureVideoPreviewLayer *)self.previewView.layer;
            connection.videoOrientation = previewLayer.connection.videoOrientation;
             previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

previewLayer.bounds = self.previewView.bounds;                 previewLayer.position = CGPointMake(CGRectGetMidX(self.previewView.bounds),CGRectGetMidY(self.previewView.bounds));

请给我一个想法,以便我可以解决这个问题。谢谢

0 个答案:

没有答案