2016-07-18 4 views
4

Я получил видео из livephoto и экспортировал видео, как это больше раз.
Это стало больше красного, чем источник livephoto. Это ошибка?
Вот код экспорта и есть демонстрационный проект в конце.AVAssetExportSession, AVMutableVideoComposition сделать видео более красным

- (void)clipMovie:(NSString *)path block:(void (^)(BOOL success))completionBlock 
    { 
     UIColor *bgcolor = (UIColor *)[UIColor blackColor]; 

     AVURLAsset *videoasset = [[AVURLAsset alloc]initWithURL:[NSURL fileURLWithPath:path] options:nil]; 

     AVMutableComposition *composition = [AVMutableComposition composition]; 
     NSError *error; 
     [composition insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoasset.duration) ofAsset:videoasset atTime:kCMTimeZero error:&error]; 

     AVMutableVideoCompositionInstruction *videoInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; 
     videoInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration); 
     videoInstruction.backgroundColor = bgcolor.CGColor; 


     if ([[composition tracksWithMediaType:AVMediaTypeVideo] count] > 0) 
     { 
      AVAssetTrack *videoTrack = [[composition tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 

      AVMutableVideoCompositionLayerInstruction *cropLayer = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack]; 

    //  CGSize naturalSize = videoTrack.naturalSize; 

      CGSize renderSize = CGSizeMake(1080, 1080); 

      AVAssetTrack *videoTracktmp = [[videoasset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]; 
      CGAffineTransform transform = videoTracktmp.preferredTransform; 

      CGRect drawrect = CGRectMake(0, 0, 1080, 1080); 

      [cropLayer setTransform:transform atTime:kCMTimeZero]; 

      CALayer *backgroundLayer = [CALayer layer]; 
      //  [backgroundLayer setContents:(id)self.patternBgView.image.CGImage]; 
      backgroundLayer.frame = CGRectMake(0, 0, renderSize.width, renderSize.height); 
      [backgroundLayer setMasksToBounds:YES]; 
      backgroundLayer.backgroundColor = bgcolor.CGColor; 

      CALayer *videoLayer = [CALayer layer]; 


      videoLayer.frame = CGRectMake(drawrect.origin.x, (renderSize.height-drawrect.size.height-drawrect.origin.y), drawrect.size.width, drawrect.size.height); 
      CALayer *parentLayer = [CALayer layer]; 
      parentLayer.frame = CGRectMake(0, 0, renderSize.width, renderSize.height); 
      [parentLayer addSublayer:backgroundLayer]; 
      [parentLayer addSublayer:videoLayer]; 

      AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition]; 
      videoComposition.animationTool = [AVVideoCompositionCoreAnimationTool videoCompositionCoreAnimationToolWithPostProcessingAsVideoLayer:videoLayer inLayer:parentLayer]; 
      videoInstruction.layerInstructions = @[cropLayer]; 
      videoComposition.instructions = @[videoInstruction]; 

      videoComposition.renderSize = CGSizeMake(renderSize.width, renderSize.height); 
      videoComposition.frameDuration = CMTimeMake(1, 15); 

      AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:videoasset presetName:AVAssetExportPresetHighestQuality]; 
      exportSession.shouldOptimizeForNetworkUse = YES; 
      exportSession.videoComposition = videoComposition; 
      //  exportSession.metadata = videoasset.metadata; 

      NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"] stringByAppendingPathComponent:@"a"]; 
      BOOL bo = [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; 
      NSString *result = [path stringByAppendingPathComponent:@"a.mov"]; 

      NSString *exportVideoPath = result; 
    //  NSLog(@"%@",result); 

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

      NSURL *exportURL = [NSURL fileURLWithPath:exportVideoPath]; 
      exportSession.outputURL = exportURL; 
    //  exportSession.metadata = [self updateMetadata:videoasset.metadata]; 
      exportSession.outputFileType = AVFileTypeQuickTimeMovie; 
      [exportSession exportAsynchronouslyWithCompletionHandler:^{ 
       dispatch_async(dispatch_get_main_queue(), ^{ 
        switch ((int)exportSession.status) { 
         case AVAssetExportSessionStatusFailed:{ 
          //     NSLog (@"FAIL"); 
          completionBlock(NO); 
          break; 
         } 
         case AVAssetExportSessionStatusCompleted: { 
          //     NSLog (@"SUCCESS"); 
          completionBlock(YES); 
          break; 
         } 
        }; 
       }); 

      }]; 
     } 

    } 

тогда видео более красное, чем источник livephoto.

здесь demo link on github

  1. запустить проект
  2. он представит imagepicker после 1s.
  3. выберите livephoto
  4. и нажмите выбрал
  5. то новое видео сохранить в свой альбом. Это стало красным!

ответ

2

Opps! Просто сделай это!

AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:videoasset presetName:AVAssetExportPresetPassthrough]; 

Этот вариант экспорта приведет к СМИ всех треков, которые передаются на выход так же, как хранится в исходном актива, за исключением дорожек, для которых PASSTHROUGH не представляется возможным, как правило, из-за ограничений, формат контейнера, указанный указанным значением outputFileType. Этот параметр не включен в массивы, возвращаемые -allExportPresets и -exportPresetsCompatibleWithAsset.