gpt4 book ai didi

ios - AVAssetExportSession 视频未保存到相机卷轴

转载 作者:行者123 更新时间:2023-11-29 10:48:55 27 4
gpt4 key购买 nike

我正在尝试在应用程序中裁剪和合并多个视频。我在步骤的最后部分遇到问题,我需要将视频保存到相机卷轴并且 UIVideoAtPathIsCompatibleWithSavedPhotosAlbum 返回 NO。知道出了什么问题吗?

- (void)video {
self.shouldRotate = NO;
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];

[self presentViewController:imagePicker animated:YES completion:nil];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
NSString* moviePath = nil;
if (CFStringCompare ((__bridge CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
// NSLog(@"%@",moviePath);
//NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL];

}

NSURL* sourceMovieURL = [NSURL fileURLWithPath:moviePath];
NSURL* outputPath = [sourceMovieURL URLByDeletingPathExtension];
outputPath = [outputPath URLByDeletingLastPathComponent];
NSMutableString* last_path_component =
[[NSMutableString alloc] initWithString:[[sourceMovieURL URLByDeletingPathExtension] lastPathComponent]];
[last_path_component appendString:@"~SQUARE"];
outputPath = [outputPath URLByAppendingPathComponent:last_path_component];
outputPath = [outputPath URLByAppendingPathExtension:@"MOV"];
AVURLAsset* asset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];

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(clipVideoTrack.naturalSize.height, clipVideoTrack.naturalSize.height);
videoComposition.frameDuration = CMTimeMake(1, 30);

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeMakeWithSeconds(60, 30) );

// 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];

// export


AVAssetExportSession* exporter = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality] ;
exporter.videoComposition = videoComposition;
exporter.outputURL=outputPath;
exporter.outputFileType=AVFileTypeQuickTimeMovie;

VideoButtonView* button = [self.scrollView buttonAdded];
[exporter exportAsynchronouslyWithCompletionHandler:^(void){
AVURLAsset* new_asset = [AVURLAsset URLAssetWithURL:outputPath options:nil];
[button addVideoAsset:new_asset];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (outputPath.absoluteString)) {
UISaveVideoAtPathToSavedPhotosAlbum (outputPath.absoluteString, self, nil, nil);
}

}];

[self dismissViewControllerAnimated:YES completion:nil];
//[picker release];
}

最佳答案

我解决了。使用 assetlibrary 似乎解决了这个问题。

[exporter exportAsynchronouslyWithCompletionHandler:^{
BOOL success = false;
switch ([exporter status]) {
case AVAssetExportSessionStatusCompleted:
success = true;
NSLog(@"Export Completed");
break;
case AVAssetExportSessionStatusWaiting:
NSLog(@"Export Waiting");
break;
case AVAssetExportSessionStatusExporting:
NSLog(@"Export Exporting");
break;
case AVAssetExportSessionStatusFailed:
{
NSError *error = [exporter error];
NSLog(@"Export failed: %@", [error localizedDescription]);

break;
}
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");

break;
default:
break;
}

if (success == true) {

ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary writeVideoAtPathToSavedPhotosAlbum:url completionBlock:^(NSURL *assetURL, NSError *error){
NSError *removeError = nil;
[[NSFileManager defaultManager] removeItemAtURL:url error:&removeError];
}];

}
}];

关于ios - AVAssetExportSession 视频未保存到相机卷轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21275239/

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