Search code examples
objective-cavfoundationexporter

Videoclip export error: 'Invalid output file type' in objective-c


I am trying to trim an existing videoclip and re-save the clip in the same location as the original file. However when I run my app I get this error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid output file type'

I have found recommendations but they require me to change the outputfiletype from AVMediaTypeVideo. I would like to keep AVMediaTypeVideo because this is what the original video file is saved as.

This is what I have so far:

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

NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];

NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];

AVURLAsset *videoclip = [AVURLAsset URLAssetWithURL:outputURL options:nil];

AVMutableCompositionTrack *finalClipTrack = [finalClip addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

[finalClipTrack insertTimeRange:CMTimeRangeMake(CMTimeMake((duration*indexNum), 1), CMTimeMake(duration,1)) ofTrack:[[videoclip tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil];

NSString *outputPathwe = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"outputwe.mov"];

NSURL *outputURLwe = [[NSURL alloc] initFileURLWithPath:outputPathwe];

if ([[NSFileManager defaultManager] fileExistsAtPath:outputPathwe])
    [[NSFileManager defaultManager] removeItemAtPath:outputPathwe error:nil];

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:finalClip presetName:AVAssetExportPresetHighestQuality];

exporter.outputFileType = AVMediaTypeVideo;

exporter.outputURL=outputURLwe;

[exporter exportAsynchronouslyWithCompletionHandler:^{

    dispatch_async(dispatch_get_main_queue(), ^{

        [self exportDidFinish:exporter];

    });
}];

I feel like it something really easy that I am just missing. This is my first time using AVFoundation so any help would be appreciated!


Solution

  • AVMediaTypeVideo is a "media type" not an "output file type". Your original video has tracks that are of type AVMediaTypeVideo. The original video is not of type AVMediaTypeVideo.

    The outputFileType of AVAssetExportSession are constants of type NSString. The allowed values are listed in AVFoundation/AVMediaFormat.h. For video, they are:

    • AVFileTypeQuickTimeMovie
    • AVFileTypeMPEG4
    • AVFileTypeAppleM4V

    You must choose one of the allowed values to use for your AVAssetExportSession's outputFileType.