6

I was wondering how to make a time range for AVAssetExportSession from time stamps such as:

NSTimeInterval start = [[NSDate date] timeIntervalSince1970];
NSTimeInterval end = [[NSDate date] timeIntervalSince1970];

The code that I am using for my export session is as follows:

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetHighestQuality];

exportSession.outputURL = videoURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
exportSession.timeRange = CMTimeRangeFromTimeToTime(start, end);

Thanks for your help!

djromero
  • 19,551
  • 4
  • 71
  • 68
user1273431
  • 73
  • 1
  • 6

1 Answers1

14

The property timeRange in AVAssetExportSession allows you to do a partial export of an asset specifying where to start and which duration. If not specified it'll export the whole video, in other words, it'll start at zero and will export the total duration.

Both start and duration should be expressed as CMTime.

For instance, if you want to export the first half of the asset:

CMTime half = CMTimeMultiplyByFloat64(exportSession.asset.duration, 0.5);
exportSession.timeRange = CMTimeRangeMake(kCMTimeZero, half);

or the second half:

exportSession.timeRange = CMTimeRangeMake(half, half);

or 10 seconds at the end:

CMTime _10 = CMTimeMakeWithSeconds(10, 600);
CMTime tMinus10 = CMTimeSubtract(exportSession.asset.duration, _10);
exportSession.timeRange = CMTimeRangeMake(tMinus10, _10);

Check CMTime reference for other ways to calculate the exact timing you need.

djromero
  • 19,551
  • 4
  • 71
  • 68
  • Thanks a lot for that! All sorted. Only thing is that CMTimeMakeWithSeconds needs to have two arguments. So the second argument just has to be the preferred time scale. – user1273431 May 27 '12 at 12:21
  • Opps, fixed. This happens to write code in a text area w/out real-time syntax checking :) – djromero May 27 '12 at 16:12