AVAssetReader && AVAssetWriter

对媒体数据资源进行简单的转码或裁剪,使用 AVAssetExportSession 类便足够了,但是更深层次的修改媒体资源,便需要用到 AVAssetReader 类和 AVAssetWriter 类。

AVAssetReader

AVAssetReader 只能与一个资源 asset 相关联,且不能用来读取实时数据,在开始读取数据之前,需要为 reader 添加 AVAssetReaderOutput 的实例对象。这个实例对象描述的是待读取的数据资源来源类型,通常使用 AVAssetReaderAudioMixOutput 、AVAssetReaderTrackOutput 、AVAssetReaderVideoCompositionOutput 三种子类。

使用该类读取媒体资源,其提供的初始化方法与一个 asset 相关联。

  1. //对于提供的参数 asset ,如果是可被修改的,那么在开始读取操作后,对其进行了修改,之后的读取操作都是无效的
  2. + (nullable instancetype)assetReaderWithAsset:(AVAsset *)asset error:(NSError * _Nullable * _Nullable)outError;
  3. - (nullable instancetype)initWithAsset:(AVAsset *)asset error:(NSError * _Nullable * _Nullable)outError NS_DESIGNATED_INITIALIZER;
  4. //当前读取操作的状态,可取值有 AVAssetReaderStatusUnknown 、AVAssetReaderStatusReading 、
  5. AVAssetReaderStatusCompleted AVAssetReaderStatusFailed AVAssetReaderStatusCancelled
  6. @property (readonly) AVAssetReaderStatus status;
  7. //当 status 的值为 AVAssetReaderStatusFailed 时,描述错误信息
  8. @property (readonly, nullable) NSError *error;
  9. //限制可读取的资源的时间范围
  10. @property (nonatomic) CMTimeRange timeRange;
  11. //判断能否添加该数据源
  12. - (BOOL)canAddOutput:(AVAssetReaderOutput *)output;
  13. //添加数据源
  14. - (void)addOutput:(AVAssetReaderOutput *)output;
  15. //开始读取
  16. - (BOOL)startReading;
  17. //结束读取
  18. - (void)cancelReading;

AVAssetReaderOutput

AVAssetReaderOutput 是用来描述待读取的数据的抽象类,读取资源时,应创建该类的对象,并添加到相应的 AVAssetReader 实例对象中去。

  1. //获取的媒体数据的类型
  2. @property (nonatomic, readonly) NSString *mediaType;
  3. //是否拷贝缓存中的数据到客户端,默认 YES ,客户端可以随意修改数据,但是为优化性能,通常设为 NO
  4. @property (nonatomic) BOOL alwaysCopiesSampleData NS_AVAILABLE(10_8, 5_0);
  5. //同步获取下一个缓存数据,使用返回的数据结束后,应使用 CFRelease 函数将其释放
  6. //当错误或没有数据可读取时,返回 NULL ,返回空后,应检查相关联的 reader 的状态
  7. - (nullable CMSampleBufferRef)copyNextSampleBuffer CF_RETURNS_RETAINED;
  8. //是否支持重新设置数据的读取时间范围,即能否修改 reader 的 timeRange 属性
  9. @property (nonatomic) BOOL supportsRandomAccess NS_AVAILABLE(10_10, 8_0);
  10. //设置重新读取的时间范围,这个时间范围集合中的每一个时间范围的开始时间必需是增长的且各个时间范围不能重叠
  11. //应在 reader 调用 copyNextSampleBuffer 方法返回 NULL 之后才可调用
  12. - (void)resetForReadingTimeRanges:(NSArray<NSValue *> *)timeRanges NS_AVAILABLE(10_10, 8_0);
  13. //该方法调用后,上面的方法即不可再调用,同时 reader 的状态也不会被阻止变为 AVAssetReaderStatusCompleted 了
  14. - (void)markConfigurationAsFinal NS_AVAILABLE(10_10, 8_0);

AVAssetReaderTrackOutput

