0

Since updating to iOS 13 my video composition I use to fade a video in and out is broken. This is my code which worked correctly up until installing iOS 13.

Now when I export the video there is sound and just a black screen.

let urlAsset = AVURLAsset(url: inputURL, options: nil)      
guard let exportSession = AVAssetExportSession(asset: urlAsset, presetName: AVAssetExportPresetHighestQuality) else { handler(nil)
     return
}
exportSession.outputURL = outputURL
exportSession.outputFileType = AVFileType.m4v
exportSession.shouldOptimizeForNetworkUse = true

let composition = AVMutableVideoComposition(propertiesOf: urlAsset)

let clipVideoTrack = urlAsset.tracks(withMediaType: AVMediaType.video)[0]

let timeDuration = CMTimeMake(value: 1, timescale: 1)

let layer = AVMutableVideoCompositionLayerInstruction(assetTrack: clipVideoTrack)

// MARK: Fade in effect
layer.setOpacityRamp(fromStartOpacity: 0.0, toEndOpacity: 1.0, timeRange: CMTimeRange(start: CMTime.zero, duration: timeDuration))

// MARK: Fade out effect
let startTime = CMTimeSubtract(urlAsset.duration, CMTimeMake(value: 1, timescale: 1))

layer.setOpacityRamp(
       fromStartOpacity: 1.0,
       toEndOpacity: 0.0,
       timeRange: CMTimeRangeMake(start: startTime, duration: timeDuration)
)

let instruction = AVMutableVideoCompositionInstruction()
instruction.layerInstructions = [layer]
instruction.timeRange = CMTimeRange(start: CMTime.zero, duration: urlAsset.duration)


composition.instructions = [instruction]

exportSession.videoComposition = composition

exportSession.exportAsynchronously { () -> Void in
       handler(exportSession)
       print("composition has completed")
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
Matthew Foster
  • 167
  • 1
  • 10
  • So looking through apple developer forum I read there is a bug affecting setTransformRamp so I am assuming it also affects setOpacityRamp. It is suppose to be fixed in iOS 13.1 which is being released Sept 30th 2019. – Matthew Foster Sep 24 '19 at 14:19

1 Answers1

0

Apple said there was a bug affecting some instructions for compositions. This was fixed in iOS 13.1. I updated and ran the function and the fade in and out worked as it was before iOS 13 update.

Matthew Foster
  • 167
  • 1
  • 10
  • 1
    Is this still working for you? I can’t get opacity instructions to work reliably on 13.3.1... – Dan Halliday Mar 09 '20 at 20:59
  • Is it working for you guys with iOS 13.4? My opacity ramp instructions are completely ignored. @DanHalliday – Erik Apr 15 '20 at 19:58
  • @Matthew can you share the source? – Erik Apr 15 '20 at 19:58
  • I eventually realised AVComposition playback is extremely buggy in the simulator. I had (as usual) a compounding bug that made it fail on device, but once I fixed that I realised it was a simulator issue. – Dan Halliday Apr 21 '20 at 09:08