我正在编写一个使用AVFoundation处理视频的应用程序。
我的应用程序的行为很简单:我从摄像机上获取一个视频,然后创建一个带有一些音频轨道的AVMutableComposition。使用混合组合,我初始化了一个AVAssetExportSession,它将视频文件存储在我的应用程序的文档目录中。
直到这一点,一切都好:我的视频被存储,我可以在另一个控制器播放它。如果我把刚才存储在文档文件夹中的视频进行一些编辑(就像第一次AVmutableComposition,AVAssetExportSession那样),那就好了。
但是,当我第三次这样编辑视频时,AVAssetExportSession状态就变成了“失败”,并且有了这个错误:
"Domain=AVFoundationErrorDomain Code=-11820 "Cannot Complete Export" UserInfo=0x1a9260 {NSLocalizedRecoverySuggestion=Try exporting again., NSLocalizedDescription=Cannot Complete Export}"
我已经读过,这是一个一般的错误,其中会话无法导出。这是什么意思?为什么我只做了第三次编辑过程?会不会是内存管理错误?一只虫子?这是我的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;
}
};
}];我在网络上做了很多搜索,有些人在会话的outputURL中遇到了问题,但是我尝试过了,而且在我的代码中似乎一切正常。若要为文件分配唯一名称,请使用NSDate。为了调试目的,我尝试恢复一个标准字符串名,但问题仍然存在。有什么想法吗?有人能给我建议一种替代的方法,将AssetWriter嵌入AVassetExportSession的资产导出到documents文件夹中吗?
发布于 2013-03-20 14:35:29
问题是_assetExport.outputFileType --您已经设置了AVFileTypeQuickTimeMovie类型。不可能被支持的类型。
尝试使用以下代码找出_assetExport支持的输出文件类型,并使用合适的输出文件类型。
NSLog (@"created exporter. supportedFileTypes: %@", exporter.supportedFileTypes);或
只需更改
_assetExport.outputFileType = AVFileTypeQuickTimeMovie;至
exporter.outputFileType = @"com.apple.m4a-audio";此外,请不要忘记将扩展更改为
NSString* ext = @".MOV"; to @".m4a" 这应该能行。对我起作用了。
https://stackoverflow.com/questions/5624150
复制相似问题