Exporting an AVAsset in iOS


// First thing is you need a AVAsset.
// Once you have an asset, you need to determine the type of preset you need to use.
// You can consider a preset as a set of properties, which is provided to the exporter. 
// I do this for helping me with my export.

-(NSString*)determineCompatibleExportPresetForAsset:(AVAsset*)asset
{
    NSArray<NSString*>* presetsRankedByPriority = @[AVAssetExportPresetHighestQuality, AVAssetExportPresetMediumQuality, AVAssetExportPresetLowQuality];
    NSArray* compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];
    for (NSString* preset in presetsRankedByPriority)
    {
        if([compatiblePresets containsObject:preset])
        {
            return preset;
        }
    }
    return nil;
}

// after getting the preset, use this call
AVAssetExportSession* exportSession = [AVAssetExportSession exportSessionWithAsset:asset presetName:preset];
exportSession.outputFileType = UTI;
exportSession.outputURL = whereToSaveTheFile;

if (!exportSession)
{
    callback([NSError errorWithDomain:kManagerErrorDomain
                                 code:kDownloadFailed
                             userInfo:nil]);
}
else
{
  [exportSession exportAsynchronouslyWithCompletionHandler: ^{
    // handle error here
  }];

Leave a Reply

Your email address will not be published. Required fields are marked *