ios - Recording video using AVFoundation Swift -
i created avcapturesession , manipulate each frame (morphing) users face,adding layers etc. how can turn frames video can saved camera roll?
here's how set avcapturesession
func setupcapture() { let session : avcapturesession = avcapturesession() session.sessionpreset = avcapturesessionpreset640x480 let device : avcapturedevice = avcapturedevice.defaultdevicewithmediatype(avmediatypevideo) let deviceinput : avcapturedeviceinput = try! avcapturedeviceinput(device: device) if session.canaddinput(deviceinput) { session.addinput(deviceinput) } stillimageoutput = avcapturestillimageoutput() videodataoutput = avcapturevideodataoutput() let rgboutputsettings = [kcvpixelbufferpixelformattypekey string: nsnumber(unsignedint: kcmpixelformat_32bgra)] videodataoutput.videosettings = rgboutputsettings videodataoutput.alwaysdiscardslatevideoframes = true videodataoutputqueue = dispatch_queue_create("videodataoutputqueue", dispatch_queue_serial) videodataoutput.setsamplebufferdelegate(self, queue: videodataoutputqueue) if session.canaddoutput(videodataoutput) { session.addoutput(videodataoutput) } videodataoutput.connectionwithmediatype(avmediatypevideo).enabled = false effectivescale = 1.0 previewlayer = avcapturevideopreviewlayer(session: session) previewlayer.backgroundcolor = uicolor.blackcolor().cgcolor previewlayer.videogravity = avlayervideogravityresizeaspect let rootlayer : calayer = previewview.layer rootlayer.maskstobounds = true previewlayer.frame = rootlayer.bounds rootlayer.addsublayer(previewlayer) session.startrunning() }
than use cmsamplebuffer
ciimage
add effects to.
func captureoutput(captureoutput: avcaptureoutput!, didoutputsamplebuffer samplebuffer: cmsamplebuffer!, fromconnection connection: avcaptureconnection!) { let pixelbuffer : cvpixelbufferref = cmsamplebuffergetimagebuffer(samplebuffer)! let attachments : cfdictionaryref = cmcopydictionaryofattachments(kcfallocatordefault, pixelbuffer, cmattachmentmode( kcmattachmentmode_shouldpropagate))! let ciimage : ciimage = ciimage(cvpixelbuffer: pixelbuffer, options: attachments as? [string : anyobject])
how can record video doing this?
i found solution problem. working in swift , had same question. i'm thankful posted question because helped me lot. able process frames , write them camera roll. have not perfected it, possible.
it turns out once start messing around avcapturedataoutput allegedly lose ability use avcapturemoviefileoutput see discussion here.
a concise solution problem can found here. version implemented. feed samplebuffer avassetwriterinput object.
a more verbose guide solving problem describes parts (i.e. avassetwriter, avassetwriterinput , outsettings etc..) needed , like.
the links posted in obj-c can translated swift. hope have solved problem.
Comments
Post a Comment