Not getting the combined video even after I got AVAssetExportSessionStatusCompleted status, target c

I was unable to get the video in the device even after getting the AVAssetExportSessionStatusCompleted status. But I was able to get the video when used in the Xcode simulator.

AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init];
AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                    preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                    preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime insertTime = kCMTimeZero;

for(i=0;i<videoForMerging.count;i++) {
    AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",videoForMerging[i]]]];
    CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);

    [videoTrack insertTimeRange:timeRange
                        ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                         atTime:insertTime
                          error:nil];

    [audioTrack insertTimeRange:timeRange
                        ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                         atTime:insertTime
                          error:nil];

    insertTime = CMTimeAdd(insertTime,asset.duration);

}
NSString* documentsDirectory= [self applicationDocumentsDirectory];
NSString *myDocumentPath= [documentsDirectory stringByAppendingPathComponent:@"merge_video.mp4"];
NSURL *urlVideoMain = [[NSURL alloc] initFileURLWithPath: myDocumentPath];

if([[NSFileManager defaultManager] fileExistsAtPath:myDocumentPath])
{
    [[NSFileManager defaultManager] removeItemAtPath:myDocumentPath error:nil];
}

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = urlVideoMain;
exporter.outputFileType = @"com.apple.quicktime-movie";
exporter.shouldOptimizeForNetworkUse = YES;

[exporter exportAsynchronouslyWithCompletionHandler:^{

    switch ([exporter status])
    {
        case AVAssetExportSessionStatusFailed:
            break;

        case AVAssetExportSessionStatusCancelled:
            break;

        case AVAssetExportSessionStatusCompleted:
            [Util okAlert:@"Video save successfull" viewController:self];
            break;

        default:
            break;
    }
}];

      

}

Document catalog:

-(NSString*) applicationDocumentsDirectory { NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; return basePath; }

      

+3


source to share


1 answer


AVMutableComposition * mixComposition = [[AVMutableComposition alloc] init];



AVMutableCompositionTrack *videoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                    preferredTrackID:kCMPersistentTrackID_Invalid];
AVMutableCompositionTrack *audioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                    preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime insertTime = kCMTimeZero;

for(i = 0; i < videoForMerging.count; i++) {
    AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",videoForMerging[i]]]];
    CMTimeRange timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);

    [videoTrack insertTimeRange:timeRange
                        ofTrack:[[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                         atTime:insertTime
                          error:nil];

    [audioTrack insertTimeRange:timeRange
                        ofTrack:[[asset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0]
                         atTime:insertTime
                          error:nil];

    insertTime = CMTimeAdd(insertTime,asset.duration);

    // Grab the source track from AVURLAsset for example.
    AVAssetTrack *assetVideoTrack = [asset tracksWithMediaType:AVMediaTypeVideo].lastObject;

    // Grab the composition video track from AVMutableComposition you already made.
    AVMutableCompositionTrack *compositionVideoTrack = [mixComposition tracksWithMediaType:AVMediaTypeVideo].lastObject;

    // Apply the original transform.
    if (assetVideoTrack && compositionVideoTrack) {
        [compositionVideoTrack setPreferredTransform:assetVideoTrack.preferredTransform];
    }
}

NSString* documentsDirectory= [self applicationDocumentsDirectory];
NSString *myDocumentPath= [documentsDirectory stringByAppendingPathComponent:@"merge_video.mp4"];
NSURL *urlVideoMain = [[NSURL alloc] initFileURLWithPath: myDocumentPath];

if([[NSFileManager defaultManager] fileExistsAtPath:myDocumentPath])
{
    [[NSFileManager defaultManager] removeItemAtPath:myDocumentPath error:nil];
}

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetHighestQuality];
exporter.outputURL = urlVideoMain;
exporter.outputFileType = AVFileTypeMPEG4;
exporter.shouldOptimizeForNetworkUse = YES;

[exporter exportAsynchronouslyWithCompletionHandler:^{

    switch ([exporter status]) {
        case AVAssetExportSessionStatusFailed: {

            break;
        }
        case AVAssetExportSessionStatusCancelled: {

            break;
        }
        case AVAssetExportSessionStatusCompleted:  {
            [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:self.videoURL];

    NSLog(@"%@", changeRequest.description);
} completionHandler:^(BOOL success, NSError *error) {
    if (success) {
        NSLog(@"saved down");
        [[NSFileManager defaultManager] removeItemAtURL:self.videoURL error:nil];

    } else {
        NSLog(@"something wrong %@", error.localizedDescription);
        [[NSFileManager defaultManager] removeItemAtURL:self.videoURL error:nil];
    }
}];
            break;
        }
        default:
            break;
    }
}];

      

+1


source







All Articles