Created
April 1, 2019 16:53
-
-
Save gonzo-oin/b300dc7391f83bddd9bcd60aae4614b7 to your computer and use it in GitHub Desktop.
AVAsset with separate video and audio urls (iOS)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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