【发布时间】:2019-10-16 12:21:07
【问题描述】:
在运行 iOS 13 的设备上,[exportSession exportAsynchronouslyWithCompletionHandler: 在将 .MOV 视频转换为 mp4 时总是失败并显示“操作无法完成”消息。但是,相同的代码在 13 即 12 之前的 iOS 上运行良好。我粘贴在我的完整方法下方
- (void)encodeVideo:(NSString *)videoURL
{
// Create the asset url with the video file
AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:videoURL] options:nil];
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
// Check if video is supported for conversion or not
if ([compatiblePresets containsObject: AVAssetExportPresetLowQuality])
{
//Create Export session
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetLowQuality];
//Creating temp path to save the converted video
NSString* documentsDirectory= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* myDocumentPath= [documentsDirectory stringByAppendingPathComponent:@"temp.mp4"];
NSURL *url = [[NSURL alloc] initFileURLWithPath:myDocumentPath];
//Check if the file already exists then remove the previous file
if ([[NSFileManager defaultManager]fileExistsAtPath:myDocumentPath])
{
[[NSFileManager defaultManager]removeItemAtPath:myDocumentPath error:nil];
}
exportSession.outputURL = url;
//set the output file format if you want to make it in other file format (ex .3gp)
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.shouldOptimizeForNetworkUse = YES;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
switch ([exportSession status])
{
case AVAssetExportSessionStatusFailed:
NSLog(@"Export session failed");
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
case AVAssetExportSessionStatusCompleted:
{
//Video conversion finished
NSLog(@"Successful!");
}
break;
default:
break;
}
}];
}
else
{
NSLog(@"Video file not supported!");
}
}
【问题讨论】:
-
一直在那里尝试不同的参数。当我将 AVAssetExportPresetLowQuality 置于 if 条件下时,它没有任何区别。
-
你试过这个解决方案了吗? stackoverflow.com/a/58278117/3241041
-
@alxlives 是的,我之前已经尝试过这个解决方案,但在我的情况下它不起作用。不过还是谢谢你的提及。
标签: ios objective-c ios13 format-conversion