Skip to content

Instantly share code, notes, and snippets.

@Limon-O-O
Created August 1, 2016 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Limon-O-O/f327af04d8a3e28f5997949e86d12869 to your computer and use it in GitHub Desktop.
Save Limon-O-O/f327af04d8a3e28f5997949e86d12869 to your computer and use it in GitHub Desktop.
Merge AVAsset
//
// Segment.swift
// MED
//
// Created by Limon on 6/28/16.
// Copyright © 2016 MED. All rights reserved.
//
import AVFoundation
func appendTrack(track: AVAssetTrack, toCompositionTrack compositionTrack: AVMutableCompositionTrack, atTime time: CMTime, withBounds bounds: CMTime) -> CMTime {
var timeRange = track.timeRange
let time = CMTimeAdd(time, timeRange.start)
if (CMTIME_IS_VALID(bounds)) {
let currentBounds = CMTimeAdd(time, timeRange.duration)
if (currentBounds > bounds) {
timeRange = CMTimeRangeMake(timeRange.start, CMTimeSubtract(timeRange.duration, CMTimeSubtract(currentBounds, bounds)));
}
}
if timeRange.duration > kCMTimeZero {
do {
try compositionTrack.insertTimeRange(timeRange, ofTrack: track, atTime: time)
// print("Inserted %@ at %fs (%fs -> %fs)", track.mediaType, CMTimeGetSeconds(time), CMTimeGetSeconds(timeRange.start), CMTimeGetSeconds(timeRange.duration))
} catch let error as NSError {
print("Failed to insert append \(compositionTrack.mediaType) track: \(error.localizedDescription)")
}
return CMTimeAdd(time, timeRange.duration);
}
return time
}
func assetRepresentingSegments(URLs: [NSURL]) -> AVAsset {
if URLs.count == 1 {
let URL = URLs.first!
return AVAsset(URL: URL)
} else {
let composition = AVMutableComposition()
appendSegmentsToComposition(composition, URLs: URLs)
return composition
}
}
func appendSegmentsToComposition(composition: AVMutableComposition, URLs: [NSURL]) {
var audioTrack: AVMutableCompositionTrack? = nil
var videoTrack: AVMutableCompositionTrack? = nil
var currentTime = composition.duration
for (_, URL) in URLs.enumerate() {
let asset = AVAsset(URL: URL)
let audioAssetTracks = asset.tracksWithMediaType(AVMediaTypeAudio)
let videoAssetTracks = asset.tracksWithMediaType(AVMediaTypeVideo)
var maxBounds = kCMTimeInvalid
var videoTime = currentTime
for (_, videoAssetTrack) in videoAssetTracks.enumerate() {
if (videoTrack == nil) {
let videoTracks = composition.tracksWithMediaType(AVMediaTypeVideo)
if (videoTracks.count > 0) {
videoTrack = videoTracks.first
} else {
videoTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid)
videoTrack?.preferredTransform = videoAssetTrack.preferredTransform
}
}
videoTime = appendTrack(videoAssetTrack, toCompositionTrack: videoTrack!, atTime: videoTime, withBounds: maxBounds)
maxBounds = videoTime
}
var audioTime = currentTime
for (_, audioAssetTrack) in audioAssetTracks.enumerate() {
if audioTrack == nil {
let audioTracks = composition.tracksWithMediaType(AVMediaTypeAudio)
if (audioTracks.count > 0) {
audioTrack = audioTracks.first
} else {
audioTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid)
}
audioTime = appendTrack(audioAssetTrack, toCompositionTrack: audioTrack!, atTime: audioTime, withBounds: maxBounds)
}
}
currentTime = composition.duration
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment