iOS 視頻壓縮(自定義壓縮參數(shù))

最近一個(gè)新需求需要將選擇的視頻進(jìn)行壓縮后上傳,且要求按照壓縮參數(shù)進(jìn)行壓縮。因此寫(xiě)上這文章記錄下,我總結(jié)過(guò)后寫(xiě)出的關(guān)于自定義壓縮參數(shù)壓縮視頻。

- (void)compressVideo:(NSURL *)videoUrl
    withVideoSettings:(NSDictionary *)videoSettings
        AudioSettings:(NSDictionary *)audioSettings
             fileType:(AVFileType)fileType
             complete:(void (^)(NSURL * _Nullable, NSError * _Nullable))complete {
  NSURL *outputUrl = [NSURL fileURLWithPath:[self buildFilePath]];
  
  AVAsset *asset = [AVAsset assetWithURL:videoUrl];
  AVAssetReader *reader = [AVAssetReader assetReaderWithAsset:asset error:nil];
  AVAssetWriter *writer = [AVAssetWriter assetWriterWithURL:outputUrl fileType:fileType error:nil];
  
  // video part
  AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] firstObject];
  AVAssetReaderTrackOutput *videoOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:videoTrack outputSettings:[self configVideoOutput]];
  AVAssetWriterInput *videoInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeVideo outputSettings:videoSettings];
  if ([reader canAddOutput:videoOutput]) {
    [reader addOutput:videoOutput];
  }
  if ([writer canAddInput:videoInput]) {
    [writer addInput:videoInput];
  }
  
  // audio part
  AVAssetTrack *audioTrack = [[asset tracksWithMediaType:AVMediaTypeAudio] firstObject];
  AVAssetReaderTrackOutput *audioOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:audioTrack outputSettings:[self configAudioOutput]];
  AVAssetWriterInput *audioInput = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioSettings];
  if ([reader canAddOutput:audioOutput]) {
    [reader addOutput:audioOutput];
  }
  if ([writer canAddInput:audioInput]) {
    [writer addInput:audioInput];
  }
  
  // 開(kāi)始讀寫(xiě)
  [reader startReading];
  [writer startWriting];
  [writer startSessionAtSourceTime:kCMTimeZero];
  
  //創(chuàng)建視頻寫(xiě)入隊(duì)列
  dispatch_queue_t videoQueue = dispatch_queue_create("Video Queue", DISPATCH_QUEUE_SERIAL);
  //創(chuàng)建音頻寫(xiě)入隊(duì)列
  dispatch_queue_t audioQueue = dispatch_queue_create("Audio Queue", DISPATCH_QUEUE_SERIAL);
  //創(chuàng)建一個(gè)線程組
  dispatch_group_t group = dispatch_group_create();
  //進(jìn)入線程組
  dispatch_group_enter(group);
  //隊(duì)列準(zhǔn)備好后 usingBlock
  [videoInput requestMediaDataWhenReadyOnQueue:videoQueue usingBlock:^{
    BOOL completedOrFailed = NO;
    while ([videoInput isReadyForMoreMediaData] && !completedOrFailed) {
          CMSampleBufferRef sampleBuffer = [videoOutput copyNextSampleBuffer];
      if (sampleBuffer != NULL) {
        [videoInput appendSampleBuffer:sampleBuffer];
        DLog(@"===%@===", sampleBuffer);
        CFRelease(sampleBuffer);
      }
      else {
        completedOrFailed = YES;
        [videoInput markAsFinished];
        dispatch_group_leave(group);
      }
    }
  }];
  
  dispatch_group_enter(group);
  //隊(duì)列準(zhǔn)備好后 usingBlock
  [audioInput requestMediaDataWhenReadyOnQueue:audioQueue usingBlock:^{
    BOOL completedOrFailed = NO;
    while ([audioInput isReadyForMoreMediaData] && !completedOrFailed) {
      CMSampleBufferRef sampleBuffer = [audioOutput copyNextSampleBuffer];
      if (sampleBuffer != NULL) {
        BOOL success = [audioInput appendSampleBuffer:sampleBuffer];
        DLog(@"===%@===", sampleBuffer);
        CFRelease(sampleBuffer);
        completedOrFailed = !success;
      }
      else {
        completedOrFailed = YES;
      }
    }
    
    if (completedOrFailed) {
      [audioInput markAsFinished];
      dispatch_group_leave(group);
    }
  }];
  
  //完成壓縮
  dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    if ([reader status] == AVAssetReaderStatusReading) {
      [reader cancelReading];
    }
    
    switch (writer.status) {
      case AVAssetWriterStatusWriting: {
        DLog(@"視頻壓縮完成");
        [writer finishWritingWithCompletionHandler:^{
          
          // 可以嘗試異步回至主線程回調(diào)
          if (complete) {
            complete(outputUrl,nil);
          }
          
        }];
      }
        break;
          
      case AVAssetWriterStatusCancelled:
        DLog(@"取消壓縮");
        break;
          
      case AVAssetWriterStatusFailed:
        DLog(@"===error:%@===", writer.error);
        if (complete) {
          complete(nil,writer.error);
        }
        break;
          
      case AVAssetWriterStatusCompleted: {
        DLog(@"視頻壓縮完成");
        [writer finishWritingWithCompletionHandler:^{
          
          // 可以嘗試異步回至主線程回調(diào)
          if (complete) {
            complete(outputUrl,nil);
          }
        }];
      }
        break;
          
      default:
        break;
    }
  });
}
/** 視頻解碼 */
- (NSDictionary *)configVideoOutput {

  NSDictionary *videoOutputSetting = @{
    (__bridge NSString *)kCVPixelBufferPixelFormatTypeKey:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_422YpCbCr8],
    (__bridge NSString *)kCVPixelBufferIOSurfacePropertiesKey:[NSDictionary dictionary]
  };
    
  return videoOutputSetting;
}

