我想在特定部分裁剪视频。给定高度、宽度、x 和 y,我想裁剪视频上的特定区域。有人知道吗?
我做了很多研究,但没有发现任何成果。我尝试使用 GPUImage Cropfilter 进行裁剪,但它花费了太多时间。
我想尽快执行此操作,请建议我如何执行此操作的技术或示例。提前致谢
您可以使用AVMutableComposition
和AVAssetExportSession
哪些可用AVFoundation Framework
有关更多详细信息,请访问苹果的参考库AVMutableComposition 类参考 和 AVAssetExportSession 类参考
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){}];