AVAsserexportsession随机失败

时间:2013-12-19 12:09:01

标签: avfoundation

我使用AVAssetwriter和AVAssetExportSession从一组图像创建视频,并成功获取视频并将其保存到文档中。但随机视频导出失败,并显示错误代码-11800出现An Unknown错误。

   ExportSessionError: -11800 adn userinfo{
   NSLocalizedDescription = "The operation could not be completed";
   NSLocalizedFailureReason = "An unknown error occurred (-12124)";
   NSUnderlyingError = "Error Domain=NSOSStatusErrorDomain Code=-12124 \"The operation   couldn\U2019t be completed. (OSStatus error -12124.)\"";

嗨,这是我的代码..其中strExport和strThumb是我的路径。它工作正常但随机显示错误..

     NSURL *soundFileURL = [NSURL fileURLWithPath:[[self pathToDocumentsDirectory] stringByAppendingPathComponent:_PATH_TMP_AUDIO_]];
   NSURL *videoFileURL = [NSURL fileURLWithPath:[[self pathToDocumentsDirectory] stringByAppendingPathComponent:@"screen.mov"]];

AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:soundFileURL options:nil];
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:videoFileURL options:nil];

AVMutableComposition* mixComposition = [AVMutableComposition composition];
AVMutableCompositionTrack *compositionCommentaryTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                                    preferredTrackID:kCMPersistentTrackID_Invalid];
if ([[videoAsset tracksWithMediaType:AVMediaTypeVideo]count]>0) {
    [compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, audioAsset.duration)
                                        ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                                         atTime:kCMTimeZero error:nil];    }


AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                               preferredTrackID:kCMPersistentTrackID_Invalid];
if ([[videoAsset tracksWithMediaType:AVMediaTypeVideo]count]>0) {
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                                   ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                                    atTime:kCMTimeZero error:nil];
}


AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition  presetName:AVAssetExportPresetPassthrough];

UIImage *image = nil;

AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:videoAsset];
imageGenerator.appliesPreferredTrackTransform = YES;

// calc midpoint time of video
Float64 durationSeconds = CMTimeGetSeconds([videoAsset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);

// get the image from
NSError *error = nil;
CMTime actualTime;
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];

if (halfWayImage != NULL)
{
    // cgimage to uiimage
    size_t width = CGImageGetWidth(halfWayImage);
    size_t height = CGImageGetHeight(halfWayImage);
    CGRect rect;
    CGSize size;
    if (width>height) {
        size=CGSizeMake(width/(height/thumb_size), thumb_size);
        rect=CGRectMake((size.width-thumb_size)/2.0, 0, thumb_size, thumb_size);
    }
    else{
        size=CGSizeMake(thumb_size,height/(width/thumb_size));
        rect=CGRectMake(0, (size.height-thumb_size)/2.0, thumb_size, thumb_size);
    }

    UIImage *scaleImage = [UIImage imageWithCGImage:halfWayImage];
    NSData *imgData = UIImageJPEGRepresentation(scaleImage, 1.0);
    [imgData writeToFile:self.strThumb atomically:YES];
    CGImageRelease(halfWayImage);
}
NSLog(@"exprt url %@ and thumb %@",self.strExport,self.strThumb);

NSURL    *exportUrl = [NSURL fileURLWithPath:self.strExport];
if ([[NSFileManager defaultManager] fileExistsAtPath:self.strExport])
    [[NSFileManager defaultManager] removeItemAtPath:self.strExport error:nil];

_assetExport.outputFileType = @"com.apple.quicktime-movie";

NSLog(@"file type %@", _assetExport.outputFileType);

_assetExport.outputURL = exportUrl;
NSLog(@"expoert urk %@",_assetExport.outputURL);
_assetExport.shouldOptimizeForNetworkUse = YES;

[_assetExport exportAsynchronouslyWithCompletionHandler: ^(void )
 {
     // your completion code here
     switch (_assetExport.status)
     {
         case AVAssetExportSessionStatusCompleted:
         {
             NSLog(@"Export Complete");
             [self performSelectorInBackground:@selector(loadText:) withObject:@"sucess"];

             break;
         }
         case AVAssetExportSessionStatusFailed:
             NSLog(@"Export Failed");
             NSLog(@"ExportSessionError: %d adn userinfo%@", [_assetExport.error code],[_assetExport.error userInfo]);
             [self performSelectorInBackground:@selector(loadText:) withObject:nil];
             break;
         case AVAssetExportSessionStatusCancelled:
             NSLog(@"Export canceled");
             NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]);
             break;
     }

 }];
[audioAsset release];
[videoAsset release];
[image release];

你能检查一下......我挣扎了一天我不知道错误发生在哪里。谢谢..

0 个答案:

没有答案