Search code examples
iosswiftavfoundationavplayeravmutablecomposition

Preview changes made to AVMutableComposition


I use AVMutableComposition to perform a scaleTimeRange operation on an AVAsset. Everytime time the timeRange is scaled (i.e. slow motion is done on the video), I have to export it using AVExportSession.

Q) I was wondering if there is a lighter way to preview the asset whenever I perform scaleTimeRange , instead of having to export the Asset everytime to view changes.

I read that AVPlayerItem is helpful, but I can't understand how.


Solution

  • You can create an AVPlayerItem from an AVAsset using AVPlayerItem(asset:). Also, an AVMutableComposition is an AVComposition is an AVAsset.

    See the overview in the documentation for AVMutableComposition. There is an example of doing exactly that: https://developer.apple.com/reference/avfoundation/avmutablecomposition

    let composition: AVMutableComposition = ...
    let snapshot = composition.copy()
    let playerItem = AVPlayerItem(asset: snapshot)