简体   繁体   English

如何裁剪视频?

[英]How to crop the video?

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. 我想在特定部分裁剪视频。给我高度,宽度,x和y,我想裁剪视频上的特定区域。任何人都有任何想法。

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. 我已经做了很多研究,但没有找到任何有成效的东西。我尝试使用GPUImage Cropfilter裁剪,但是花了太多时间。

I want to perform this operation as quick as possible,Please suggest me the technique or example how to do it. 我想尽快执行此操作,请告诉我如何操作的技巧或示例。 Thanks in Advance 提前致谢

You can use AVMutableComposition and AVAssetExportSession Which are available in AVFoundation Framework 您可以使用AVFoundation Framework中提供的AVMutableCompositionAVAssetExportSession

For more detail visit apple's reference library AVMutableComposition Class Reference and AVAssetExportSession Class Reference 有关更多详细信息,请访问apple的参考库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){}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM