Changing Recorded Video Orientation: A Step-by-Step Guide for iOS and macOS Developers

Changing Recorded Video Orientation

=====================================================

In this article, we’ll explore the process of changing the orientation of a recorded video from landscape mode to portrait mode permanently. We’ll dive into the world of iOS and macOS video handling, including the AVURLAsset class and its properties.

Background


When you record a video on an iOS or macOS device, it’s stored in the device’s document directory as a .mov file. By default, this file is in landscape mode (width > height). However, when you want to share the video on social media platforms like Facebook or Twitter, you might encounter issues with the video’s orientation being displayed in portrait mode.

To ensure that your videos are always shown in the correct orientation, regardless of how they’re shared, you need to change their metadata programmatically. This involves creating an AVURLAsset object, setting its properties, and then updating the file’s metadata.

Technical Overview


The process of changing video orientation involves several steps:

  1. Get the video file path from the document directory.
  2. Create an AVURLAsset object with the video file URL.
  3. Set the AVURLAssetPreferPreciseDurationAndTimingKey property to YES, which indicates that you want to use precise duration and timing for the asset.
  4. Set the AVURLAssetReferenceRestrictionsKey property to a value that allows you to change the video’s orientation.
  5. Update the file’s metadata using the AVAssetExportSession class.

Code Example


Here’s an example code snippet in Objective-C that demonstrates how to change the orientation of a recorded video:

-(IBAction)changeMovieToPortrait {
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsPath = [paths objectAtIndex:0];
    NSString* movieFile1 = [documentsPath stringByAppendingPathComponent:@"movie1.mov"];

    NSURL *movURL = [NSURL fileURLWithPath:movieFile1];

    NSMutableDictionary* myDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES] ,
                                   AVURLAssetPreferPreciseDurationAndTimingKey ,
                                   [NSNumber numberWithInt:0],
                                   AVURLAssetReferenceRestrictionsKey, nil];

    AVURLAsset* movie = [[AVURLAsset alloc] initWithURL:movURL options:myDict];

    // Create an asset export session
    AVAssetExportSession* exportSession = [[AVAssetExportSession alloc] initWithAsset:movie
                                                                      presetName:kAVPresetVideo360P];

    // Set the reference restrictions to allow orientation changes
    AVAssetTrack* videoTrack = [movie.tracks[0] objectAtIndex:0];
    videoTrack.preferedTransform = CATransform3DMakeRotation(M_PI_2, 1, 0, 0);

    // Update the file's metadata
    [exportSession setOutputURL:[NSURL fileURLWithPath:movieFile1]];
}

This code creates an AVURLAsset object with the video file URL and sets its properties using the myDict dictionary. It then creates an asset export session, sets the reference restrictions to allow orientation changes, and updates the file’s metadata.

Working with AVURLAsset


The AVURLAsset class represents a media asset that’s associated with a URL. It provides various properties and methods for accessing and manipulating the asset’s metadata.

Here are some key concepts and properties you should be familiar with when working with AVURLAsset:

  • assetUrl: The URL associated with the asset.
  • tracks: An array of tracks that make up the asset (e.g., video, audio).
  • preferredTransform: The preferred transformation for the asset’s media data.

To access these properties and more, you can use various methods on the AVURLAsset class, such as:

  • initWithURL:options:: Initializes an AVURLAsset object with a URL and options dictionary.
  • tracks: Returns an array of tracks that make up the asset.
  • preferredTransform: Returns the preferred transformation for the asset’s media data.

Working with AVAssetExportSession


The AVAssetExportSession class represents a session used to export an asset as a new file or stream. It provides various properties and methods for configuring the export process.

Here are some key concepts and properties you should be familiar with when working with AVAssetExportSession:

  • asset: The asset being exported.
  • presetName: The preset name used to determine the export settings.
  • outputURL: The URL where the exported file will be written.

To access these properties and more, you can use various methods on the AVAssetExportSession class, such as:

  • initWithAsset:, presetName:: Initializes an AVAssetExportSession object with an asset and preset name.
  • outputURL: Returns the URL where the exported file will be written.

Best Practices


When working with video orientation changes, keep in mind the following best practices:

  • Use precise duration and timing for your assets to ensure accurate playback.
  • Set reference restrictions to allow orientation changes when exporting videos.
  • Update the file’s metadata using asset export sessions to ensure consistent playback.

By following these guidelines and using the AVURLAsset and AVAssetExportSession classes, you can change recorded video orientations with ease.


Last modified on 2024-04-14