Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @import Cocoa;
- @import AVFoundation;
- @interface AppDelegate : NSObject <NSApplicationDelegate>
- @end
- static NSArray * metadataFromAssetDictionary(NSArray *sourceMetadata, NSDictionary *metadataDict)
- {
- NSMutableDictionary *mutableMetadataDict = [NSMutableDictionary dictionaryWithDictionary:metadataDict];
- NSMutableArray *newMetadata = [NSMutableArray array];
- for (AVMetadataItem *item in sourceMetadata) {
- AVMutableMetadataItem *newItem = [item mutableCopy];
- NSString *identifier = [newItem identifier];
- if (mutableMetadataDict[identifier]) {
- newItem.value = mutableMetadataDict[identifier];
- [mutableMetadataDict removeObjectForKey:identifier];
- }
- if (newItem.value) {
- [newMetadata addObject:newItem];
- }
- }
- [mutableMetadataDict enumerateKeysAndObjectsUsingBlock:^(NSString *identifier, id<NSObject, NSCopying> value, BOOL *stop) {
- NSString *keySpace = [AVMetadataItem keySpaceForIdentifier:identifier];
- AVMutableMetadataItem *newItem = [AVMutableMetadataItem metadataItem];
- if ([keySpace isEqual:AVMetadataKeySpaceQuickTimeUserData]) {
- newItem.extendedLanguageTag = @"en";
- newItem.locale = NSLocale.currentLocale;
- } else if ([keySpace isEqual:AVMetadataKeySpaceQuickTimeMetadata]) {
- newItem.extendedLanguageTag = @"en-US";
- newItem.locale = NSLocale.currentLocale;
- }
- newItem.identifier = identifier;
- newItem.value = value;
- [newMetadata addObject:newItem];
- }];
- return newMetadata;
- }
- @implementation AppDelegate
- - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- NSURL *videoInUrl = [NSURL fileURLWithPath:@"<#video in#>" isDirectory:NO];
- NSURL *outputDirURL = [NSURL fileURLWithPath:@"<#video output dir#>" isDirectory:YES];
- // The number of digits is important!
- // GPS format: "+00.000000-000.000000+000.000/"
- // GPS format without altitude: "+00.000000-000.000000"
- // Date format: 0000-00-00T00:00:00-00:00
- NSString *location = @"<#GPS location#>";
- NSString *date = @"<#ISO 8601 date#>";
- NSString *dateModified = @"<#ISO 8601 date#>";
- // ---
- NSString *videoOutExt = @"m4v";
- NSURL *videoOutURL = [[[outputDirURL URLByAppendingPathComponent:videoInUrl.lastPathComponent isDirectory:NO] URLByDeletingPathExtension] URLByAppendingPathExtension:videoOutExt];
- NSString *type = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)videoOutExt, NULL);
- AVURLAsset *inAsset = [AVURLAsset URLAssetWithURL:videoInUrl options:nil];
- NSArray *sourceMetadata = inAsset.metadata;
- NSDateFormatter *fmt = NSDateFormatter.new;
- fmt.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ";
- fmt.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
- NSDictionary *appendingMetadata = @{
- AVMetadataIdentifierQuickTimeMetadataCreationDate: date,
- AVMetadataIdentifierQuickTimeMetadataLocationISO6709: location,
- AVMetadataIdentifierQuickTimeUserDataCreationDate: date,
- AVMetadataIdentifierQuickTimeUserDataLocationISO6709: location,
- };
- AVAssetExportSession *session = [AVAssetExportSession exportSessionWithAsset:inAsset presetName:AVAssetExportPresetPassthrough];
- session.outputURL = videoOutURL;
- session.outputFileType = type;
- session.metadata = metadataFromAssetDictionary(sourceMetadata, appendingMetadata);
- [session exportAsynchronouslyWithCompletionHandler:^{
- NSDate *creation = [fmt dateFromString:date];
- [videoOutURL setResourceValue:creation forKey:NSURLCreationDateKey error:NULL];
- NSDate *modified = [fmt dateFromString:dateModified];
- [videoOutURL setResourceValue:modified forKey:NSURLContentModificationDateKey error:NULL];
- [NSApplication.sharedApplication terminate:session];
- }];
- });
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement