5

i'm developing an app that export audio file stored in the iPod library, but i should verify the file size before export and upload (server has a fixed max upload size):

  • i know that after exporting file, the size will not be the same: is there any method to estimate the new size ?
  • main question : can i know the original file size before export (using MPMediaItem or something else), so i can tell user that this file can't be uploaded (the export can take a while). thanks.
Red Mak
  • 1,176
  • 2
  • 25
  • 56

2 Answers2

2

I have not tested but AVAssetExportSession can help you with this.

MPMediaItem *curItem = musicPlayer.nowPlayingItem;

NSURL *url = [curItem valueForProperty: MPMediaItemPropertyAssetURL];

AVURLAsset *songAsset = [AVURLAsset URLAssetWithURL: url options:nil];

AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset
                                   presetName: AVAssetExportPresetPassthrough];

exporter.estimatedOutputFileLength will give you size in bytes.

cjd
  • 549
  • 3
  • 11
  • Yes it work thanks :), but we need to add a timeRange to the exporter : AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset: songAsset presetName: AVAssetExportPresetAppleM4A]; CMTime half = CMTimeMultiplyByFloat64(exporter.asset.duration, 1); exporter.timeRange = CMTimeRangeMake(kCMTimeZero, half); long long size = exporter.estimatedOutputFileLength; – Red Mak Jan 06 '14 at 14:56
  • can't give you the +50 ! the link doesn't seem to work, how should i do ? – Red Mak Jan 06 '14 at 15:03
  • Which link are you asking about? – cjd Jan 07 '14 at 04:18
  • You cannot get the estimatedOutputFileLength with AVAssetExportPresetPassthrough and you also need timeRange if using a different preset – Ryan Heitner Dec 01 '14 at 18:29
  • I have exactly the same question. estimatedOutputFileLength returns 0 in my case. Any ideas ? I am using swift 4 – Awais Fayyaz Feb 08 '19 at 09:18
  • The documentation says this returns 0 when the export preset is `AVAssetExportPresetPassthrough`, and it's deprecated in iOS 13 – Jordan H May 23 '21 at 01:55
-2

I'm not sure, but i think you could use:

MPMediaItem *item = [[collection items] objectAtIndex:0];
NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(@"length %d",[data length]);

Regards,

Mariano

Mariano
  • 330
  • 1
  • 4
  • 13