mafioso
mafioso

Reputation: 1632

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()

Upvotes: 17

Views: 14242

Answers (3)

Dr. Karvakuono
Dr. Karvakuono

Reputation: 1

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.

Upvotes: -2

Maor
Maor

Reputation: 3430

Hooni answer in Swift 3 :

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

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

Upvotes: 8

hooni
hooni

Reputation: 249

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))];
}

Upvotes: 6

Related Questions