简体   繁体   中英

How to change video orientation of AVPlayer?

I want to rotate the replay of a video using AVPlayer. Is there a way to rotate it 90 degrees clockwise?
Here's some code:

self.player = AVPlayer(URL: NSURL(fileURLWithPath: dataPath))
playerLayer = AVPlayerLayer.init(player: self.player)
playerLayer.frame = view.bounds

playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
view.layer.addSublayer(playerLayer)
player.play()

UPDATE

This one works:

self.player = AVPlayer(URL: NSURL(fileURLWithPath: dataPath))
playerLayer = AVPlayerLayer.init(player: self.player)                                     
playerLayer.setAffineTransform(CGAffineTransformMakeRotation(CGFloat(M_PI))
playerLayer.frame = view.bounds        
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
view.layer.addSublayer(playerLayer)
player.play()

If you are using Objective-C, the code below will help.

Sample code:

#define degreeToRadian(x) (M_PI * x / 180.0)
#define radianToDegree(x) (180.0 * x / M_PI)

- (void)rotateVideoPlayerWithDegree:(CGFloat)degree {
    [_playerLayer setAffineTransform:CGAffineTransformMakeRotation(degreeToRadian(degree))];
}

Hooni answer in Swift 3 :

let affineTransform = CGAffineTransform(rotationAngle: degreeToRadian(90))
avPlayerLayer.setAffineTransform(affineTransform)

func degreeToRadian(_ x: CGFloat) -> CGFloat {
    return .pi * x / 180.0
}

I do,

+ (Class)layerClass
{
    return [AVPlayerLayer class];
}

Then in init, the class is an NSView:

[(AVPlayerLayer*)self.layer setPlayer:player];

self.wantsLayer=YES;
self.superview.wantsLayer=YES;

if (fakePortrait) {
   [self rotateByAngle:90];
}

However, this only works in 10.13. I'm looking for a solution for 10.9 and above. If I rotate the video pre 10.13, it disappears.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM