ios AVPlayer帧动画

nukf8bse  于 2023-04-22  发布在  iOS
关注(0)|答案(1)|浏览(105)

我正在开发一个应用程序,其中包括功能,以播放视频与每帧动画。你可以看到一个example这样的功能。
我已经尝试将CAKeyFrameAnimation添加到AVSynchronizedLayer的子层,并将some troubles与之一起使用。
我也试过用AVAssetExportSession来预渲染视频,效果很好,但是速度很慢,渲染这样的视频需要3分钟。
也许还有其他方法可以做到这一点?

更新:

下面是我使用AVSynchronizedLayer实现动画的方法:

let fullScreenAnimationLayer = CALayer()
fullScreenAnimationLayer.frame = videoRect
fullScreenAnimationLayer.geometryFlipped = true

values: [NSValue] = [], times: [NSNumber] = []

// fill values array with positions of face center for each frame
values.append(NSValue(CATransform3D: t))

// fill times with corresoinding time for each frame
times.append(NSNumber(double: (Double(j) / fps) / videoDuration)) // where fps = 25 (according to video file fps)

...

let transform = CAKeyframeAnimation(keyPath: "transform")
transform.duration = videoDuration
transform.calculationMode = kCAAnimationDiscrete
transform.beginTime = AVCoreAnimationBeginTimeAtZero
transform.values = values
transform.keyTimes = times
transform.removedOnCompletion = false
transform.fillMode = kCAFillModeForwards
fullScreenAnimationLayer.addAnimation(transform, forKey: "a_transform")

...

if let syncLayer = AVSynchronizedLayer(playerItem: player.currentItem) {
    syncLayer.frame = CGRect(origin: CGPointZero, size: videoView.bounds.size)
    syncLayer.addSublayer(fullScreenAnimationLayer)
    videoView.layer.addSublayer(syncLayer)
}
46scxncf

46scxncf1#

我的看法是
将AVPlayer层属性(AVPlayerLayer类)添加到UIView层的子层,然后操作视图动画。
例如,

AVURLAsset *urlAsset = [AVURLAsset URLAssetWithURL:blahURL options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:urlAsset];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
playerLayer.frame = yourFrame;

UIView *videoView = [UIView new];
[videoView addSublayer:playerLayer];

然后
给予animations to video查看

相关问题