gpt4 book ai didi

iphone - 如何更正 objective-c 中的视频方向

转载 作者:搜寻专家 更新时间:2023-10-30 20:02:38 25 4
gpt4 key购买 nike

我正在使用 UIImagePickerController 在 iPhone 中捕捉视频,但视频显示向右旋转了 90 度。

如何解决这个问题

有什么方法可以纠正方向。

最佳答案

您需要使用 AVExportSession 对视频进行重新编码,以将视频旋转到正确的方向,如所说 Oleksiy Ivanov .所以你需要这样的东西:

NSError *error = nil;
AVURLAsset *videoAssetURL = [[AVURLAsset alloc] initWithURL:self.videoUrl options:nil];

AVMutableComposition *composition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];

AVAssetTrack *videoTrack = [[videoAssetURL tracksWithMediaType:AVMediaTypeVideo] firstObject];
AVAssetTrack *audioTrack = [[videoAssetURL tracksWithMediaType:AVMediaTypeAudio] firstObject];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAssetURL.duration) ofTrack:videoTrack atTime:kCMTimeZero error:&error];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAssetURL.duration) ofTrack:audioTrack atTime:kCMTimeZero error:&error];

CGAffineTransform transformToApply = videoTrack.preferredTransform;
AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];
[layerInstruction setTransform:transformToApply atTime:kCMTimeZero];
[layerInstruction setOpacity:0.0 atTime:videoAssetURL.duration];

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake( kCMTimeZero, videoAssetURL.duration);
instruction.layerInstructions = @[layerInstruction];

AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.instructions = @[instruction];
videoComposition.frameDuration = CMTimeMake(1, 30); //select the frames per second
videoComposition.renderScale = 1.0;
videoComposition.renderSize = CGSizeMake(640, 640); //select you video size

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:composition presetName:AVAssetExportPresetMediumQuality];

exportSession.outputURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"videoname.MOV"]];
exportSession.outputFileType = AVFileTypeMPEG4; //very important select you video format (AVFileTypeQuickTimeMovie, AVFileTypeMPEG4, etc...)
exportSession.videoComposition = videoComposition;
exportSession.shouldOptimizeForNetworkUse = NO;
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, videoAssetURL.duration);

[exportSession exportAsynchronouslyWithCompletionHandler:^{

switch ([exportSession status]) {

case AVAssetExportSessionStatusCompleted: {

NSLog(@"Triming Completed");

//generate video thumbnail
self.videoUrl = exportSession.outputURL;
AVURLAsset *videoAssetURL = [[AVURLAsset alloc] initWithURL:self.videoUrl options:nil];
AVAssetImageGenerator *genrateAsset = [[AVAssetImageGenerator alloc] initWithAsset:videoAssetURL];
genrateAsset.appliesPreferredTrackTransform = YES;
CMTime time = CMTimeMakeWithSeconds(0.0,600);
NSError *error = nil;
CMTime actualTime;

CGImageRef cgImage = [genrateAsset copyCGImageAtTime:time actualTime:&actualTime error:&error];
self.videoImage = [[UIImage alloc] initWithCGImage:cgImage];
CGImageRelease(cgImage);

break;
}
default: {
break;
}
}
}];

只需要为您的视频 url 更改 self.videoUrl,它应该可以正常工作:)

关于iphone - 如何更正 objective-c 中的视频方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20402106/

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