Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

avfoundation - AVAsserexportsession failed randomly

I am creating the video from a set of images with AVAssetwriter and AVAssetExportSession and successfully getting the video and save it to the documents. But randomly the video exporting failed and it shows An Unknown error occured with error code -11800.

   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   couldnU2019t be completed. (OSStatus error -12124.)"";

Hi here is my code.. where strExport and strThumb is my path. It works fine but randomly shows errors..

     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];

Can you check this... I struggled for 1 day i don't know where the error came. Thanks..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...