gpt4 book ai didi

ios - 使用 AVFoundation 获得裁剪视频的意外结果

转载 作者:行者123 更新时间:2023-12-01 16:15:29 26 4
gpt4 key购买 nike

我有 1920*1080(16:9) 的视频,我想按 1080*1080(1:1) 的中间像素裁剪该视频。
为此我写了下面的代码。

-(void)cropVideo
{
AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *track = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *track1 = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
NSString *path = [NSString stringWithFormat:@"%@/staticVideo.mp4", [[NSBundle mainBundle] resourcePath]];
AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:path]];

[track insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:CMTimeMake(0, 1) error:nil];
[track1 insertTimeRange:CMTimeRangeMake(kCMTimeZero, asset.duration) ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:CMTimeMake(0, 1) error:nil];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat:@"%@1x1.mov",NSBundle.mainBundle.infoDictionary[@"CFBundleExecutable"]]];
[[NSFileManager defaultManager] removeItemAtPath:myPathDocs error:nil];
NSURL *url = [NSURL fileURLWithPath:myPathDocs];

/** code for crop video*/

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);

AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:track];

[layerInstruction setCropRectangle:CGRectMake(420, 0, 1080, 1080) atTime:kCMTimeZero];

[layerInstruction setOpacity:0.0 atTime:asset.duration];

instruction.layerInstructions = [NSArray arrayWithObjects:layerInstruction,nil];

AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];

mainCompositionInst.renderSize = CGSizeMake(1080, 1080);
mainCompositionInst.instructions = [NSArray arrayWithObject:instruction];
mainCompositionInst.frameDuration = CMTimeMake(1, 30);

/** code for crop video*/

AVAssetExportSession *exporter;
int currentSize = (int) [[NSUserDefaults standardUserDefaults] integerForKey:@"VideoSize"];
if (currentSize == 1)
{
exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset640x480];
}
else if (currentSize == 2)
{
exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset1280x720];
}
else if (currentSize == 3)
{
exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset1920x1080];
}
else if (currentSize == 4)
{
exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset3840x2160];
}
exporter.videoComposition = mainCompositionInst;
exporter.outputURL=url;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;

[exporter exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
/*update Video*/
});
}];

}

抱歉,我无法解释我想要的方式和获得的方式。
所以我画了一张图,你可以理解我的问题。

diagram

像这张图一样,我想要 1080*1080 的 A 结果,但我得到的是 1080*1080 的 B 结果。这是结果 B 的显示截图。

ScreenShot of B result

请帮我解决这个问题。提前致谢。

最佳答案


只需降低视频的 x 值即可。

CGAffineTransform t1 = CGAffineTransformMakeTranslation(-420, 0);
[layerInstruction setTransform:t1 atTime:kCMTimeZero];

解决了我的问题...

关于ios - 使用 AVFoundation 获得裁剪视频的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33781480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com