Last active
October 26, 2015 22:56
-
-
Save ccapndave/4965b3b0dab63aacb74e to your computer and use it in GitHub Desktop.
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
//: Playground - noun: a place where people can play | |
import UIKit | |
import AVFoundation | |
do { | |
let documentDirectoryURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true) | |
let outputURL = documentDirectoryURL.URLByAppendingPathComponent("output.mov") | |
if (NSFileManager.defaultManager().fileExistsAtPath(outputURL.path!)) { | |
try NSFileManager.defaultManager().removeItemAtPath(outputURL.path!) | |
} | |
let audioPath = NSBundle.mainBundle().pathForResource("audio", ofType: "wav") | |
let audioURL:NSURL = NSURL.fileURLWithPath(audioPath!) | |
let audio = AVURLAsset.init(URL: audioURL) | |
let audioAssetTrack = audio.tracksWithMediaType(AVMediaTypeAudio).first! | |
let videoPath = NSBundle.mainBundle().pathForResource("video", ofType: "mp4") | |
let videoURL:NSURL = NSURL.fileURLWithPath(videoPath!) | |
let video = AVURLAsset.init(URL: videoURL) | |
let videoAssetTrack = video.tracksWithMediaType(AVMediaTypeVideo).first! | |
let imagePath = NSBundle.mainBundle().pathForResource("photo", ofType: "jpg") | |
let imageURL:NSURL = NSURL.fileURLWithPath(imagePath!) | |
let composition = AVMutableComposition.init() | |
let compositionAudioTrack = composition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: kCMPersistentTrackID_Invalid) | |
let compositionVideoTrack = composition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid) | |
try compositionAudioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, audioAssetTrack.timeRange.duration), ofTrack: audioAssetTrack, atTime: kCMTimeZero) | |
try compositionVideoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, audioAssetTrack.timeRange.duration), ofTrack: videoAssetTrack, atTime: kCMTimeZero) | |
//let cgImage = UIImage.init(data: NSData.init(contentsOfURL: imageURL)!)!.CGImage | |
//let videoAssetTrack = | |
//try compositionVideoTrack.in | |
//let videoWriter = try AVAssetWriter.init(URL: outputURL, fileType: AVFileTypeMPEG4) | |
let exporter = AVAssetExportSession.init(asset: composition, presetName: AVAssetExportPresetHighestQuality) | |
exporter?.outputURL = outputURL | |
exporter?.outputFileType = AVFileTypeQuickTimeMovie | |
exporter?.shouldOptimizeForNetworkUse = true | |
let group = dispatch_group_create() | |
dispatch_group_enter(group) | |
exporter?.exportAsynchronouslyWithCompletionHandler { | |
dispatch_group_leave(group) | |
} | |
while exporter?.status == .Waiting || exporter?.status == .Exporting { | |
print("Progress: \((exporter?.progress)! * 100.0)%.") | |
dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, Int64(500 * NSEC_PER_MSEC))) | |
} | |
} catch { | |
print("Something went wrong") | |
} | |
/** | |
Oct 26 23:54:01 Merger[5938] <Error>: CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. | |
Oct 26 23:54:01 Merger[5938] <Error>: CGContextTranslateCTM: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. | |
Oct 26 23:54:01 Merger[5938] <Error>: CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. | |
2015-10-26 23:54:01.517 Merger[5938:191866] 23:54:01.516 WARNING: 40: ERROR: couldn't get default input device, ID = 0, err = 0! | |
2015-10-26 23:54:01.523 Merger[5938:191866] 23:54:01.523 WARNING: 803: The default input device 0x0 '(null)' has no input channels. | |
Progress: 0.0%. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment