Skip to content

Instantly share code, notes, and snippets.

@gonzo-oin
Created April 1, 2019 16:53
Show Gist options
  • Save gonzo-oin/b300dc7391f83bddd9bcd60aae4614b7 to your computer and use it in GitHub Desktop.
Save gonzo-oin/b300dc7391f83bddd9bcd60aae4614b7 to your computer and use it in GitHub Desktop.
AVAsset with separate video and audio urls (iOS)
let videoAsset = AVAsset(url: videoStreamURL)
var duration: CMTime!
if (CMTimeGetSeconds(audioAsset.duration) < CMTimeGetSeconds(videoAsset.duration)) {
duration = audioAsset.duration;
} else {
duration = videoAsset.duration;
}
var compo = AVMutableComposition()
let track = compo.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)
try? track?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: duration), of: audioAsset.tracks[0], at: CMTime.zero)
let trackVideo = compo.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)
try? trackVideo?.insertTimeRange(CMTimeRangeMake(start: CMTime.zero, duration: duration), of: videoAsset.tracks[0], at: CMTime.zero)
let playerItem = AVPlayerItem(asset: compo)
let playerVC = AVPlayerViewController.init()
playerVC.player = AVPlayer(playerItem: playerItem)
present(this.playerVC, animated: true, completion: nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment