我知道这是一个常见问题,我环顾四周,没有明确的答案。裁剪视频到广场iOS
的代码是从文章:http://www.netwalk.be/article/record-square-video-ios
我想:我要裁剪的视频,使之方。
所以基本上这就是我正在做的。这对我来说很合理,但由于某种原因,视频不会裁剪,实际上,它保持相同的尺寸(宽度和高度)。
AVAsset* asset = [AVAsset assetWithURL:self.videoURL];
AVMutableComposition *composition = [AVMutableComposition composition];
[composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
// input clip
AVAssetTrack *clipVideoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
// make it square
AVMutableVideoComposition* videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.renderSize = CGSizeMake(3.0 , 3.0);
videoComposition.frameDuration = CMTimeMake(1, 30);
AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(clipVideoTrack.naturalSize.width, clipVideoTrack.naturalSize.width));
// rotate to portrait
AVMutableVideoCompositionLayerInstruction* transformer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:clipVideoTrack];
CGAffineTransform t1 = CGAffineTransformMakeTranslation(clipVideoTrack.naturalSize.height, -(clipVideoTrack.naturalSize.width - clipVideoTrack.naturalSize.height) /2);
CGAffineTransform t2 = CGAffineTransformRotate(t1, M_PI_2);
CGAffineTransform finalTransform = t2;
[transformer setTransform:finalTransform atTime:kCMTimeZero];
instruction.layerInstructions = [NSArray arrayWithObject:transformer];
videoComposition.instructions = [NSArray arrayWithObject: instruction];
AVAssetExportSession *exporter;
// export
exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality] ;
exporter.videoComposition = videoComposition;
exporter.outputURL=self.videoURL;
exporter.outputFileType=AVFileTypeMPEG4;
[exporter exportAsynchronouslyWithCompletionHandler:^(void){
NSLog(@"Exporting done!");
}];
我相信问题出在最后出口商。要么它没有正确导出,要么有我失踪的其他东西。请有人提及我一个很好的方法来做到这一点。谢谢。
难道解决方案如下解决您的问题。我有完全相同的问题,我似乎无法弄清楚为什么视频保持相同的大小? – 2015-09-18 16:49:07
如果你能解决这个问题,请帮助我。 http://stackoverflow.com/questions/43451879/square-video-using-avfoundation – 2017-04-17 13:22:20