/** 音頻解碼 */
- (NSDictionary *)configAudioOutput {
  NSDictionary *audioOutputSetting = @{
    AVFormatIDKey: @(kAudioFormatLinearPCM)
  };
  return audioOutputSetting;
}
/// 指定音視頻的壓縮碼率,profile,幀率等關(guān)鍵參數(shù)信息,這些參數(shù)可以根據(jù)要求自行更改
- (NSDictionary *)performanceVideoSettings {
  NSDictionary *compressionProperties = @{
    AVVideoAverageBitRateKey          : @(409600), // 碼率 400K
    AVVideoExpectedSourceFrameRateKey : @24, // 幀率
    AVVideoProfileLevelKey            : AVVideoProfileLevelH264HighAutoLevel
  };
  
  NSString *videoCodeec;
  if (@available(iOS 11.0, *)) {
      videoCodeec = AVVideoCodecTypeH264;
  } else {
      videoCodeec = AVVideoCodecH264;
  }
  NSDictionary *videoCompressSettings = @{
    AVVideoCodecKey                 : videoCodeec,
    AVVideoWidthKey                 : @640,
    AVVideoHeightKey                : @360,
    AVVideoCompressionPropertiesKey : compressionProperties,
    AVVideoScalingModeKey           : AVVideoScalingModeResizeAspectFill
  };
  
  return videoCompressSettings;
}
- (NSDictionary *)performanceAudioSettings {
  AudioChannelLayout stereoChannelLayout = {
    .mChannelLayoutTag = kAudioChannelLayoutTag_Stereo, 
    .mChannelBitmap = kAudioChannelBit_Left,
    .mNumberChannelDescriptions = 0
  };
  NSData *channelLayoutAsData = [NSData dataWithBytes:&stereoChannelLayout length:offsetof(AudioChannelLayout, mChannelDescriptions)];
  NSDictionary *audioCompressSettings = @{
    AVFormatIDKey         : @(kAudioFormatMPEG4AAC),
    AVEncoderBitRateKey   : @(49152), // 碼率 48K
    AVSampleRateKey       : @44100, // 采樣率
    AVChannelLayoutKey    : channelLayoutAsData,
    AVNumberOfChannelsKey : @(2)  // 聲道
  };
  
  return audioCompressSettings;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容