Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flip a video using AVFoundation

I've recorded a video with the front facing camera and the output is mirrored...

I've tried using AVMutablecomposition and layerinstructions to flip the video but no luck.

Googling and searching Stack Overflow has been fruitless so I bet a simple, straight forward example of how to do this is something that would benefit many.

like image 409
PinkFloydRocks Avatar asked Dec 10 '15 11:12

PinkFloydRocks


3 Answers

Swift 5. AVCaptureSession:

let movieFileOutput = AVCaptureMovieFileOutput()
let connection = movieFileOutput.connection(with: .video)

if connection?.isVideoMirroringSupported ?? false {
    connection?.isVideoMirrored = true
}

Same for PhotoOutput.

like image 31
atereshkov Avatar answered Nov 13 '22 00:11

atereshkov


Theres no indication on what you are using to record the video, ill assume AVCaptureSession + AVCaptureVideoDataOutput

lazy var videoFileOutput: AVCaptureVideoDataOutput = AVCaptureVideoDataOutput()
let v = videoFileOutput.connectionWithMediaType(AVMediaTypeVideo)
v.videoOrientation = .Portrait
v.videoMirrored = true
like image 145
Sean Lintern Avatar answered Nov 13 '22 00:11

Sean Lintern


You can use -[AVMutableVideoCompositionLayerInstruction setTransform:atTime:]

CGAffineTransform transform = CGAffineTransformMakeTranslation(self.config.videoSize, 0);
transform = CGAffineTransformScale(transform, -1.0, 1.0);
[videoCompositionLayerInstruction setTransform:transform atTime:videoTime];

// then append video tracks
// [compositionTrack insertTimeRange:timeRange ofTrack:track atTime:atTime error:&error];

// apply instructions
videoCompositionInstruction.timeRange = CMTimeRangeMake(kCMTimeZero, composition.duration);
videoCompositionInstruction.layerInstructions = @[videoCompositionLayerInstruction];

videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.renderSize = CGSizeMake(self.config.videoSize, self.config.videoSize);
videoComposition.frameDuration = CMTimeMake(1, self.config.videoFrameRate);
videoComposition.instructions = @[videoCompositionInstruction];

https://github.com/ElfSundae/AVDemo/tree/ef2ca437d0d8dcb3dd41c5a272c8754a29d8a936/AVSimpleEditoriOS

Export composition:

AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:presetName];
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.outputURL = outputURL;
exportSession.shouldOptimizeForNetworkUse = YES;
// videoComposition contains transform instructions for video tracks
exportSession.videoComposition = videoComposition;
// audioMix contains background music for audio tracks
exportSession.audioMix = audioMix;

[exportSession exportAsynchronouslyWithCompletionHandler:^{
    AVAssetExportSessionStatus status = exportSession.status;
    if (status != AVAssetExportSessionStatusCompleted) {
        // exportSession.error
    } else {
        // exportSession.outputURL
    }
}];
like image 7
Elf Sundae Avatar answered Nov 13 '22 01:11

Elf Sundae