SDAVAssetExportSession.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // SDAVAssetExportSession.h
  3. //
  4. // This file is part of the SDAVAssetExportSession package.
  5. //
  6. // Created by Olivier Poitrey <rs@dailymotion.com> on 13/03/13.
  7. // Copyright 2013 Olivier Poitrey. All rights servered.
  8. //
  9. // For the full copyright and license information, please view the LICENSE
  10. // file that was distributed with this source code.
  11. //
  12. #import <Foundation/Foundation.h>
  13. #import <AVFoundation/AVFoundation.h>
  14. @protocol SDAVAssetExportSessionDelegate;
  15. /**
  16. * An `SDAVAssetExportSession` object transcodes the contents of an AVAsset source object to create an output
  17. * of the form described by specified video and audio settings. It implements most of the API of Apple provided
  18. * `AVAssetExportSession` but with the capability to provide you own video and audio settings instead of the
  19. * limited set of Apple provided presets.
  20. *
  21. * After you have initialized an export session with the asset that contains the source media, video and audio
  22. * settings, and the output file type (outputFileType), you can start the export running by invoking
  23. * `exportAsynchronouslyWithCompletionHandler:`. Because the export is performed asynchronously, this method
  24. * returns immediately — you can observe progress to check on the progress.
  25. *
  26. * The completion handler you supply to `exportAsynchronouslyWithCompletionHandler:` is called whether the export
  27. * fails, completes, or is cancelled. Upon completion, the status property indicates whether the export has
  28. * completed successfully. If it has failed, the value of the error property supplies additional information
  29. * about the reason for the failure.
  30. */
  31. @interface SDAVAssetExportSession : NSObject
  32. @property (nonatomic, weak) id<SDAVAssetExportSessionDelegate> delegate;
  33. /**
  34. * The asset with which the export session was initialized.
  35. */
  36. @property (nonatomic, strong, readonly) AVAsset *asset;
  37. /**
  38. * Indicates whether video composition is enabled for export, and supplies the instructions for video composition.
  39. *
  40. * You can observe this property using key-value observing.
  41. */
  42. @property (nonatomic, copy) AVVideoComposition *videoComposition;
  43. /**
  44. * Indicates whether non-default audio mixing is enabled for export, and supplies the parameters for audio mixing.
  45. */
  46. @property (nonatomic, copy) AVAudioMix *audioMix;
  47. /**
  48. * The type of file to be written by the session.
  49. *
  50. * The value is a UTI string corresponding to the file type to use when writing the asset.
  51. * For a list of constants specifying UTIs for standard file types, see `AV Foundation Constants Reference`.
  52. *
  53. * You can observe this property using key-value observing.
  54. */
  55. @property (nonatomic, copy) NSString *outputFileType;
  56. /**
  57. * The URL of the export session’s output.
  58. *
  59. * You can observe this property using key-value observing.
  60. */
  61. @property (nonatomic, copy) NSURL *outputURL;
  62. /**
  63. * The settings used for input video track.
  64. *
  65. * The dictionary’s keys are from <CoreVideo/CVPixelBuffer.h>.
  66. */
  67. @property (nonatomic, copy) NSDictionary *videoInputSettings;
  68. /**
  69. * The settings used for encoding the video track.
  70. *
  71. * A value of nil specifies that appended output should not be re-encoded.
  72. * The dictionary’s keys are from <AVFoundation/AVVideoSettings.h>.
  73. */
  74. @property (nonatomic, copy) NSDictionary *videoSettings;
  75. /**
  76. * The settings used for encoding the audio track.
  77. *
  78. * A value of nil specifies that appended output should not be re-encoded.
  79. * The dictionary’s keys are from <CoreVideo/CVPixelBuffer.h>.
  80. */
  81. @property (nonatomic, copy) NSDictionary *audioSettings;
  82. /**
  83. * The time range to be exported from the source.
  84. *
  85. * The default time range of an export session is `kCMTimeZero` to `kCMTimePositiveInfinity`,
  86. * meaning that (modulo a possible limit on file length) the full duration of the asset will be exported.
  87. *
  88. * You can observe this property using key-value observing.
  89. *
  90. */
  91. @property (nonatomic, assign) CMTimeRange timeRange;
  92. /**
  93. * Indicates whether the movie should be optimized for network use.
  94. *
  95. * You can observe this property using key-value observing.
  96. */
  97. @property (nonatomic, assign) BOOL shouldOptimizeForNetworkUse;
  98. /**
  99. * The metadata to be written to the output file by the export session.
  100. */
  101. @property (nonatomic, copy) NSArray *metadata;
  102. /**
  103. * Describes the error that occurred if the export status is `AVAssetExportSessionStatusFailed`
  104. * or `AVAssetExportSessionStatusCancelled`.
  105. *
  106. * If there is no error to report, the value of this property is nil.
  107. */
  108. @property (nonatomic, strong, readonly) NSError *error;
  109. /**
  110. * The progress of the export on a scale from 0 to 1.
  111. *
  112. *
  113. * A value of 0 means the export has not yet begun, 1 means the export is complete.
  114. *
  115. * Unlike Apple provided `AVAssetExportSession`, this property can be observed using key-value observing.
  116. */
  117. @property (nonatomic, assign, readonly) float progress;
  118. /**
  119. * The status of the export session.
  120. *
  121. * For possible values, see “AVAssetExportSessionStatus.”
  122. *
  123. * You can observe this property using key-value observing. (TODO)
  124. */
  125. @property (nonatomic, assign, readonly) AVAssetExportSessionStatus status;
  126. /**
  127. * Returns an asset export session configured with a specified asset.
  128. *
  129. * @param asset The asset you want to export
  130. * @return An asset export session initialized to export `asset`.
  131. */
  132. + (id)exportSessionWithAsset:(AVAsset *)asset;
  133. /**
  134. * Initializes an asset export session with a specified asset.
  135. *
  136. * @param asset The asset you want to export
  137. * @return An asset export session initialized to export `asset`.
  138. */
  139. - (id)initWithAsset:(AVAsset *)asset;
  140. /**
  141. * Starts the asynchronous execution of an export session.
  142. *
  143. * This method starts an asynchronous export operation and returns immediately. status signals the terminal
  144. * state of the export session, and if a failure occurs, error describes the problem.
  145. *
  146. * If internal preparation for export fails, handler is invoked synchronously. The handler may also be called
  147. * asynchronously, after the method returns, in the following cases:
  148. *
  149. * 1. If a failure occurs during the export, including failures of loading, re-encoding, or writing media data to the output.
  150. * 2. If cancelExport is invoked.
  151. * 3. After the export session succeeds, having completely written its output to the outputURL.
  152. *
  153. * @param handler A block that is invoked when writing is complete or in the event of writing failure.
  154. */
  155. - (void)exportAsynchronouslyWithCompletionHandler:(void (^)(void))handler;
  156. /**
  157. * Cancels the execution of an export session.
  158. *
  159. * You can invoke this method when the export is running.
  160. */
  161. - (void)cancelExport;
  162. @end
  163. @protocol SDAVAssetExportSessionDelegate <NSObject>
  164. - (void)exportSession:(SDAVAssetExportSession *)exportSession renderFrame:(CVPixelBufferRef)pixelBuffer withPresentationTime:(CMTime)presentationTime toBuffer:(CVPixelBufferRef)renderBuffer;
  165. @end