Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. @import Cocoa;
  2. @import AVFoundation;
  3.  
  4. @interface AppDelegate : NSObject <NSApplicationDelegate>
  5. @end
  6.  
  7. static NSArray * metadataFromAssetDictionary(NSArray *sourceMetadata, NSDictionary *metadataDict)
  8. {
  9. NSMutableDictionary *mutableMetadataDict = [NSMutableDictionary dictionaryWithDictionary:metadataDict];
  10. NSMutableArray *newMetadata = [NSMutableArray array];
  11.  
  12. for (AVMetadataItem *item in sourceMetadata) {
  13. AVMutableMetadataItem *newItem = [item mutableCopy];
  14. NSString *identifier = [newItem identifier];
  15. if (mutableMetadataDict[identifier]) {
  16. newItem.value = mutableMetadataDict[identifier];
  17. [mutableMetadataDict removeObjectForKey:identifier];
  18. }
  19.  
  20. if (newItem.value) {
  21. [newMetadata addObject:newItem];
  22. }
  23. }
  24.  
  25. [mutableMetadataDict enumerateKeysAndObjectsUsingBlock:^(NSString *identifier, id<NSObject, NSCopying> value, BOOL *stop) {
  26. NSString *keySpace = [AVMetadataItem keySpaceForIdentifier:identifier];
  27. AVMutableMetadataItem *newItem = [AVMutableMetadataItem metadataItem];
  28. if ([keySpace isEqual:AVMetadataKeySpaceQuickTimeUserData]) {
  29. newItem.extendedLanguageTag = @"en";
  30. newItem.locale = NSLocale.currentLocale;
  31. } else if ([keySpace isEqual:AVMetadataKeySpaceQuickTimeMetadata]) {
  32. newItem.extendedLanguageTag = @"en-US";
  33. newItem.locale = NSLocale.currentLocale;
  34. }
  35. newItem.identifier = identifier;
  36. newItem.value = value;
  37. [newMetadata addObject:newItem];
  38. }];
  39.  
  40. return newMetadata;
  41. }
  42.  
  43. @implementation AppDelegate
  44.  
  45. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  46. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  47. NSURL *videoInUrl = [NSURL fileURLWithPath:@"<#video in#>" isDirectory:NO];
  48. NSURL *outputDirURL = [NSURL fileURLWithPath:@"<#video output dir#>" isDirectory:YES];
  49.  
  50. // The number of digits is important!
  51. // GPS format: "+00.000000-000.000000+000.000/"
  52. // GPS format without altitude: "+00.000000-000.000000"
  53. // Date format: 0000-00-00T00:00:00-00:00
  54. NSString *location = @"<#GPS location#>";
  55. NSString *date = @"<#ISO 8601 date#>";
  56. NSString *dateModified = @"<#ISO 8601 date#>";
  57.  
  58. // ---
  59.  
  60. NSString *videoOutExt = @"m4v";
  61. NSURL *videoOutURL = [[[outputDirURL URLByAppendingPathComponent:videoInUrl.lastPathComponent isDirectory:NO] URLByDeletingPathExtension] URLByAppendingPathExtension:videoOutExt];
  62. NSString *type = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)videoOutExt, NULL);
  63.  
  64. AVURLAsset *inAsset = [AVURLAsset URLAssetWithURL:videoInUrl options:nil];
  65. NSArray *sourceMetadata = inAsset.metadata;
  66.  
  67. NSDateFormatter *fmt = NSDateFormatter.new;
  68. fmt.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ";
  69. fmt.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  70.  
  71. NSDictionary *appendingMetadata = @{
  72. AVMetadataIdentifierQuickTimeMetadataCreationDate: date,
  73. AVMetadataIdentifierQuickTimeMetadataLocationISO6709: location,
  74. AVMetadataIdentifierQuickTimeUserDataCreationDate: date,
  75. AVMetadataIdentifierQuickTimeUserDataLocationISO6709: location,
  76. };
  77.  
  78. AVAssetExportSession *session = [AVAssetExportSession exportSessionWithAsset:inAsset presetName:AVAssetExportPresetPassthrough];
  79. session.outputURL = videoOutURL;
  80. session.outputFileType = type;
  81. session.metadata = metadataFromAssetDictionary(sourceMetadata, appendingMetadata);
  82. [session exportAsynchronouslyWithCompletionHandler:^{
  83. NSDate *creation = [fmt dateFromString:date];
  84. [videoOutURL setResourceValue:creation forKey:NSURLCreationDateKey error:NULL];
  85.  
  86. NSDate *modified = [fmt dateFromString:dateModified];
  87. [videoOutURL setResourceValue:modified forKey:NSURLContentModificationDateKey error:NULL];
  88.  
  89. [NSApplication.sharedApplication terminate:session];
  90. }];
  91. });
  92. }
  93.  
  94. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement