Rotating Video with AVMutableVideoCompositionLayerInstruction

前端 未结 5 1448
一向
一向 2021-02-01 08:32

I\'m shooting video on an iPhone 4 with the front camera and combining the video with some other media assets. I\'d like for this video to be portrait orientation - the default

相关标签:
5条回答
  • 2021-02-01 09:03

    I found solution in Flutter plugin project.

    - (CGAffineTransform)fixTransform:(AVAssetTrack*)videoTrack {
      CGAffineTransform transform = videoTrack.preferredTransform;
    
      if (transform.tx == 0 && transform.ty == 0) {
          NSInteger rotationDegrees = (NSInteger)round(radiansToDegrees(atan2(transform.b, transform.a)));
          NSLog(@"TX and TY are 0. Rotation: %ld. Natural width,height: %f, %f", (long)rotationDegrees,
          videoTrack.naturalSize.width, videoTrack.naturalSize.height);
          if (rotationDegrees == 90) {
            NSLog(@"Setting transform tx");
            transform.tx = videoTrack.naturalSize.height;
            transform.ty = 0;
          } else if (rotationDegrees == 270) {
            NSLog(@"Setting transform ty");
            transform.tx = 0;
            transform.ty = videoTrack.naturalSize.width;
         }
      }
      return transform;
    }
    
    // set layerInstruction
    [firstVideoLayerInstruction setTransform:[self fixTransform:firstVideoAssetTrack] atTime:kCMTimeZero];
    

    Flutter VideoPlayerPlugin

    0 讨论(0)
  • 2021-02-01 09:04

    Building on what was answered so far. I found a very good way of debugging and finding out what went wrong with your transforms. Using the ramp methods available, you are able to animate the transforms making it easier to see what your transform is doing.

    Most of the time I found myself having transforms that appeared to do nothing until I realised that just using preferredTransform property of a video track alone may result in the video feed moving out of the render screen.

    AVMutableVideoCompositionLayerInstruction *videoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
    
    [videoLayerInstruction setTransformRampFromStartTransform:CGAffineTransformIdentity
    toEndTransform:videoTrack.preferredTransform 
    timeRange:CMTimeRangeMake(projectClipStart, projectClipDuration)];
    

    Eventually, I found that in some cases I needed to apply a translation to bring back the rotated video into the render screen.

    CGAffineTransformConcat(videoTrack.preferredTransform, CGAffineTransformMakeTranslation(0, renderSize.height))
    

    Note: Your translation values may be different.

    0 讨论(0)
  • 2021-02-01 09:09

    Also may be relevant, but there is a bug in the preferredTransform being set when using the front-facing camera .. see here for example, where the guys in the SDAVAssetExportSession project have coded a work-around:

    https://github.com/rs/SDAVAssetExportSession/pull/70

    0 讨论(0)
  • 2021-02-01 09:12
    AVAssetTrack *videoAssetTrack= [[videoAsset tracksWithMediaType:AVMediaTypeVideo] lastObject];
    
    AVMutableCompositionTrack *videoCompositionTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    [videoCompositionTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:videoAssetTrack atTime:kCMTimeZero error:&error];
    
    videoCompositionTrack.preferredTransform = videoAssetTrack.preferredTransform;
    
    0 讨论(0)
  • 2021-02-01 09:20

    Try this:

    AVMutableVideoCompositionLayerInstruction *passThroughLayer = AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
    CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(degreesToRadians(90.0));
    CGAffineTransform rotateTranslate = CGAffineTransformTranslate(rotateTransform,320,0);
    [passThroughLayer setTransform:rotateTranslate atTime:kCMTimeZero];
    

    Essentially the idea is to create a rotation and translation matrix. You rotate it to the proper orientation and then translate it into the view. I did not see any way to specify a center point while I was glancing through the API.

    0 讨论(0)
提交回复
热议问题