Raman Srivastava
Raman Srivastava

Reputation: 396

Rotate recorded video by 90 degree programmatically in iOS

I have recorded a video in iphone and I am getting its local file URL. Is there any way to rotate that video by 90 degree using the URL?

Upvotes: 0

Views: 1594

Answers (2)

Dhiru
Dhiru

Reputation: 3060

You can rotate the Video before exporting session using AVMutableVideoCompositionLayerInstruction class and you can apply Transform on it .

This is the method

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

here is the full implementation

          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];
                });
            }];

i hope this will help you

Upvotes: 3

Zalak Patel
Zalak Patel

Reputation: 1965

If you are using MPMoviePlayerViewController for playing video:- Then you can try this, First fetch your url from bundle using below line,

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

Or you can use your local file url directly in mpmovieplayerviewcontroller

Then create MPmovieplayerview controller's object and use your video file as shown below :-

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

Hope it helps!

Upvotes: -2

Related Questions