Cri*_*682 11 iphone avfoundation ios
我正在编写一个使用AVFoundation处理视频的应用程序.
我的应用程序的行为很简单:我从相机胶卷拍摄视频,然后创建一个带有一些音轨的AVMutableComposition.使用混合合成我初始化AVAssetExportSession,将视频文件存储在我的应用程序的文档目录中.
在此之前,一切都没问题:我的视频已存储,我可以在另一个控制器中播放.如果我将刚刚存储在我的文档文件夹中的视频进行一些编辑(与第一次AVmutableComposition,AVAssetExportSession相同)再次没问题.
但是我第三次执行此过程来编辑视频,AVAssetExportSession状态变为"Fail"并出现此错误:
"Domain=AVFoundationErrorDomain Code=-11820 "Cannot Complete Export" UserInfo=0x1a9260 {NSLocalizedRecoverySuggestion=Try exporting again., NSLocalizedDescription=Cannot Complete Export}"
我读过这是一个无法导出会话的一般错误.这是什么意思?为什么我第三次进行编辑过程?这可能是内存管理错误吗?一个bug?这是我的AVAssetExportSession的代码:
_assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
_assetExport.shouldOptimizeForNetworkUse = YES;
///data odierna
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"ddMMyyyyHHmmss"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
[now release];
[format release];
NSString* ext = @".MOV";
NSString* videoName=[NSString stringWithFormat:@"%@%@", dateString, ext];
///data odierna
NSString *exportPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:videoName];
if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
{
[[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
}
_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
[_assetExport setTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)];
NSURL *exportUrl = [NSURL fileURLWithPath:exportPath] ;
_assetExport.outputURL = exportUrl ;
[_assetExport exportAsynchronouslyWithCompletionHandler:^
{
switch (_assetExport.status)
{
case AVAssetExportSessionStatusFailed:
{
NSLog (@"FAIL %@",_assetExport.error);
if ([[NSFileManager defaultManager] fileExistsAtPath:[_assetExport.outputURL path]])
{
[[NSFileManager defaultManager] removeItemAtPath:[_assetExport.outputURL path] error:nil];
}
[self performSelectorOnMainThread:@selector (ritenta)
withObject:nil
waitUntilDone:NO];
break;
}
case AVAssetExportSessionStatusCompleted:
{
NSLog (@"SUCCESS");
[self performSelectorOnMainThread:@selector (saveVideoToAlbum:)
withObject:exportPath
waitUntilDone:NO];
break;
}
case AVAssetExportSessionStatusCancelled:
{
NSLog (@"CANCELED");
break;
}
};
}];
Run Code Online (Sandbox Code Playgroud)
我在网上做了很多搜索,有些人在会话的outputURL中遇到了问题,但是我已经尝试了,似乎在我的代码中都可以.要为文件指定唯一的名称,我使用NSDate.出于调试目的,我尝试恢复标准字符串名称,但问题仍然存在.有任何想法吗?有人可以向我建议一种替代方法,导出到文档文件夹,资产与AssetWriter insted AVassetExportSession?
Kri*_*hna -1
问题是_assetExport.outputFileType你设置了类型AVFileTypeQuickTimeMovie。这不太可能是受支持的类型。
尝试使用以下代码找出 _assetExport 支持哪些输出文件类型,并使用合适的类型。
NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);
Run Code Online (Sandbox Code Playgroud)
或者
只是改变
_assetExport.outputFileType = AVFileTypeQuickTimeMovie;
Run Code Online (Sandbox Code Playgroud)
到
exporter.outputFileType = @"com.apple.m4a-audio";
Run Code Online (Sandbox Code Playgroud)
另外不要忘记更改扩展名
NSString* ext = @".MOV"; to @".m4a"
Run Code Online (Sandbox Code Playgroud)
这应该有效。这对我有用。