views:

34

answers:

0

Hi,

Anybody know how to generate chapter metadata in Mac OS X? (preferably Cocoa). I've tried to create an M4A file by concatenating two AIFF files. The M4A was generated fine but the chapter marks are all gone.

Here is my prototype code. Is there anything wrong with it?

-(void) generatePodcast {
    NSLog(@"Generating...");
    NSBundle* const mainBundle = [NSBundle mainBundle];
    NSString* firstFile = [mainBundle pathForResource:@"first" ofType:@"aiff"];
    NSString* secondFile = [mainBundle pathForResource:@"second" ofType:@"aiff"];
    NSString* outputFile = @"/Users/adib/Downloads/output.m4a";

    NSMutableArray* chapters = [NSMutableArray arrayWithCapacity:2];

    NSError* error = nil;
    NSMutableData* outputData = [NSMutableData data];
    QTMovie* outputMovie = [[[QTMovie alloc] initToWritableData:outputData error:&error] autorelease];
    if (error) {
        NSLog(@"Error initializing blank movie: %@",error);
    }
    error = nil;
    QTMovie* firstMovie = [QTMovie movieWithDataReference:[QTDataReference dataReferenceWithReferenceToFile:firstFile] error:&error];
    if (error) {
        NSLog(@"Error creating first movie: %@",error);
    }

    error = nil;
    QTMovie* secondMovie = [QTMovie movieWithDataReference:[QTDataReference dataReferenceWithReferenceToFile:secondFile] error:&error];
    if (error) {
        NSLog(@"Error creating second movie: %@",error);
    }
    NSDictionary* currentChapter = nil;

    QTTime currentTime = QTMakeTimeWithTimeInterval(0);

    currentChapter = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"First Chapter",QTMovieChapterName,
                      [NSValue valueWithQTTime:currentTime],QTMovieChapterStartTime,
                      nil
                      ];
    [chapters addObject:currentChapter];
    [firstMovie setSelection:QTMakeTimeRange(QTMakeTimeWithTimeInterval(0), [firstMovie duration])];
    [outputMovie appendSelectionFromMovie:firstMovie];

    currentTime = QTTimeIncrement(currentTime, [firstMovie duration]);
    currentChapter = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"Second Chapter",QTMovieChapterName,
                      [NSValue valueWithQTTime:currentTime],QTMovieChapterStartTime,
                      nil
                      ];
    [chapters addObject:currentChapter];
    [secondMovie setSelection:QTMakeTimeRange(QTMakeTimeWithTimeInterval(0), [secondMovie duration])];
    [outputMovie appendSelectionFromMovie:secondMovie];

    error = nil;

    [outputMovie addChapters:chapters withAttributes:nil error:&error];
    if (error) {
        NSLog(@"Error adding chapters: %@",error);
    }

    NSDictionary* exportAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [NSNumber numberWithBool:YES],QTMovieExport,
                                      [NSNumber numberWithLong:kQTFileTypeMP4],QTMovieExportType,
                                      nil];
    error = nil;
    if ([outputMovie writeToFile:outputFile withAttributes:exportAttributes error:&error]) {
        NSLog(@"Write success");
        error = nil;
        QTMovie* movie = [QTMovie movieWithFile:outputFile error:&error];

        NSInteger numChapters = [movie chapterCount];
        NSLog(@"Number of Chapters: %d", numChapters);

        NSArray* chapterArray = [movie chapters];
        for ( NSDictionary* chapDict in chapterArray )
        {
            NSLog(@"%@", [chapDict objectForKey:@"QTMovieChapterName"] );
        }
    } 
    if (error) {
        NSLog(@"Write failed. error: %@",error);
    }

}

The output when I ran it was:

2010-06-24 21:59:36.259 PodcastMakerTest[1676:a0f] Generating...
2010-06-24 21:59:38.334 PodcastMakerTest[1676:a0f] Write success
2010-06-24 21:59:38.354 PodcastMakerTest[1676:a0f] Number of Chapters: 0

As you can see for yourself, the chapter markers are all gone. How do I programmatically generate an AAC file with chapter markers so that the chapter markers can be used in an iPod or iPhone?

Thanks.