Issue with setting and retrieving the value of AVM

2019-03-26 01:02发布

问题:

I have a video recording app for the iPhone. I am using AVAssetWriter for writing the recorded video data to a file. I am also interested in embedding custom metadata to the file. For eg: I want to identify my application as the creator of the video.

Hence after creating the asset writer, I am adding the creator value using the key AVMetadataCommonKeyCreator to the metadata of the asset writer.

Code snippet is as follows:

AVAssetWriter *assetWrtr = [[AVAssetWriter alloc] initWithURL:inURL fileType:AVFileTypeQuickTimeMovie error:&error];
self.assetWriter = assetWrtr;
[assetWrtr release];

NSArray *existingMetadataArray = self.assetWriter.metadata;
NSMutableArray *newMetadataArray = nil;
if (existingMetadataArray) 
{
    newMetadataArray = [existingMetadataArray mutableCopy]; // To prevent overriding of existing metadata
}
else 
{
    newMetadataArray = [[NSMutableArray alloc] init];
}

AVMutableMetadataItem *item = [[AVMutableMetadataItem alloc] init];
item.keySpace = AVMetadataKeySpaceCommon;
item.key = AVMetadataCommonKeyCreator;
item.value = @"My App";

[newMetadataArray addObject:item];
self.assetWriter.metadata = newMetadataArray;
[newMetadataArray release];
[item release];

Once the recording is done, I am trying to read the contents of the file using AVURLAsset.

NSURL *outputFileURL = [self.assetWriter outputURL];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:outputFileURL options:nil];
NSArray *keys = [NSArray arrayWithObject:@"commonMetadata"];

[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^(void) {

    NSError *error = nil;
    AVKeyValueStatus durationStatus = [asset statusOfValueForKey:@"commonMetadata" error:&error];
    switch (durationStatus) {
        case AVKeyValueStatusLoaded:
            NSLog(@"AVKeyValueStatusLoaded");
            NSLog(@"commonMetadata:%@", [asset commonMetadata]);
            break;
        case AVKeyValueStatusFailed:
            NSLog(@"AVKeyValueStatusFailed");
            break;
        case AVKeyValueStatusCancelled:
            NSLog(@"AVKeyValueStatusCancelled");
            break;
    }
}];

However [asset commonMetadata] does not return any AVMetadataItems. It returns an empty array. I am able to set the values of other meta data objects like AVMetadataCommonKeyTitle, AVMetadataCommonKeyModel, AVMetadataCommonKeySource. These values are retrieved properly. The problem is only with AVMetadataCommonKeyCreator. Please let me know the reason for this behavior. Is it not possible to set app specific value for AVMetadataCommonKeyCreator? What does this key actually represent?

回答1:

I've same problems about one year ago. I didn't see any metadata when I've wrote audio file using AVFoundation. I think that AVMetadataCommonKeyCreator doesn't work. I can't say about now but it was so.

I solve this problem by using C library taglib http://taglib.github.com it's really easy to build this library for iOS. I think you can delete some files if you can't compile it.



回答2:

I would also like to confirm that AVMetadataCommonKeyCreator will not be saved to the metadata of a video's asset. The other ones I have tried (AVMetadataCommonKeyTitle, AVMetadataCommonKeyDescription, etc) do work. My base SDK is iOS 8.2.

Is there something special about the creator key? Is Apple reserving it for some other use? Are there other keys that are restricted to us (or just don't work)?

( I have not had the luxury of time to test every one of the 23 AVMetadataCommonKeys.. )