Skip to content

Instantly share code, notes, and snippets.

@thetzel
Created February 19, 2012 17:43
Show Gist options
  • Save thetzel/1864805 to your computer and use it in GitHub Desktop.
Save thetzel/1864805 to your computer and use it in GitHub Desktop.
Parse Markers from Audition Session File to Chapter List
- (BOOL)application:(NSApplication*)app openFile:(NSString*)file{
NSData *data = [[NSData alloc] initWithContentsOfFile:file];
NSString *query = @"/sesx/session/xmpMetadata";
NSArray *result = PerformXMLXPathQuery(data, query);
NSString *xmpQuery = @"/x:xmpmeta/rdf:RDF/rdf:Description/xmpDM:Tracks/rdf:Bag/rdf:li/xmpDM:markers/rdf:Seq";
NSString *xmpMetadataString = [[[[result objectAtIndex:0] objectForKey:@"nodeChildArray"] objectAtIndex:0] objectForKey:@"nodeContent"];
NSData *xmpMetadataData = [xmpMetadataString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *xmpResult = PerformXMLXPathQuery(xmpMetadataData, xmpQuery);
NSArray *markerDict = [[xmpResult objectAtIndex:0] objectForKey:@"nodeChildArray"];
NSString *chapterName = nil;
NSString *chapterStart = nil;
NSMutableArray *chapters = [[NSMutableArray alloc] init];
for (id chapter in markerDict) {
NSArray *chapterArray = [chapter objectForKey:@"nodeChildArray"];
for (id chapterArrayDetail in chapterArray) {
if ([[chapterArrayDetail objectForKey:@"nodeName"] isEqualToString:@"startTime"]){
NSString *startTimeString = [chapterArrayDetail objectForKey:@"nodeContent"];
if([startTimeString isEqualToString:@"0"]){
chapterStart = @"0";
}
else{
NSArray *chunks = [startTimeString componentsSeparatedByString:@"f"];
chapterStart = [NSString stringWithFormat:@"%f", [[chunks objectAtIndex:0] floatValue] / [[chunks objectAtIndex:1] floatValue] ];
}
}
if ([[chapterArrayDetail objectForKey:@"nodeName"] isEqualToString:@"name"]){
chapterName = [chapterArrayDetail objectForKey:@"nodeContent"];
}
}
NSDictionary * newChapter = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:chapterStart, chapterName, nil] forKeys:[NSArray arrayWithObjects:@"chapterStart", @"chapterName", nil]];
[chapters addObject:newChapter];
}
NSMutableString *chapterText = [[NSMutableString alloc] init];
for (id chapter in chapters) {
chapterStart = [chapter objectForKey:@"chapterStart"];
int chapterStartInt = [chapterStart intValue];
float chapterStartFloat = [chapterStart floatValue];
int decimals = ( chapterStartFloat - floor(chapterStartFloat) )* 1000;
chapterName = [chapter objectForKey:@"chapterName"];
[chapterText appendFormat:@"%02u:%02u:%02u.%03u %@\n", chapterStartInt / 3600, (chapterStartInt / 60) % 60, chapterStartInt % 60, decimals, chapterName];
}
NSError *error = nil;
[chapterText writeToFile:[[[[file stringByDeletingPathExtension] stringByAppendingString:@"."] stringByAppendingString:@"chapter"] stringByAppendingPathExtension:@"txt"] atomically:YES encoding:NSUTF8StringEncoding error:&error];
return YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment