How to find the memory size an exported movie takes on iOS?

240 views Asked by At

I am using AVAssetExportSession to export a movie I pick. How do I get the byte count the exported movie actually takes?

This is a snippet from my code. Notice I do: "exportSession.estimatedOutputFileLength", but it only gives 0. Also, I need an exact size and not an estimated so even if it worked I am not sure this is the right way to do this.

if ([compatiblePresets containsObject:AVAssetExportPresetLowQuality])
{
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]initWithAsset:avAsset presetName:AVAssetExportPresetPassthrough];
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* docsDir = [paths objectAtIndex:0];
    NSString* targetFileName = [[mediaPath.description lastPathComponent] stringByDeletingPathExtension];
    NSString* formattedFileName = [NSString stringWithFormat:@"%@.mp4", targetFileName];
    NSString* videoPath = [NSString stringWithFormat:@"%@/%@", docsDir, formattedFileName];
    exportSession.outputURL = [NSURL fileURLWithPath:videoPath];
    exportSession.outputFileType = AVFileTypeMPEG4;
    exportSession.shouldOptimizeForNetworkUse = NO;

    NSLog(@"file size = %lld", exportSession.estimatedOutputFileLength);}
1

There are 1 answers

0
Ryan Heitner On

Try add before the estimatedOutputFileLength you need to give the range of the media you intend to export

CMTime full = CMTimeMultiplyByFloat64(exportSession.asset.duration, 1);
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, full);

See below Also you cannot use AVAssetExportPresetPassthrough since

/* Indicates the estimated byte size of exported file. Returns zero when export preset is AVAssetExportPresetPassthrough or AVAssetExportPresetAppleProRes422LPCM. This property will also return zero if a numeric value (ie. not invalid, indefinite, or infinite) for the timeRange property has not been set. */