更新:奇怪的是,此代码在iPad 2上运行良好,但不适用于iPad第4代。
更新#2:如果我将presetName:AVAssetExportPresetHighestQuality
更改为presetName:AVAssetExportPresetPassThrough
,视频会成功导出但我无法在设备中播放。如果我通过xCode的组织者将应用程序包拉到我的计算机上,我就可以玩它。同样,这个问题只发生在iPad 4上,而不是iPad 2,64位模拟器,视网膜模拟器或1x模拟器。
我正在使用AVExportSession
混合一些音频和视频。它在模拟器和iPad 2上工作得相当愉快,但不是iPad第4代。导出会话会出现-11820
错误(AVErrorExportFailed
),但这是我可以从中获得的有用信息的范围。处理。源文件存在,其他一切都在游泳,但不是AVExportSession
。
你能帮助我让它在设备上工作吗?
对方法的冗长表示道歉。
-(NSURL*)bindAudioAndVideo:(NSString*)audioFileName videoFileName:(NSString*)videoFileName
{
//documents folder
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsFolder = [[NSString alloc] initWithString:[paths objectAtIndex:0]]; //Get the docs directory
AVMutableComposition* mixComposition = [AVMutableComposition composition];
NSString* audio_inputFileName = audioFileName;
NSString* audio_inputFilePath = [documentsFolder stringByAppendingPathComponent:audio_inputFileName];
NSURL* audio_inputFileUrl = [NSURL fileURLWithPath:audio_inputFilePath];
NSString* video_inputFileName = videoFileName;
NSString* video_inputFilePath = [documentsFolder stringByAppendingPathComponent:video_inputFileName];
NSURL* video_inputFileUrl = [NSURL fileURLWithPath:video_inputFilePath];
NSString* outputFileName = @"outputFile.mp4";
NSString* outputFilePath = [documentsFolder stringByAppendingPathComponent:outputFileName];
NSURL* outputFileUrl = [NSURL fileURLWithPath:outputFilePath];
//Check files actually exist before beginning (they do)
AVMutableComposition* mixComposition = [AVMutableComposition composition];
CMTime nextClipStartTime = kCMTimeZero;
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil];
CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);
AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];
AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];
AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
_assetExport.outputFileType = @"com.apple.quicktime-movie";
_assetExport.outputURL = outputFileUrl;
[_assetExport exportAsynchronouslyWithCompletionHandler:
^(void ) {
[self addSkipBackupAttributeToItemAtURL:outputFileUrl];
NSLog(@"Completed. Tidy time.");
switch ([_assetExport status]) {
case AVAssetExportSessionStatusCompleted:
NSLog(@"Export Completed");
break;
case AVAssetExportSessionStatusFailed:
NSLog(@"Export failed: %@", [[_assetExport error] localizedDescription]);
NSLog (@"FAIL %@",_assetExport.error); //-11820! I AM A USELESS ERROR CODE
NSLog (@"supportedFileTypes: %@", _assetExport.supportedFileTypes);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export cancelled");
break;
default:
break;
}
NSTimer *refreshTimer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(exportCompleteRefreshView) userInfo:Nil repeats:NO];
//Throw back to main thread unuless you want really long delays for no reason.
[[NSRunLoop mainRunLoop] addTimer:refreshTimer forMode:NSRunLoopCommonModes];
}
];
return outputFileUrl;
}
答案 0 :(得分:1)
如果问题与视网膜iPad有关 - 它与设备分辨率有关,因为某些原因模拟器没有模拟。
由于我在设备上创建视频,我在视网膜设备上制作了一个2048x1536视频(非视网膜设备上的视频为1024x768)。显然这只是AVExportSession
要处理的像素太多,或iPad正常播放,所以它只是在播放或导出时向我抛出了各种模糊的错误消息。以点分辨率记录,而不是像素分辨率似乎已经解决了问题。
模拟器似乎是一个红鲱鱼,因为它拥有相对无限的健康mac资源,而不是A6。