2013-04-20 20 views
12

Pracuję z aplikacją, w której użytkownik wybiera wideo ze Zdjęć i przesyła je na serwer. Ponieważ moim serwerem jest serwer .Net, wideo zostaje obrócone. Wiem, że przyczyna problemu jest prawdopodobnie taka sama jak w przypadku obrazu (możesz odwołać się do mojej wcześniejszej odpowiedzi https://stackoverflow.com/a/10601175/1030951), więc szukałem go i otrzymałem kod do naprawy orientacja wideo, dostałem kod z RayWenderlich.com i zmodyfikowałem podążać drogą. Teraz moje wideo wyjściowe działa dobrze, ale wideo jest wyciszone. odtwarza, ale nie odtwarza dźwięku. Uprzejmie mi pomóż, jeśli czegoś brakuje.Jak naprawić problem z orientacją wideo w iOS

mijam słownika informacją -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info metody

- (void)fix:(NSDictionary*)pobjInfoDirectory withFileName:(NSString*)pstrOutputFileName 
{ 
    firstAsset = [AVAsset assetWithURL:[pobjInfoDirectory objectForKey:UIImagePickerControllerMediaURL]]; 


    if(firstAsset !=nil) 
    { 
     //Create AVMutableComposition Object.This object will 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; 
} 
+1

Jesteś pewien, że to nie twój telefon iPhone jest wyciszony? – Peres

+0

Tak, grałem w aplikację ze zdjęciami, grałem z dźwiękiem .. – HarshIT

+0

sprawdź, czy to nie jest możliwe http://stackoverflow.com/questions/2814626/programatically-access-orientation-of-an-iphone-video –

Odpowiedz

8

dodać to po //VIDEO TRACK części

 //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]; 
+0

Dzięki, to naprawdę działa ....... – HarshIT

+0

czy to będzie działać również dla wideo? mam na myśli mam problem z orientacją połączonych filmów po zastosowaniu transformacji. Każda pomoc jest doceniana. –

+0

@AbdulYasin znajdziesz rozwiązanie problemu Orientation po połączeniu ??? jeśli tak, możesz dać mi sugestię – Nilesh

1
AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:@"your video url here..." options:nil]; 

Dodaj Po AVMutableCompositionTrack.

zestaw setPreferredTransform: ustaw tutaj źródło wideo, które chcesz wyeksportować z tą samą orientacją.

Powiązane problemy