gpt4 book ai didi

ios - 在 iOS 中以编程方式将录制的视频旋转 90 度

转载 作者:行者123 更新时间:2023-11-29 00:27:59 24 4
gpt4 key购买 nike

我在 iPhone 中录制了一段视频,并且正在获取其本地文件 URL。有什么方法可以使用 URL 将该视频旋转 90 度吗?

最佳答案

您可以在导出 session 之前使用 AVMutableVideoCompositionLayerInstruction 类旋转视频,并且可以对其应用 Transform

这就是方法

 [yourlayerInstruction setTransform:CGAffineTransformMakeRotation(M_PI/2) atTime:firstAssets.duration];

这是完整的实现

          AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];

AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
preferredTrackID:kCMPersistentTrackID_Invalid];


AVMutableVideoCompositionInstruction * mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeAdd(firstAsset.duration, secondAsset.duration));

AVMutableVideoCompositionLayerInstruction *firstlayerInstruction = [AVMutableVideoCompositionLayerInstruction
videoCompositionLayerInstructionWithAssetTrack:firstTrack];

// set Trasform Here
[firstlayerInstruction setTransform:CGAffineTransformMakeRotation(M_PI/2) atTime:kCMTimeZero];

[firstlayerInstruction setOpacity:0.0 atTime:firstAsset.duration];

mainInstruction.layerInstructions = [NSArray arrayWithObjects:firstlayerInstruction,nil];;

AVMutableVideoComposition *mainCompositionInst = [AVMutableVideoComposition videoComposition];

mainCompositionInst.instructions = [NSArray arrayWithObject:mainInstruction];
mainCompositionInst.frameDuration = CMTimeMake(1, 30);
mainCompositionInst.renderSize = CGSizeMake(320.0, 480.0);

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition
presetName:AVAssetExportPreset640x480];
exporter.outputURL=url;
exporter.videoComposition=mainCompositionInst;
exporter.outputFileType = AVFileTypeQuickTimeMovie;
exporter.shouldOptimizeForNetworkUse = YES;
[exporter exportAsynchronouslyWithCompletionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^
{
[self exportDidFinish:exporter];
});
}];

希望这对你有帮助

关于ios - 在 iOS 中以编程方式将录制的视频旋转 90 度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42549369/

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