AVAssetReaderTrackOutput 是 AVAssetReaderOutput 的子类,它用来描述待读取的数据来自 asset track ,在读取前,还可以对数据的格式进行修改。

  1. //初始化方法,参数中指定了 track 和 媒体的格式
  2. //指定的 track 应在 reader 的 asset 中
  3. + (instancetype)assetReaderTrackOutputWithTrack:(AVAssetTrack *)track outputSettings:(nullable NSDictionary<NSString *, id> *)outputSettings;
  4. - (instancetype)initWithTrack:(AVAssetTrack *)track outputSettings:(nullable NSDictionary<NSString *, id> *)outputSettings NS_DESIGNATED_INITIALIZER;
  5. //指定音频处理时的算法
  6. @property (nonatomic, copy) NSString *audioTimePitchAlgorithm NS_AVAILABLE(10_9, 7_0);

AVAssetReaderAudioMixOutput

AVAssetReaderAudioMixOutput 是 AVAssetReaderOutput 的子类,它用来描述待读取的数据来自音频组合数据。创建该类实例对象提供的参数 audioTracks 集合中的每一个 asset track 都属于相应的 reader 中的 asset 实例对象,且类型为 AVMediaTypeAudio 。

  1. + (instancetype)assetReaderAudioMixOutputWithAudioTracks:(NSArray<AVAssetTrack *> *)audioTracks audioSettings:(nullable NSDictionary<NSString *, id> *)audioSettings;
  2. - (instancetype)initWithAudioTracks:(NSArray<AVAssetTrack *> *)audioTracks audioSettings:(nullable NSDictionary<NSString *, id> *)audioSettings NS_DESIGNATED_INITIALIZER

AVAssetReaderVideoCompositionOutput

AVAssetReaderVideoCompositionOutput 是 AVAssetReaderOutput 的子类,该类用来表示要读取的类是组合的视频数据。
同 AVAssetReaderAudioMixOutput 类似,该类也提供了两个创建实例的方法,需要提供的参数的 videoTracks 集合中每一个 track 都是 与 reader 相关联的 asset 中的 track 。

  1. + (instancetype)assetReaderVideoCompositionOutputWithVideoTracks:(NSArray<AVAssetTrack *> *)videoTracks videoSettings:(nullable NSDictionary<NSString *, id> *)videoSettings;
  2. - (instancetype)initWithVideoTracks:(NSArray<AVAssetTrack *> *)videoTracks videoSettings:(nullable NSDictionary<NSString *, id> *)videoSettings NS_DESIGNATED_INITIALIZER;

AVAssetWriter

AVAssetWriter 类可以将媒体数据从多个源写入指定文件格式的单个文件。我们也不需要将 asset writer 对象与特定 asset 相关联,但必须为要创建的每个输出文件使用单独的 asset writer。由于 asset writer 可以从多个来源写出媒体数据,因此我们必须为每个要被写入到输出文件的轨道创建一个 AVAssetWriterInput 对象。每个 AVAssetWriterInput 对象都期望以 CMSampleBufferRef 对象的形式接收数据,但是如果要将 CVPixelBufferRef 对象附加到 asset writer,可以使用 AVAssetWriterInputPixelBufferAdaptor 类。

  1. // AVAssetWriter 创建函数,需要指定输出文件的 URL 和所需的文件类型
  2. + (nullable instancetype)assetWriterWithURL:(NSURL *)outputURL fileType:(AVFileType)outputFileType error:(NSError * _Nullable * _Nullable)outError;
  3. - (nullable instancetype)initWithURL:(NSURL *)outputURL fileType:(AVFileType)outputFileType error:(NSError * _Nullable * _Nullable)outError NS_DESIGNATED_INITIALIZER;

AVAssetWriterInput

