Rotating Video with AVMutableVideoCompositionLayerInstruction

前端 未结 5 1566
一向
一向 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

提交回复
热议问题