lundi 29 juin 2015

No change to video with applied CGAffineTransform's

I'm currently developing an app for the iPhone using PBJVision in Objective-C and am trying to save a video in different orientations depending on the orientation in which it was taken.

I am currently using the writeVideoAtPathToSavedPhotosAlbum: completionBlock: method to save the photo to the iPhones photo album.

[self.library writeVideoAtPathToSavedPhotosAlbum: videoPathURL
                                 completionBlock:^(NSURL* assetURL, NSError* error) {

                                     //Calling here
                                     [self encodeVideoOrientation:assetURL];//testing

                                     //readableURL = assetURL;//test
                                     if (error.code == 0) {

                                         // try to get the asset
                                         [self.library assetForURL:assetURL
                                                       resultBlock:^(ALAsset *asset) {
                                                           // assign the video to the album

                                                       }
                                                      failureBlock:^(NSError* error) {
                                                          NSLog(@"Failed to save video to library");
                                                      }];
                                     } else {
                                     }
                                 }];

I'm passing the assetURL to a function encodeVideoOrientation with the intention of overwriting the file with the rotated video.

- (void)encodeVideoOrientation:(NSURL *)anOutputFileURL
{
CGAffineTransform rotationTransform;
CGAffineTransform transformToApply = videoAsset.preferredTransform;
CGSize renderSize;
CGFloat newWidth;
CGFloat newHeight;
float currentVideoRotation;

AVURLAsset * videoAsset = [[AVURLAsset alloc]initWithURL:anOutputFileURL options:nil];

AVAssetTrack *sourceVideoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
AVAssetTrack *sourceAudioTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];

CGSize size = [sourceVideoTrack naturalSize];

if (self.currentOrientationByAccelerometer == LANDSCAPE_RIGHT){
    //setting currentVideoRotation, newWidth, and newHeight
} else if (self.currentOrientationByAccelerometer == LANDSCAPE_LEFT){
    //setting currentVideoRotation, newWidth, and newHeight
} else { //default to portrait
    //setting currentVideoRotation, newWidth, and newHeight
}

renderSize = CGSizeMake(newWidth, newHeight);
rotationTransform = CGAffineTransformMakeRotation(currentVideoRotation);    

AVMutableComposition* composition = [AVMutableComposition composition];

AVMutableCompositionTrack *compositionVideoTrack = [composition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                               ofTrack:sourceVideoTrack
                                atTime:kCMTimeZero error:nil];
[compositionVideoTrack setPreferredTransform:rotationTransform]; //Rotations here?

AVMutableCompositionTrack *compositionAudioTrack = [composition addMutableTrackWithMediaType:AVMediaTypeAudio
            preferredTrackID:kCMPersistentTrackID_Invalid];
[compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration)
                               ofTrack:sourceAudioTrack
                                atTime:kCMTimeZero error:nil];

AVMutableVideoCompositionInstruction *instruction = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
AVMutableVideoCompositionLayerInstruction *layerInstruction = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:compositionVideoTrack];
[layerInstruction setTransform:transformToApply atTime:kCMTimeZero]; //Rotations here?

AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.frameDuration = CMTimeMake(1,30);
videoComposition.renderScale = 1.0;
videoComposition.renderSize = renderSize;
instruction.layerInstructions = [NSArray arrayWithObject: layerInstruction];
instruction.timeRange = CMTimeRangeMake(kCMTimeZero, videoAsset.duration);
videoComposition.instructions = [NSArray arrayWithObject: instruction];

AVAssetExportSession * assetExport = [[AVAssetExportSession alloc] initWithAsset:composition
                                                                      presetName:AVAssetExportPresetMediumQuality];

NSString* videoName = @"export.mov";
NSString *anOutputFileURLPath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName];

NSURL * exportUrl = [NSURL fileURLWithPath:anOutputFileURLPath];

if ([[NSFileManager defaultManager] fileExistsAtPath:anOutputFileURLPath])
{
    [[NSFileManager defaultManager] removeItemAtPath:anOutputFileURLPath error:nil];
}


assetExport.outputFileType = AVFileTypeMPEG4;
assetExport.outputURL = exportUrl;
assetExport.shouldOptimizeForNetworkUse = YES;
assetExport.videoComposition = videoComposition;

[assetExport exportAsynchronouslyWithCompletionHandler:
 ^(void ) {
     switch (assetExport.status)
     {
         case AVAssetExportSessionStatusCompleted:
             //                export complete
             NSLog(@"Export Complete");
             break;
         case AVAssetExportSessionStatusFailed:
             NSLog(@"Export Failed");
             NSLog(@"ExportSessionError: %@", [assetExport.error localizedDescription]);
             //                export error (see exportSession.error)
             break;
         case AVAssetExportSessionStatusCancelled:
             NSLog(@"Export Cancelled");
             NSLog(@"ExportSessionError: %@", [assetExport.error localizedDescription]);
             //                export cancelled
             break;
     }
 }];

}

The export is succeeding, however, there seems to be no change to the video regardless of the set CGAffineTransform's.

I based the code for encodeVideoOrientation from a solution in this SO post.

iOS AVFoundation: Setting Orientation of Video

Any thoughts?

Aucun commentaire:

Enregistrer un commentaire