最终目标是将我的480x640 .mov视频裁剪为精确分辨率为480x360的4:3比例风景视频。
我知道在这个链接上提出了几乎确切的问题: How do I use AVFoundation to crop a video
我相信显示已经裁剪过的预览图层会更容易,然后在最后裁剪最终(未剪切的肖像480x640)视频。我一直在玩下面的代码。目前,我只是试图让它改变任何东西 - 那里有无关的代码我稍后会修剪。以下代码尝试将视频缩放到70%:
-(void) convertVideoToSize{
AVURLAsset *videoToConvert = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:tempURL] options:nil];
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[firstTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoToConvert.duration) ofTrack:[[videoToConvert tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];
AVMutableVideoCompositionInstruction *mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero,videoToConvert.duration);
AVMutableVideoCompositionLayerInstruction *firstLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:firstTrack];
//WE PERFORM VIDEO EDITING OPERATIONS HERE
CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation( 0,-420);
CGAffineTransform Rotate = CGAffineTransformMakeRotation(M_PI_2);
CGAffineTransform Scale = CGAffineTransformMakeScale(1.0f,1.0f);
CGAffineTransform shrinkWidth = CGAffineTransformMakeScale(1, 1);
CGAffineTransform finalTransform = CGAffineTransformConcat(shrinkWidth, CGAffineTransformConcat(translateToCenter, Rotate) );
[firstLayerInstruction setTransform:finalTransform atTime:kCMTimeZero];
//[firstLayerInstruction setTransform:CGAffineTransformConcat(Rotate,Scale) atTime:kCMTimeZero];
//[firstLayerInstruction setTransform:Scale atTime:kCMTimeZero];
mainInstruction.layerInstructions = [NSArray arrayWithObject:firstLayerInstruction];
AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];
mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];
mainCompositionInst.frameDuration = CMTimeMake(1,30);
mainCompositionInst.renderSize = CGSizeMake(480, 360);
//AVPlayerItem *newPlayerItem = [AVPlayerItem playerItemWithAsset:mixComposition];
//newPlayerItem.videoComposition = mainCompositionInst;
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];
exportSession.outputURL = [NSURL fileURLWithPath:tempURL3];
exportSession.videoComposition = mainCompositionInst;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^
{
switch (exportSession.status)
{
case AVAssetExportSessionStatusFailed:
{
NSLog (@"FAIL");
break;
}
case AVAssetExportSessionStatusCompleted:
{
NSLog (@"SUCCESS");
break;
}
case AVAssetExportSessionStatusCancelled:
{
NSLog (@"CANCELED");
break;
}
};
}];
}
它不起作用,并输出一个不可读的.mov文件。我想知道是否有人能指出我更容易的方向,或者让我了解更改/缩放视频的最佳方式。
更新:将视频保存到文件transformed.mov后,我尝试播放它,并在QuickTime中收到“电影的文件格式无法识别”错误。任何腐败的可能来源?
更新2:我已成功设置导出会话和合成以编写转换。现在我只需要进行特定的转换,即可将480x640转换为480x360。有帮助吗?