想要用 asset writer 来写媒体数据,必须至少设置一个 asset writer input。例如,如果数据源已经使用 CMSampleBufferRef 类来表示,那么使用 AVAssetWriterInput 类即可。

  1. - (instancetype)initWithMediaType:(AVMediaType)mediaType outputSettings:(nullable NSDictionary<NSString *, id> *)outputSettings;
  2. 使用例子
  3. 在本例子中,我们创建 ResourceSession 用于管理我们的资源读取和写入,该类主要有以下两个方法
  4. @interface ResourceSession : NSObject
  5. // 使用指定的 asset 初始化 session
  6. - (instancetype)initWithAsset:(AVAsset *)asset;
  7. // 开始转换
  8. - (void)startWithOutputURL:(NSURL *)url;
  9. @end

我们需要将 asset 资源中 videoTrack 和 audioTrack 的数据给读取出来,并写入到指定文件中;所以这里我们统一使用 SampleBufferChannel 这个类目去处理 SampleBuffer 的读取和写入功能

  1. @interface SampleBufferChannel () {
  2. // 写入队列
  3. dispatch_queue_t _serializationQueue;
  4. // 完成回调
  5. dispatch_block_t _completionHandler;
  6. }
  7. // 媒体类型
  8. @property (nonatomic, copy) NSString *mediaType;
  9. // 数据读取
  10. @property (nonatomic, strong) AVAssetReaderOutput *assetReaderOutput;
  11. // 数据写入
  12. @property (nonatomic, strong) AVAssetWriterInput *assetWriterInput;
  13. @end
  14. @implementation SampleBufferChannel
  15. // 初始化当前输入输出
  16. - (instancetype)initWithAssetReaderOutput:(AVAssetReaderOutput *)assetReaderOutput
  17. assetWriterInput:(AVAssetWriterInput *)assetWriterInput {
  18. if (self = [super init]) {
  19. _assetReaderOutput = assetReaderOutput;
  20. _assetWriterInput = assetWriterInput;
  21. _serializationQueue = dispatch_queue_create("com.SampleBufferChannel.serialQueue", NULL);
  22. }
  23. return self;
  24. }
  25. // 开始读取
  26. - (void)startWithDelegate:(id <SampleBufferChannelDelegate>)delegate completionHandler:(dispatch_block_t)completionHandler {
  27. _completionHandler = [completionHandler copy];
  28. __weak typeof(self) weakSelf = self;
  29. // AVAssetWriterInput 请求媒体数据,当 readyForMoreMediaData 的时候可以写入
  30. [self.assetWriterInput requestMediaDataWhenReadyOnQueue:_serializationQueue usingBlock:^{
  31. __strong typeof(weakSelf) strongSelf = weakSelf;
  32. BOOL completedOrFailed = NO;
  33. while ([strongSelf.assetWriterInput isReadyForMoreMediaData] && !completedOrFailed) {
  34. // 获取下一个 SampleBuffer 写入,当到结束时间点的时候,返回空
  35. CMSampleBufferRef sampleBuffer = [strongSelf.assetReaderOutput copyNextSampleBuffer];
  36. if (sampleBuffer) {
  37. // 写入数据
  38. BOOL success = [strongSelf.assetWriterInput appendSampleBuffer:sampleBuffer];
  39. CFRelease(sampleBuffer);
  40. sampleBuffer = NULL;
  41. completedOrFailed = !success;
  42. } else {
  43. completedOrFailed = YES;
  44. }
  45. }
  46. if (completedOrFailed) {
  47. [strongSelf callCompletionHandler];
  48. }
  49. }];
  50. }
  51. - (void)callCompletionHandler {
  52. // let the asset writer know that we will not be appending any more samples to this input
  53. [self.assetWriterInput markAsFinished];
  54. if (_completionHandler) {
  55. _completionHandler();
  56. }
  57. }
  58. @end

回到我们的 ResourceSession,实现我们的初始化函数

  1. - (instancetype)initWithAsset:(AVAsset *)asset {
  2. self = [super init];
  3. if (self) {
  4. _asset = asset;
  5. _serializationQueue = dispatch_queue_create("com.ResourceSession.serialQueue", NULL);
  6. }
  7. return self;
  8. }

