Skip to content

Instantly share code, notes, and snippets.

@xaviervia
Created August 30, 2013 23:38
Show Gist options
  • Save xaviervia/6395279 to your computer and use it in GitHub Desktop.
Save xaviervia/6395279 to your computer and use it in GitHub Desktop.
How to effectibly use AVAsset and AVAssetExportSession to convert files between .mov and .m4v on Mac OS X using the "User Selected File Read/Write Access" entitlement
  • Don't forget to link the (IBAction)doSelect: to some kind of button otherwise nothing will happen.
//
// UNRAppDelegate.h
//
#import <Cocoa/Cocoa.h>
#import <AVFoundation/AVFoundation.h>
@interface UNRAppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSButton *select;
-(IBAction)doSelect:(id)sender;
-(AVAsset*)getAsset:(NSString*)folder;
@end
//
// UNRAppDelegate.m
//
#import "UNRAppDelegate.h"
@implementation UNRAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
}
-(IBAction)doSelect:(id)sender
{
// Create a File Open Dialog class.
NSOpenPanel* dialog = [NSOpenPanel openPanel];
// Setup options in the dialog.
[dialog setCanChooseFiles:NO];
[dialog setCanChooseDirectories:YES];
[dialog setAllowsMultipleSelection:NO];
if ([dialog runModal] == NSOKButton) {
// Gets list of all files selected
NSArray *files = [dialog URLs];
// Get the asset
NSString* aFolder = [files objectAtIndex:0];
AVAsset* asset = [self getAsset:aFolder];
// List compatible presets for the asset
NSArray* compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:asset];
NSLog(@"%@", compatiblePresets);
// Is the asset readable?
if ([asset isReadable]) {
NSLog(@"The asset is readable, ok.");
}
else {
NSLog(@"Bad news. You should probably perform a different action");
}
// Is the m4v SD compatible with the current asset?
if ([compatiblePresets containsObject:AVAssetExportPresetAppleM4V480pSD]) {
// Then, start the export session
NSLog(@"Starting export session");
AVAssetExportSession* session = [AVAssetExportSession exportSessionWithAsset:asset
presetName:AVAssetExportPresetAppleM4V480pSD];
NSString *outputFile = [NSString stringWithFormat:@"%@FILENAME.m4v", aFolder];
NSURL *outputURL = [NSURL URLWithString:outputFile];
session.outputURL = outputURL;
session.outputFileType = AVFileTypeAppleM4V;
NSLog(@"%@", session);
[session exportAsynchronouslyWithCompletionHandler:^{
switch ([session status]) {
case AVAssetExportSessionStatusFailed:
NSLog(@"Export failed: %@", [[session error] localizedDescription]);
NSLog(@"%@", [[session error] localizedFailureReason]);
NSLog(@"%@", [[session error] localizedRecoveryOptions]);
NSLog(@"%@", [[session error] localizedRecoverySuggestion]);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
default:
NSLog(@"Export succeded");
break;
}
}];
}
// If it is not compatible!
else {
// TODO should raise exception and send exception mail
}
}
}
-(AVAsset*)getAsset:(NSString*)folder
{
// filePath
NSString *filePath = [NSString stringWithFormat:@"%@FILENAME.mov", folder];
NSURL *url = [NSURL URLWithString:filePath];
// Build asset
return [AVURLAsset URLAssetWithURL:url options:nil];
}
@end
@xaviervia
Copy link
Author

Not pictured: how to read the folder in search for files. I leave that to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment