Objective c : fastest method to overlay a video -
after recording video, want overlay dynamic uiview updated related current video frame timestamp. in fact trying same thing vidometer application. following apple example, able extract video frame buffer , overlay uiimage of uiview.
the steps :
extract video frame:
cmsamplebufferref samplebuffer = [assetreadervideooutput copynextsamplebuffer]; if (cancelled) { completedorfailed = yes; [assetwritervideoinput markasfinished]; } cvpixelbufferref pixelbuffer = cmsamplebuffergetimagebuffer(samplebuffer);
update uiview's subviews following frame timestamp:
mergetime = cmtimegetseconds(cmsamplebuffergetpresentationtimestamp(samplebuffer)); [self updatewidget:mergetime*1000];
get uiimage of uiview:
msurcoucheimage = [self imagefromsurcouche];
with
-(uiimage*)imagefromsurcouche{ cgsize msize; msize = cgsizemake(self.msurcouche.bounds.size.width, self.msurcouche.bounds.size.height); uigraphicsbeginimagecontextwithoptions(msize, no, 0.0); if (videoorientation == uiinterfaceorientationlandscaperight) { cgcontexttranslatectm(uigraphicsgetcurrentcontext(), msize.width, msize.height); cgcontextrotatectm(uigraphicsgetcurrentcontext(), m_pi); } [self.msurcouche.layer renderincontext:uigraphicsgetcurrentcontext()]; uiimage* image = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return image; }
apply filter keep alpha of uiimage:
cvpixelbufferlockbaseaddress(pixelbuffer, 0); inputimage = [ciimage imagewithcvpixelbuffer:pixelbuffer options:options]; filter = [cifilter filterwithname:@"cisourceovercompositing"]; [filter setvalue:maskimage forkey:kciinputimagekey]; [filter setvalue:inputimage forkey:kciinputbackgroundimagekey]; outputimage = [filter outputimage]; cvpixelbufferunlockbaseaddress(pixelbuffer, 0);
with
colorspace = cgcolorspacecreatedevicergb(); options = [nsdictionary dictionarywithobject:(__bridge id)colorspace forkey:kciimagecolorspace];
render uiimage video frame buffer
[cicontext render:outputimage tocvpixelbuffer:pixelbuffer bounds:[outputimage extent] colorspace:cgcolorspacecreatedevicergb()];
with
eaglcontext = [[eaglcontext alloc] initwithapi:keaglrenderingapiopengles2]; cicontext = [cicontext contextwitheaglcontext:eaglcontext options:@{kcicontextworkingcolorspace : [nsnull null]}]; if (eaglcontext != [eaglcontext currentcontext]) [eaglcontext setcurrentcontext:eaglcontext];
i trying reach same performance vidometer application have overlay duration less video duration, far method.
question 1: method best performing?
question 2: see method use avmutablecomposition, don't think can synchronize uiview video frame timestamps. can i?
Comments
Post a Comment