开始转换

  1. - (void)startWithOutputURL:(NSURL *)url {
  2. self.outputURL = url;
  3. NSArray *keys = @[@"tracks", @"duration"];
  4. __weak typeof(self) weakSelf = self;
  5. // 判断当前属性是否已经准备好
  6. [self.asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
  7. __strong typeof(weakSelf) strongSelf = weakSelf;
  8. dispatch_async(strongSelf->_serializationQueue, ^{
  9. BOOL success = YES;
  10. NSError *localError = nil;
  11. // 获取对应属性
  12. success = ([strongSelf.asset statusOfValueForKey:@"tracks" error:&localError] == AVKeyValueStatusLoaded);
  13. if (success) {
  14. success = ([strongSelf.asset statusOfValueForKey:@"duration" error:&localError] == AVKeyValueStatusLoaded);
  15. }
  16. if (success) {
  17. strongSelf.timeRange = CMTimeRangeMake(kCMTimeZero, strongSelf.asset.duration);
  18. NSFileManager *fm = [NSFileManager defaultManager];
  19. NSString *path = [url path];
  20. if ([fm fileExistsAtPath:path]) {
  21. success = [fm removeItemAtPath:path error:&localError];
  22. }
  23. }
  24. if (success) {
  25. // 创建并初始化 Reader、Writer
  26. success = [strongSelf setupReaderAndWriterReturningError:&localError];
  27. }
  28. if (success) {
  29. // 开始转换
  30. success = [self startReadingAndWritingReturningError:&localError];
  31. }
  32. if (!success) {
  33. NSLog(@"not success");
  34. [self.assetReader cancelReading];
  35. [self.assetWriter cancelWriting];
  36. }
  37. });
  38. }];
  39. }
  40. 创建并初始化 ReaderWriter
  41. - (BOOL)setupReaderAndWriterReturningError:(NSError **)outError {
  42. BOOL success = YES;
  43. NSError *localError = nil;
  44. // 创建 AVAssetReader
  45. self.assetReader = [[AVAssetReader alloc] initWithAsset:self.asset error:&localError];
  46. success = self.assetReader != nil;
  47. if (success) {
  48. // 创建 AVAssetWriter
  49. self.assetWriter = [[AVAssetWriter alloc] initWithURL:self.outputURL fileType:AVFileTypeMPEG4 error:&localError];
  50. success = self.assetWriter != nil;
  51. }
  52. if (!success) {
  53. *outError = localError;
  54. return success;
  55. }
  56. AVAssetTrack *audioTrack = nil;
  57. AVAssetTrack *videoTrack = nil;
  58. // 获取 Audio Track
  59. NSArray *audioTracks = [self.asset tracksWithMediaType:AVMediaTypeAudio];
  60. if ([audioTracks count] > 0) {
  61. audioTrack = [audioTracks objectAtIndex:0];
  62. }
  63. // 获取 Video Track
  64. NSArray *videoTracks = [self.asset tracksWithMediaType:AVMediaTypeVideo];
  65. if ([videoTracks count] > 0) {
  66. videoTrack = [videoTracks objectAtIndex:0];
  67. }
  68. if (audioTrack) {
  69. // 设置指定读入格式为 PCM
  70. NSDictionary *decompressionAudioSettings = @{AVFormatIDKey: @(kAudioFormatLinearPCM)};
  71. // 指定 track、setting 初始化 AVAssetReaderOutput
  72. AVAssetReaderOutput *output = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:audioTrack outputSettings:decompressionAudioSettings];
  73. [self.assetReader addOutput:output];
  74. AudioChannelLayout stereoChannelLayout = {
  75. .mChannelLayoutTag = kAudioChannelLayoutTag_Stereo,
  76. .mChannelBitmap = 0,
  77. .mNumberChannelDescriptions = 0
  78. };
  79. NSData *channelLayoutAsData = [NSData dataWithBytes:&stereoChannelLayout length:offsetof(AudioChannelLayout, mChannelDescriptions)];
  80. // 指定输出格式
  81. NSDictionary *compressionAudioSettings = @{AVFormatIDKey: @(kAudioFormatMPEG4AAC),
  82. AVEncoderBitRateKey: @(128000),
  83. AVSampleRateKey: @(44100),
  84. AVChannelLayoutKey: channelLayoutAsData,
  85. AVNumberOfChannelsKey: @(2)};
  86. AVAssetWriterInput *input = [AVAssetWriterInput assetWriterInputWithMediaType:[audioTrack mediaType] outputSettings:compressionAudioSettings];
  87. // 添加 input
  88. [self.assetWriter addInput:input];
  89. self.audioSampleBufferChannel = [[SampleBufferChannel alloc] initWithAssetReaderOutput:output assetWriterInput:input];
  90. }
  91. if (videoTrack) {
  92. // 设置指定读入格式 ARGB
  93. NSDictionary *decompressionVideoSettings = @{(id)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32ARGB),
  94. (id)kCVPixelBufferIOSurfacePropertiesKey: @{}};
  95. AVAssetReaderOutput *output = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:videoTrack outputSettings:decompressionVideoSettings];
  96. [self.assetReader addOutput:output];
  97. // 获取当前视频 formatDescription
  98. CMFormatDescriptionRef formatDescription = NULL;
  99. NSArray *formatDescriptions = [videoTrack formatDescriptions];
  100. if ([formatDescriptions count] > 0) {
  101. formatDescription = (__bridge CMFormatDescriptionRef)[formatDescriptions objectAtIndex:0];
  102. }
  103. // 获取视频尺寸
  104. CGSize trackDimensions = CGSizeZero;
  105. if (formatDescription) {
  106. trackDimensions = CMVideoFormatDescriptionGetPresentationDimensions(formatDescription, false, false);
  107. } else {
  108. trackDimensions = [videoTrack naturalSize];
  109. }
  110. // Grab clean aperture, pixel aspect ratio from format description
  111. NSDictionary *compressionSettings = nil;
  112. if (formatDescription) {
  113. NSDictionary *cleanAperture = nil;
  114. NSDictionary *pixelAspectRatio = nil;
  115. CFDictionaryRef cleanApertureFromCMFormatDescription = CMFormatDescriptionGetExtension(formatDescription, kCMFormatDescriptionExtension_CleanAperture);
  116. if (cleanApertureFromCMFormatDescription) {
  117. cleanAperture = @{AVVideoCleanApertureWidthKey: (NSNumber *)CFDictionaryGetValue(cleanApertureFromCMFormatDescription, kCMFormatDescriptionKey_CleanApertureWidth),
  118. AVVideoCleanApertureHeightKey: (NSNumber *)CFDictionaryGetValue(cleanApertureFromCMFormatDescription, kCMFormatDescriptionKey_CleanApertureHeight),
  119. AVVideoCleanApertureHorizontalOffsetKey: (NSNumber *)CFDictionaryGetValue(cleanApertureFromCMFormatDescription, kCMFormatDescriptionKey_CleanApertureHorizontalOffset),
  120. AVVideoCleanApertureVerticalOffsetKey: (NSNumber *)CFDictionaryGetValue(cleanApertureFromCMFormatDescription, kCMFormatDescriptionKey_CleanApertureVerticalOffset)};
  121. }
  122. CFDictionaryRef pixelAspectRatioFromCMFormatDescription = CMFormatDescriptionGetExtension(formatDescription, kCMFormatDescriptionExtension_PixelAspectRatio);
  123. if (pixelAspectRatioFromCMFormatDescription) {
  124. pixelAspectRatio = @{AVVideoPixelAspectRatioHorizontalSpacingKey: (NSNumber *)CFDictionaryGetValue(pixelAspectRatioFromCMFormatDescription, kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing),
  125. AVVideoPixelAspectRatioVerticalSpacingKey: (NSNumber *)CFDictionaryGetValue(pixelAspectRatioFromCMFormatDescription, kCMFormatDescriptionKey_PixelAspectRatioVerticalSpacing)};
  126. }
  127. if (cleanAperture || pixelAspectRatio)
  128. {
  129. NSMutableDictionary *mutableCompressionSettings = [NSMutableDictionary dictionary];
  130. if (cleanAperture)
  131. [mutableCompressionSettings setObject:cleanAperture forKey:AVVideoCleanApertureKey];
  132. if (pixelAspectRatio)
  133. [mutableCompressionSettings setObject:pixelAspectRatio forKey:AVVideoPixelAspectRatioKey];
  134. compressionSettings = mutableCompressionSettings;
  135. }
  136. }
  137. // 指定输出格式 H.264
  138. NSMutableDictionary *videoSettings = @{AVVideoCodecKey: AVVideoCodecTypeH264,
  139. AVVideoWidthKey: @(trackDimensions.width),
  140. AVVideoHeightKey: @(trackDimensions.height)}.mutableCopy;
  141. if (compressionSettings) {
  142. videoSettings[AVVideoCompressionPropertiesKey] = compressionSettings;
  143. }
  144. AVAssetWriterInput *input = [AVAssetWriterInput assetWriterInputWithMediaType:[videoTrack mediaType] outputSettings:videoSettings];
  145. [self.assetWriter addInput:input];
  146. self.videoSampleBufferChannel = [[SampleBufferChannel alloc] initWithAssetReaderOutput:output assetWriterInput:input];
  147. }
  148. if (outError) {
  149. *outError = localError;
  150. }
  151. return success;
  152. }

