Jonathan Ellis
Jonathan Ellis

Reputation: 5479

AVAssetExportSession estimatedOutputFileLength always returns 0

Having a problem where the estimatedOutputFileLength property of AVAssetExportSession always returns 0 (and returns -9223372036854775808 on the simulator).

I've tried everything to get this to work, trying different outputFileTypes, toggling shouldOptimizeForNetworkUse on and off, specifying (or not specifying) the outputURL... despite all this, nothing seems to work and I'm beginning to think this may be a bug in the SDK.

This is my code:

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetMediumQuality]; // doesn't matter which preset is used
//exportSession.shouldOptimizeForNetworkUse = YES;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
NSLog(@"bytes = %lld", exportSession.estimatedOutputFileLength);

I just can't figure out why this isn't working! (iOS 6, iPhone 5)

Upvotes: 4

Views: 1303

Answers (2)

Ryan Heitner
Ryan Heitner

Reputation: 13652

You need to include the timerange.

How much of the file you intend to export. Without that it will return 0,

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset presetName: AVAssetExportPresetAppleM4A];
exporter.outputFileType = AVFileTypeAppleM4A;

CMTime full = CMTimeMultiplyByFloat64(exporter.asset.duration, 1);
exporter.timeRange = CMTimeRangeMake(kCMTimeZero, full);
long long size = exporter.estimatedOutputFileLength;
fileInfo.fileSize = size;

Upvotes: 1

liuliu
liuliu

Reputation: 731

You can workaround this problem by setting proper timeRange on the exportSession:

exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, asset.duration);

It seems that in iOS, the AVAssetExportSessionInternal.timeRange is not getting sensible result when estimating file length.

Upvotes: 8

Related Questions