iphone - How to fix video orientation issue in iOS -
i working app in user picks video photos , uploads server. server .net server , video gets rotated. know reason of problem same in case of image (you may refer earlier answer https://stackoverflow.com/a/10601175/1030951 ) , googled , got code fix video orientation , got code raywenderlich.com , modified in following way. output video works fine video mute. plays doesn't play audio. kindly me if missing something.
i pass info dictionary of -(void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info
method
- (void)fix:(nsdictionary*)pobjinfodirectory withfilename:(nsstring*)pstroutputfilename { firstasset = [avasset assetwithurl:[pobjinfodirectory objectforkey:uiimagepickercontrollermediaurl]]; if(firstasset !=nil) { //create avmutablecomposition object.this object hold our multiple avmutablecompositiontrack. avmutablecomposition* mixcomposition = [[avmutablecomposition alloc] init]; //video track avmutablecompositiontrack *firsttrack = [mixcomposition addmutabletrackwithmediatype:avmediatypevideo preferredtrackid:kcmpersistenttrackid_invalid]; [firsttrack inserttimerange:cmtimerangemake(kcmtimezero, firstasset.duration) oftrack:[[firstasset trackswithmediatype:avmediatypevideo] objectatindex:0] attime:kcmtimezero error:nil]; avmutablevideocompositioninstruction * maininstruction = [avmutablevideocompositioninstruction videocompositioninstruction]; maininstruction.timerange = cmtimerangemake(kcmtimezero, firstasset.duration); //fixing orientation// avmutablevideocompositionlayerinstruction *firstlayerinstruction = [avmutablevideocompositionlayerinstruction videocompositionlayerinstructionwithassettrack:firsttrack]; avassettrack *firstassettrack = [[firstasset trackswithmediatype:avmediatypevideo] objectatindex:0]; uiimageorientation firstassetorientation_ = uiimageorientationup; bool isfirstassetportrait_ = no; cgaffinetransform firsttransform = firstassettrack.preferredtransform; if(firsttransform.a == 0 && firsttransform.b == 1.0 && firsttransform.c == -1.0 && firsttransform.d == 0) { firstassetorientation_= uiimageorientationright; isfirstassetportrait_ = yes; } if(firsttransform.a == 0 && firsttransform.b == -1.0 && firsttransform.c == 1.0 && firsttransform.d == 0) { firstassetorientation_ = uiimageorientationleft; isfirstassetportrait_ = yes; } if(firsttransform.a == 1.0 && firsttransform.b == 0 && firsttransform.c == 0 && firsttransform.d == 1.0) { firstassetorientation_ = uiimageorientationup; } if(firsttransform.a == -1.0 && firsttransform.b == 0 && firsttransform.c == 0 && firsttransform.d == -1.0) { firstassetorientation_ = uiimageorientationdown; } cgfloat firstassetscaletofitratio = 320.0/firstassettrack.naturalsize.width; if(isfirstassetportrait_) { firstassetscaletofitratio = 320.0/firstassettrack.naturalsize.height; cgaffinetransform firstassetscalefactor = cgaffinetransformmakescale(firstassetscaletofitratio,firstassetscaletofitratio); [firstlayerinstruction settransform:cgaffinetransformconcat(firstassettrack.preferredtransform, firstassetscalefactor) attime:kcmtimezero]; } else { cgaffinetransform firstassetscalefactor = cgaffinetransformmakescale(firstassetscaletofitratio,firstassetscaletofitratio); [firstlayerinstruction settransform:cgaffinetransformconcat(cgaffinetransformconcat(firstassettrack.preferredtransform, firstassetscalefactor),cgaffinetransformmaketranslation(0, 160)) attime:kcmtimezero]; } [firstlayerinstruction setopacity:0.0 attime:firstasset.duration]; maininstruction.layerinstructions = [nsarray arraywithobjects:firstlayerinstruction,nil];; avmutablevideocomposition *maincompositioninst = [avmutablevideocomposition videocomposition]; maincompositioninst.instructions = [nsarray arraywithobject:maininstruction]; maincompositioninst.frameduration = cmtimemake(1, 30); maincompositioninst.rendersize = cgsizemake(320.0, 480.0); nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; nsstring *mypathdocs = [documentsdirectory stringbyappendingpathcomponent:[nsstring stringwithformat:@"mergevideo-%d.mov",arc4random() % 1000]]; nsurl *url = [nsurl fileurlwithpath:mypathdocs]; avassetexportsession *exporter = [[avassetexportsession alloc] initwithasset:mixcomposition presetname:avassetexportpresethighestquality]; exporter.outputurl=url; exporter.outputfiletype = avfiletypequicktimemovie; exporter.videocomposition = maincompositioninst; exporter.shouldoptimizefornetworkuse = yes; [exporter exportasynchronouslywithcompletionhandler:^ { dispatch_async(dispatch_get_main_queue(), ^{ [self exportdidfinish:exporter]; }); }]; } } - (void)exportdidfinish:(avassetexportsession*)session { if(session.status == avassetexportsessionstatuscompleted) { nsurl *outputurl = session.outputurl; alassetslibrary *library = [[alassetslibrary alloc] init]; if ([library videoatpathiscompatiblewithsavedphotosalbum:outputurl]) { if ([self.delegate respondstoselector:@selector(videoexported)]) [self.delegate videoexported]; } } firstasset = nil; }
add after //video track
part
//audio track avmutablecompositiontrack *firstaudiotrack = [mixcomposition addmutabletrackwithmediatype:avmediatypeaudio preferredtrackid:kcmpersistenttrackid_invalid]; [firstaudiotrack inserttimerange:cmtimerangemake(kcmtimezero, firstasset.duration) oftrack:[[firstasset trackswithmediatype:avmediatypeaudio] objectatindex:0] attime:kcmtimezero error:nil];
Comments
Post a Comment