- Don't forget to link the
(IBAction)doSelect:
to some kind of button otherwise nothing will happen.
Created
August 30, 2013 23:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not pictured: how to read the folder in search for files. I leave that to you.