2016-08-02 89 views
8

我試圖將高質量視頻壓縮成更小的尺寸,並且能夠使用以下目標-c代碼縮小我壓縮的視頻的大小:使用庫或Algo壓縮視頻大小的最快方法

- (BOOL)convertMovieToMP4:(NSString) originalMovPath andStoragePath:(NSString) compMovPath 
     { 
      NSURL *tmpSourceUrl = [NSURL fileURLWithPath:originalMovPath]; 

      compMovPath = [compMovPath stringByReplacingOccurrencesOfString:[compMovPath pathExtension] withString:@"mp4"]; 
      NSURL *tmpDestUrl = [NSURL fileURLWithPath:compMovPath]; 

      AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:tmpSourceUrl options:nil]; 
      AVMutableComposition* mixComposition = [AVMutableComposition composition]; 

      AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 

      AVAssetTrack *clipVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
      [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
              ofTrack:clipVideoTrack 
              atTime:kCMTimeZero error:nil]; 

      [compositionVideoTrack setPreferredTransform:[[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] preferredTransform]]; 

      CGSize videoSize = [[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] naturalSize]; 

      CATextLayer *titleLayer = [CATextLayer layer]; 
      titleLayer.string = @"Ojatro"; 
      titleLayer.font = (_bridge CFTypeRef Nullable)(@"Helvetica"); 
      titleLayer.fontSize = videoSize.height/8; 
      titleLayer.shadowOpacity = 0.2; 
      titleLayer.alignmentMode = kCAAlignmentCenter; 
      titleLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height/6); 
      titleLayer.position=CGPointMake(videoSize.width/2, videoSize.height/2); 

      CALayer *parentLayer = [CALayer layer]; 
      CALayer *videoLayer = [CALayer layer]; 
      parentLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height); 
      videoLayer.frame = CGRectMake(0, 0, videoSize.width, videoSize.height); 
      [parentLayer addSublayer:videoLayer]; 
      [parentLayer addSublayer:titleLayer]; 

      AVMutableVideoComposition* videoComp = [AVMutableVideoComposition videoComposition]; 
      videoComp.renderSize = videoSize; 
      videoComp.frameDuration = CMTimeMake(1, 30); 
      videoComp.animationTool = [AVVideoCompositionCoreAnimationTool  videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer]; 

      AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; 
      instruction.timeRange = CMTimeRangeMake(kCMTimeZero, [mixComposition duration]); 

      AVAssetTrack *videoTrack = [[mixComposition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 

      AVMutableVideoCompositionLayerInstruction* layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack]; 

      instruction.layerInstructions = [NSArray arrayWithObject:layerInstruction]; 
      videoComp.instructions = [NSArray arrayWithObject: instruction]; 

      AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetMediumQuality];//AVAssetExportPresetPasst 
      _assetExport.videoComposition = videoComp; 
      //NSString* videoName = @"mynewwatermarkedvideo.mov"; 
      NSString *tmpDirPath = [compMovPath stringByReplacingOccurrencesOfString:[compMovPath lastPathComponent] withString:@""]; 
      if ([Utility makeDirectoryAtPath:tmpDirPath]) 
      { 
       NSLog(@"Directory Created"); 
      } 
      //exportPath=[exportPath stringByAppendingString:videoName]; 
      NSURL *exportUrl = tmpDestUrl; 

      if ([[NSFileManager defaultManager] fileExistsAtPath:compMovPath]) 
      { 
       [[NSFileManager defaultManager] removeItemAtPath:compMovPath error:nil]; 
      } 

      _assetExport.outputURL = exportUrl; 
      _assetExport.shouldOptimizeForNetworkUse = YES; 
      _assetExport.outputFileType = AVFileTypeMPEG4; 

      //[strRecordedFilename setString: exportPath]; 

      [_assetExport exportAsynchronouslyWithCompletionHandler: 
      ^(void) { 
       switch (_assetExport.status) 
       { 
        case AVAssetExportSessionStatusUnknown: 
         NSLog(@"Export Status Unknown"); 

         break; 
        case AVAssetExportSessionStatusWaiting: 
         NSLog(@"Export Waiting"); 

         break; 
        case AVAssetExportSessionStatusExporting: 
         NSLog(@"Export Status"); 

         break; 
        case AVAssetExportSessionStatusCompleted: 
         NSLog(@"Export Completed"); 
         totalFilesCopied++; 
         [self startProgressBar]; 

         break; 
        case AVAssetExportSessionStatusFailed: 
         NSLog(@"Export failed"); 

         break; 
        case AVAssetExportSessionStatusCancelled: 
         NSLog(@"Export canceled"); 

         break; 
       } 
      } 
      ]; 
      return NO; 
     } 

但我的主要問題是,當我壓縮500MB視頻(即平均視頻)文件,大約需要20至30分鐘。它將視頻大小減少到大約130MB。我正在使用本地AVFoundation庫來壓縮視頻以減小其大小。

我需要就像蘋果Compressor應用程序非常快的壓縮視頻大小,它僅壓縮在30秒內將500MB的文件...

https://itunes.apple.com/en/app/compressor/id424390742?mt=12

我也用FFMPEG庫的是,但這也很慢,我沒有發現這個庫更有用。

我也試圖找到使用其他語言如java,python的解決方案。但沒有找到任何解決方案。

如果有人對這個問題有解決方案,或者有一些庫(即Paid庫或開源庫)可以用更少的時間至少1分鐘完成壓縮...請與我分享。或者其他一些可以將壓縮時間問題從20到30分鐘到至少1分鐘的代碼。

謝謝...

回答