How to Flip video frame in iOS

1.1k Views Asked by At

I am recording video and saving it in application's document folder.I want to flip the frames of video and save it so that video play in flipped mode like mirrored video.I am not getting how to achieve this. I tried to flip the video but it is saving as blank video. Here is code to flip video

NSURL *assetURL = self.outPutUrl;
    AVAsset *movieAsset = [AVAsset assetWithURL:assetURL];
    NSLog(@"Asset: %0.1f",CMTimeGetSeconds(movieAsset.duration));
    NSLog(@"Asset Preferred Transform %@", NSStringFromCGAffineTransform(movieAsset.preferredTransform));
    //Output Composition
    AVMutableComposition *outputComposition = [[AVMutableComposition alloc] init];
    AVMutableCompositionTrack *videoTrack = [outputComposition addMutableTrackWithMediaType:AVMediaTypeVideo
                                                                           preferredTrackID:kCMPersistentTrackID_Invalid];
    [videoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, movieAsset.duration)
                        ofTrack:[[movieAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]
                         atTime:kCMTimeZero
                          error:nil];

   [videoTrack setPreferredTransform:CGAffineTransformMakeScale(1, -1)];
    AVMutableVideoCompositionLayerInstruction *videoLayerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];
    [videoLayerInstruction setTransform:CGAffineTransformMakeScale(1, -1) atTime:kCMTimeZero];
    AVMutableVideoCompositionInstruction *videoInstruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
    videoInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, movieAsset.duration);
    videoInstruction.layerInstructions = [NSArray arrayWithObjects:videoLayerInstruction, nil];
    AVMutableVideoComposition *outputVideoComposition = [AVMutableVideoComposition videoComposition];
    outputVideoComposition.instructions = [NSArray arrayWithObjects:videoInstruction, nil];
    outputVideoComposition.frameDuration = CMTimeMake(1, 30);
    outputVideoComposition.renderSize = CGSizeMake(480, 640);
    //Export

    AVAssetExportSession *exporter = [[AVAssetExportSession alloc] initWithAsset:outputComposition presetName:AVAssetExportPresetHighestQuality] ;
    exporter.videoComposition = outputVideoComposition;
    NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [filePath objectAtIndex:0];
    NSString *newPath =  [documentsDirectory stringByAppendingPathComponent:
                             [NSString stringWithFormat:@"FinalVideo-%d.mov",arc4random() % 1000]];
    [[NSFileManager defaultManager] removeItemAtPath:newPath error:nil];
    exporter.outputURL = [NSURL fileURLWithPath:newPath];
    exporter.outputFileType = AVFileTypeQuickTimeMovie;
    NSLog(@"Starting export%@",exporter.outputURL);
    [exporter exportAsynchronouslyWithCompletionHandler:^(void){
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Video exported");
            NSLog(@"%@", newPath);
            NSURL *newAssetURL = [NSURL fileURLWithPath:newPath];
            AVAsset *newMovieAsset = [AVAsset assetWithURL:newAssetURL];
       NSLog(@"New Asset %@",newMovieAsset);
       NSLog(@"New Asset Duration: %0.1f",CMTimeGetSeconds(newMovieAsset.duration));
       NSLog(@"New Asset Preferred Transform %@", NSStringFromCGAffineTransform(newMovieAsset.preferredTransform));
            UISaveVideoAtPathToSavedPhotosAlbum(newPath, nil, nil, nil);


        });
                       }];

Please help! Thanks!

1

There are 1 best solutions below

0
On

Check this link.. I think you want to reverse the video clip.. It worked for me..