2017-03-02 59 views
0

我在iPhone中錄製了一個視頻,我正在獲取其本地文件URL。有什麼方法可以使用URL將視頻旋轉90度?在iOS中通過90度編程旋轉錄制的視頻

+0

@ Anbu.Karthik我沒有得到任何想法,該怎麼辦呢 –

+0

你如何加載你的視頻AVplayer或MPMoviePlayerViewController –

+0

我必須將它與其他視頻合併並將其發送到服務器 –

回答

1

您可以使用AVMutableVideoCompositionLayerInstruction類在導出會話之前旋轉視頻,並且您可以在其上應用Transform

這是方法

[yourlayerInstruction setTransform:CGAffineTransformMakeRotation(M_PI/2) atTime:firstAssets.duration]; 

這裏是全面實施

  AVMutableComposition *mixComposition = [[AVMutableComposition alloc] init]; 

      AVMutableCompositionTrack *firstTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo 
                       preferredTrackID:kCMPersistentTrackID_Invalid]; 


      AVMutableVideoCompositionInstruction * mainInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction]; 
      mainInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, CMTimeAdd(firstAsset.duration, secondAsset.duration)); 

      AVMutableVideoCompositionLayerInstruction *firstlayerInstruction = [AVMutableVideoCompositionLayerInstruction 
    videoCompositionLayerInstructionWithAssetTrack:firstTrack]; 

// set Trasform Here 
    [firstlayerInstruction setTransform:CGAffineTransformMakeRotation(M_PI/2) 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); 

     AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:mixComposition 
                       presetName:AVAssetExportPreset640x480]; 
      exporter.outputURL=url; 
      exporter.videoComposition=mainCompositionInst; 
      exporter.outputFileType = AVFileTypeQuickTimeMovie; 
      exporter.shouldOptimizeForNetworkUse = YES; 
      [exporter exportAsynchronouslyWithCompletionHandler:^{ 
       dispatch_async(dispatch_get_main_queue(),^
       { 
        [self exportDidFinish:exporter]; 
       }); 
      }]; 

我希望這將幫助你

+0

它工作正常 –

0

如果使用MPMoviePlayerViewController播放視頻: - 那你可以試試這個, 首先從捆綁使用以下行提取您的網址,

NSURL *fileUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"your video" ofType:@"mp4"]]; 

或者你也可以直接在mpmovieplayerviewcontroller使用本地文件的網址

然後創建MPmovieplayerview控制器的對象,並使用您的視頻文件,如下圖所示: -

MPMoviePlayerViewController *moviePlayerController = [[MPMoviePlayerViewController alloc]initWithContentURL:fileUrl]; 
    [moviePlayerController.moviePlayer prepareToPlay]; 
    moviePlayerController.view.transform = CGAffineTransformMakeRotation(M_PI/2); 
    [self.view addSubview:moviePlayerController.view]; 

希望它有幫助!

+0

那麼這隻會在播放時旋轉視頻,他希望它在輸出視頻到服務器時進行旋轉。 – iphonic