如何裁剪视频?

时间:2013-09-04 06:13:10

标签: iphone ios ios5 ios6 ios4

我想在特定部分播放视频。给予高度,宽度,x和y,我想裁剪视频中的特定区域。是否有人有任何想法。

我做了很多研究,但没有找到任何有效的方法。我尝试使用GPUImage Cropfilter裁剪,但是花了太多时间。

我想尽快执行此操作,请告诉我技术或示例如何操作。 在此先感谢

1 个答案:

答案 0 :(得分:1)

您可以使用 AVMutableComposition AVAssetExportSession AVFoundation Framework

有关详细信息,请访问apple的参考库AVMutableComposition Class Reference和    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){}];