ios - Why don't I get video when exporting a movie using AVAssetExportSession? -
it trimming audio, video blank.
this function initiates trimming
- (void)viewdidload { [super viewdidload]; // additional setup after loading view. self.view.backgroundcolor = [uicolor blackcolor]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(finishedtrimming:) name:@"videofinishedtrimming" object:nil]; nsurl *furl = [nsurl fileurlwithpath:[nstemporarydirectory() stringbyappendingpathcomponent:@"capture0.mp4"]]; //[self playvideowithurl:furl]; //play original video //play trimmed video cmtime starttrim = cmtimemake(0, 1); cmtime endtrim = cmtimemake(2,1); cmtimerange exporttimerange = cmtimerangefromtimetotime(starttrim, endtrim); [processinghelper trimassetwithurl:furl andrange:exporttimerange]; }
this function exports , trims video.
+(void)trimassetwithurl:(nsurl *)urlin andrange:(cmtimerange)timerangein { avasset *videoasset = [avasset assetwithurl:urlin]; //creates session videoasset avassetexportsession *exportsession = [[avassetexportsession alloc] initwithasset:videoasset presetname:avassetexportpresethighestquality]; //creates path export - saving temporary directory nsstring* filename = [nsstring stringwithformat:@"trimmedcapture%d.mp4", 0]; nsstring* path = [nstemporarydirectory() stringbyappendingpathcomponent:filename]; //checks if there file @ output url. session not overwrite previous data if ([[nsfilemanager defaultmanager] fileexistsatpath:path]) { nslog(@"removing item @ path: %@", path); [[nsfilemanager defaultmanager] removeitematpath:path error:nil]; } //set output url exportsession.outputurl = [nsurl fileurlwithpath:path]; //set output file type exportsession.outputfiletype = avfiletypempeg4; //avfiletypeac3; // avfiletypempeglayer3; // avfiletypewave; // avfiletypequicktimemovie; exportsession.timerange = timerangein; exportsession.metadata = nil; //exports! [exportsession exportasynchronouslywithcompletionhandler:^{ switch (exportsession.status) { case avassetexportsessionstatuscompleted:{ nslog(@"export complete"); nsdictionary *options = [nsdictionary dictionarywithobjectsandkeys:exportsession.outputurl, @"outputurl", nil]; [[nsnotificationcenter defaultcenter] postnotificationname:@"videofinishedtrimming" object:self userinfo:options]; break; } case avassetexportsessionstatusfailed: nslog(@"export error: %@", [exportsession.error description]); break; case avassetexportsessionstatuscancelled: nslog(@"export cancelled"); break; default: break; } }]; exportsession = nil; }
this function initiates playing of video
-(void)finishedtrimming:(nsnotification *)notification { nsdictionary *userinfo = notification.userinfo; nsurl *outputurl = [userinfo objectforkey:@"outputurl"]; [self playvideowithurl:outputurl]; }
this function plays video
-(void)playvideowithurl:(nsurl *)furl { nsdata *moviedata; nserror *datareadingerror = nil; moviedata = [nsdata datawithcontentsofurl: furl options:nsdatareadingmapped error:&datareadingerror]; if(moviedata != nil) nslog(@"successfully loaded data."); else nslog(@"failed load data error = %@", datareadingerror); //avplayer self.avplayer = [avplayer playerwithurl:furl]; avplayerlayer *avplayerlayer = [avplayerlayer playerlayerwithplayer:self.avplayer]; avplayerlayer.frame = self.vplaybackmovie.bounds; avplayerlayer.videogravity = avlayervideogravityresizeaspectfill; avplayerlayer.needsdisplayonboundschange = yes; [self.vplaybackmovie.layer addsublayer:avplayerlayer]; self.vplaybackmovie.layer.needsdisplayonboundschange = yes; [self.avplayer play]; }
thanks chrish, right! export taking place on thread in handler need main queue...
i needed main thread after
case avassetexportsessionstatuscompleted:{ dispatch_async(dispatch_get_main_queue(), ^{ //post notification! }); break; }
Comments
Post a Comment