Question

I want to crop the video at particular portion.Given height,width,x and y ,I want crop the particular region on the video.Does anyone have any idea.

I have done lot of research but didn't find anything fruitful.I tried cropping using GPUImage Cropfilter but it is taking too much time.

I want to perform this operation as quick as possible,Please suggest me the technique or example how to do it. Thanks in Advance

Était-ce utile?

La solution

You can use AVMutableComposition and AVAssetExportSession Which are available in AVFoundation Framework

For more detail visit apple's reference library AVMutableComposition Class Reference and AVAssetExportSession Class Reference

AVAsset* asset = // Create your asset with source video url

AVMutableComposition *videoComposition = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionVideoTrack = [videoComposition              addMutableTrackWithMediaType:AVMediaTypeVideo     preferredTrackID:kCMPersistentTrackID_Invalid];

AVAssetTrack *clipVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];

AVMutableVideoComposition* videoComposition = [[AVMutableVideoComposition    videoComposition]retain];
videoComposition.renderSize = CGSizeMake(320, 240);
videoComposition.frameDuration = CMTimeMake(1, 30);

AVMutableVideoCompositionInstruction *instruction =    [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30) );

 AVMutableVideoCompositionLayerInstruction* transformer =    [AVMutableVideoCompositionLayerInstruction    videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
CGAffineTransform finalTransform = // setup a transform that grows the video, effectively causing a crop
[transformer setTransform:finalTransform atTime:kCMTimeZero];
instruction.layerInstructions = [NSArray arrayWithObject:transformer];
videoComposition.instructions = [NSArray arrayWithObject: instruction];

CGSize videoSize = myVideoComposition.renderSize;

CALayer *parentLayer = [CALayer layer];

CALayer *videoLayer = [CALayer layer];

NSLog(@"%f %f",_playerLayer.frame.origin.x,_playerLayer.frame.size.width);

parentLayer.frame = CGRectMake( 0, 0  , cropsize.x , cropsize.y );

[videoLayer setPosition:CGPointMake(videoLayer.position.x, videoSize.height)];

[parentLayer addSublayer:videoLayer];


videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer];

exporter = [[AVAssetExportSession alloc] initWithAsset:saveComposition    presetName:AVAssetExportPresetHighestQuality] ;
exporter.videoComposition = videoComposition;
exporter.outputURL=url3;
exporter.outputFileType=AVFileTypeQuickTimeMovie;

 [exporter exportAsynchronouslyWithCompletionHandler:^(void){}];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top