开始转换

  1. - (BOOL)startReadingAndWritingReturningError:(NSError **)outError {
  2. BOOL success = YES;
  3. NSError *localError = nil;
  4. // start
  5. success = [self.assetReader startReading];
  6. if (!success) {
  7. localError = self.assetReader.error;
  8. }
  9. if (success) {
  10. success = [self.assetWriter startWriting];
  11. if (!success) {
  12. localError = self.assetWriter.error;
  13. }
  14. }
  15. if (success) {
  16. dispatch_group_t dispatchGroup = dispatch_group_create();
  17. [self.assetWriter startSessionAtSourceTime:self.timeRange.start];
  18. if (self.audioSampleBufferChannel) {
  19. dispatch_group_enter(dispatchGroup);
  20. [self.audioSampleBufferChannel startWithDelegate:self completionHandler:^{
  21. dispatch_group_leave(dispatchGroup);
  22. }];
  23. }
  24. if (self.videoSampleBufferChannel)
  25. {
  26. dispatch_group_enter(dispatchGroup);
  27. [self.videoSampleBufferChannel startWithDelegate:self completionHandler:^{
  28. dispatch_group_leave(dispatchGroup);
  29. }];
  30. }
  31. dispatch_group_notify(dispatchGroup, _serializationQueue, ^{
  32. // end
  33. BOOL finalSuccess = YES;
  34. NSError *finalError = nil;
  35. if (self.cancelled) {
  36. [self.assetReader cancelReading];
  37. [self.assetWriter cancelWriting];
  38. } else {
  39. if (self.assetReader.status == AVAssetReaderStatusFailed) {
  40. finalSuccess = NO;
  41. finalError = self.assetReader.error;
  42. }
  43. if (finalSuccess) {
  44. [self.assetWriter finishWritingWithCompletionHandler:^{
  45. NSLog(@"finished");
  46. }];
  47. }
  48. }
  49. });
  50. }
  51. if (outError) {
  52. *outError = localError;
  53. }
  54. return success;
  55. }

如果你正在跳槽或者正准备跳槽不妨动动小手,添加一下咱们的交流群1012951431来获取一份详细的大厂面试资料为你的跳槽多添一份保障。

更多精彩分享