AVPlayer帧动画

rky*_*kyr 17 video avfoundation ios swift

我正在开发一个应用程序,其中包括使用每帧动画播放视频的功能.您可以看到此类功能的示例.

我已经尝试添加CAKeyFrameAnimation到子层,AVSynchronizedLayer并有一些麻烦.

我也已经尝试使用预渲染视频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)
}
Run Code Online (Sandbox Code Playgroud)

Bom*_*hen -2

这是我的意见,

将 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];
Run Code Online (Sandbox Code Playgroud)

然后

为 videoView 提供动画