Read media tags from AVFoundation and some cosmetics
This commit is contained in:
1 parent
2aa920bd5a
commit
7861f4d6e6
1 file changed
+91
-47
+91
-47
@@ -25,9 +25,7 @@
|
|||||||
@interface AVFDemuxHelper : NSObject {
|
@interface AVFDemuxHelper : NSObject {
|
||||||
@public
|
@public
|
||||||
int64_t currentTime;
|
int64_t currentTime;
|
||||||
int64_t delta;
|
|
||||||
AVAssetReaderOutput *assetReaderOutput;
|
AVAssetReaderOutput *assetReaderOutput;
|
||||||
|
|
||||||
MP42EditListsReconstructor *editsConstructor;
|
MP42EditListsReconstructor *editsConstructor;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,11 +44,7 @@
|
|||||||
|
|
||||||
- (NSString *)formatForTrack:(AVAssetTrack *)track {
|
- (NSString *)formatForTrack:(AVAssetTrack *)track {
|
||||||
NSString *result = @"";
|
NSString *result = @"";
|
||||||
|
CMFormatDescriptionRef formatDescription = (CMFormatDescriptionRef)track.formatDescriptions.firstObject;
|
||||||
CMFormatDescriptionRef formatDescription = NULL;
|
|
||||||
NSArray *formatDescriptions = track.formatDescriptions;
|
|
||||||
if ([formatDescriptions count] > 0)
|
|
||||||
formatDescription = (CMFormatDescriptionRef)[formatDescriptions objectAtIndex:0];
|
|
||||||
|
|
||||||
if (formatDescription) {
|
if (formatDescription) {
|
||||||
FourCharCode code = CMFormatDescriptionGetMediaSubType(formatDescription);
|
FourCharCode code = CMFormatDescriptionGetMediaSubType(formatDescription);
|
||||||
@@ -114,6 +108,9 @@
|
|||||||
case kCMTextFormatType_3GText:
|
case kCMTextFormatType_3GText:
|
||||||
result = MP42SubtitleFormatTx3g;
|
result = MP42SubtitleFormatTx3g;
|
||||||
break;
|
break;
|
||||||
|
case kCMSubtitleFormatType_WebVTT:
|
||||||
|
result = MP42SubtitleFormatWebVTT;
|
||||||
|
break;
|
||||||
case 'SRT ':
|
case 'SRT ':
|
||||||
result = MP42SubtitleFormatText;
|
result = MP42SubtitleFormatText;
|
||||||
break;
|
break;
|
||||||
@@ -172,8 +169,6 @@
|
|||||||
|
|
||||||
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError {
|
- (instancetype)initWithURL:(NSURL *)fileURL error:(NSError **)outError {
|
||||||
if ((self = [super initWithURL:fileURL])) {
|
if ((self = [super initWithURL:fileURL])) {
|
||||||
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
||||||
|
|
||||||
_localAsset = [[AVAsset assetWithURL:self.fileURL] retain];
|
_localAsset = [[AVAsset assetWithURL:self.fileURL] retain];
|
||||||
|
|
||||||
NSArray<AVAssetTrack *> *tracks = [_localAsset tracks];
|
NSArray<AVAssetTrack *> *tracks = [_localAsset tracks];
|
||||||
@@ -197,18 +192,18 @@
|
|||||||
|
|
||||||
// Converts the tracks to the MP42File types
|
// Converts the tracks to the MP42File types
|
||||||
for (AVAssetTrack *track in tracks) {
|
for (AVAssetTrack *track in tracks) {
|
||||||
|
|
||||||
MP42Track *newTrack = nil;
|
MP42Track *newTrack = nil;
|
||||||
|
|
||||||
// Retrieves the formatDescription
|
// Retrieves the formatDescription
|
||||||
CMFormatDescriptionRef formatDescription = NULL;
|
|
||||||
NSArray *formatDescriptions = track.formatDescriptions;
|
NSArray *formatDescriptions = track.formatDescriptions;
|
||||||
if ([formatDescriptions count] > 0)
|
CMFormatDescriptionRef formatDescription = (CMFormatDescriptionRef)formatDescriptions.firstObject;
|
||||||
formatDescription = (CMFormatDescriptionRef)[formatDescriptions objectAtIndex:0];
|
|
||||||
|
if ([track.mediaType isEqualToString:AVMediaTypeVideo]) {
|
||||||
|
|
||||||
if ([[track mediaType] isEqualToString:AVMediaTypeVideo]) {
|
|
||||||
// Video type, do the usual video things
|
// Video type, do the usual video things
|
||||||
MP42VideoTrack *videoTrack = [[MP42VideoTrack alloc] init];
|
MP42VideoTrack *videoTrack = [[MP42VideoTrack alloc] init];
|
||||||
CGSize naturalSize = [track naturalSize];
|
CGSize naturalSize = track.naturalSize;
|
||||||
|
|
||||||
videoTrack.trackWidth = naturalSize.width;
|
videoTrack.trackWidth = naturalSize.width;
|
||||||
videoTrack.trackHeight = naturalSize.height;
|
videoTrack.trackHeight = naturalSize.height;
|
||||||
@@ -217,12 +212,14 @@
|
|||||||
videoTrack.height = naturalSize.height;
|
videoTrack.height = naturalSize.height;
|
||||||
|
|
||||||
if (formatDescription) {
|
if (formatDescription) {
|
||||||
|
|
||||||
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
|
CMVideoDimensions dimensions = CMVideoFormatDescriptionGetDimensions(formatDescription);
|
||||||
videoTrack.width = dimensions.width;
|
videoTrack.width = dimensions.width;
|
||||||
videoTrack.height = dimensions.height;
|
videoTrack.height = dimensions.height;
|
||||||
|
|
||||||
// Reads the pixel aspect ratio information
|
// Reads the pixel aspect ratio information
|
||||||
CFDictionaryRef pixelAspectRatioFromCMFormatDescription = CMFormatDescriptionGetExtension(formatDescription, kCMFormatDescriptionExtension_PixelAspectRatio);
|
CFDictionaryRef pixelAspectRatioFromCMFormatDescription = CMFormatDescriptionGetExtension(formatDescription, kCMFormatDescriptionExtension_PixelAspectRatio);
|
||||||
|
|
||||||
if (pixelAspectRatioFromCMFormatDescription) {
|
if (pixelAspectRatioFromCMFormatDescription) {
|
||||||
NSInteger hSpacing, vSpacing;
|
NSInteger hSpacing, vSpacing;
|
||||||
CFNumberGetValue(CFDictionaryGetValue(pixelAspectRatioFromCMFormatDescription, kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing), kCFNumberIntType, &hSpacing);
|
CFNumberGetValue(CFDictionaryGetValue(pixelAspectRatioFromCMFormatDescription, kCMFormatDescriptionKey_PixelAspectRatioHorizontalSpacing), kCFNumberIntType, &hSpacing);
|
||||||
@@ -230,8 +227,10 @@
|
|||||||
videoTrack.hSpacing = hSpacing;
|
videoTrack.hSpacing = hSpacing;
|
||||||
videoTrack.vSpacing = vSpacing;
|
videoTrack.vSpacing = vSpacing;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reads the clean aperture information
|
// Reads the clean aperture information
|
||||||
CFDictionaryRef cleanApertureFromCMFormatDescription = CMFormatDescriptionGetExtension(formatDescription, kCMFormatDescriptionExtension_CleanAperture);
|
CFDictionaryRef cleanApertureFromCMFormatDescription = CMFormatDescriptionGetExtension(formatDescription, kCMFormatDescriptionExtension_CleanAperture);
|
||||||
|
|
||||||
if (cleanApertureFromCMFormatDescription) {
|
if (cleanApertureFromCMFormatDescription) {
|
||||||
double cleanApertureWidth, cleanApertureHeight;
|
double cleanApertureWidth, cleanApertureHeight;
|
||||||
double cleanApertureHorizontalOffset, cleanApertureVerticalOffset;
|
double cleanApertureHorizontalOffset, cleanApertureVerticalOffset;
|
||||||
@@ -255,7 +254,10 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
newTrack = videoTrack;
|
newTrack = videoTrack;
|
||||||
} else if ([[track mediaType] isEqualToString:AVMediaTypeAudio]) {
|
|
||||||
|
}
|
||||||
|
else if ([track.mediaType isEqualToString:AVMediaTypeAudio]) {
|
||||||
|
|
||||||
// Audio type, check the channel layout and channels number
|
// Audio type, check the channel layout and channels number
|
||||||
MP42AudioTrack *audioTrack = [[MP42AudioTrack alloc] init];
|
MP42AudioTrack *audioTrack = [[MP42AudioTrack alloc] init];
|
||||||
|
|
||||||
@@ -266,47 +268,84 @@
|
|||||||
if (layoutSize) {
|
if (layoutSize) {
|
||||||
audioTrack.channels = AudioChannelLayoutTag_GetNumberOfChannels(layout->mChannelLayoutTag);
|
audioTrack.channels = AudioChannelLayoutTag_GetNumberOfChannels(layout->mChannelLayoutTag);
|
||||||
audioTrack.channelLayoutTag = layout->mChannelLayoutTag;
|
audioTrack.channelLayoutTag = layout->mChannelLayoutTag;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
audioTrack.channels = 1;
|
audioTrack.channels = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
newTrack = audioTrack;
|
newTrack = audioTrack;
|
||||||
} else if ([[track mediaType] isEqualToString:AVMediaTypeSubtitle]) {
|
|
||||||
|
}
|
||||||
|
else if ([track.mediaType isEqualToString:AVMediaTypeSubtitle]) {
|
||||||
|
|
||||||
// Subtitle type, nothing interesting here
|
// Subtitle type, nothing interesting here
|
||||||
newTrack = [[MP42SubtitleTrack alloc] init];
|
newTrack = [[MP42SubtitleTrack alloc] init];
|
||||||
} else if ([[track mediaType] isEqualToString:AVMediaTypeText]) {
|
|
||||||
|
}
|
||||||
|
else if ([track.mediaType isEqualToString:AVMediaTypeText]) {
|
||||||
|
|
||||||
// It looks like there is no way to know what text track is used for chapters in the original file.
|
// It looks like there is no way to know what text track is used for chapters in the original file.
|
||||||
if (chapters) {
|
if (chapters) {
|
||||||
newTrack = chapters;
|
newTrack = chapters;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
newTrack = [[MP42ChapterTrack alloc] init];
|
newTrack = [[MP42ChapterTrack alloc] init];
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
// Unknown type
|
// Unknown type
|
||||||
newTrack = [[MP42Track alloc] init];
|
newTrack = [[MP42Track alloc] init];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the usual track properties
|
// Set the usual track properties
|
||||||
newTrack.format = [self formatForTrack:track];
|
newTrack.format = [self formatForTrack:track];
|
||||||
newTrack.Id = [track trackID];
|
newTrack.Id = track.trackID;
|
||||||
newTrack.sourceURL = self.fileURL;
|
newTrack.sourceURL = self.fileURL;
|
||||||
newTrack.dataLength = [track totalSampleDataLength];
|
newTrack.dataLength = track.totalSampleDataLength;
|
||||||
|
|
||||||
|
NSArray<AVMetadataItem *> *trackMetadata = [track metadataForFormat:AVMetadataFormatQuickTimeUserData];
|
||||||
|
|
||||||
// "name" is undefined in AVMetadataFormat.h, so read the official track name "tnam", and then "name". On 10.7, "name" is returned as an NSData
|
// "name" is undefined in AVMetadataFormat.h, so read the official track name "tnam", and then "name". On 10.7, "name" is returned as an NSData
|
||||||
NSArray *trackMetadata = [track metadataForFormat:AVMetadataFormatQuickTimeUserData];
|
NSString *trackName = [[[AVMetadataItem metadataItemsFromArray:trackMetadata
|
||||||
id trackName = [[[AVMetadataItem metadataItemsFromArray:trackMetadata withKey:AVMetadataQuickTimeUserDataKeyTrackName keySpace:nil] lastObject] value];
|
withKey:AVMetadataQuickTimeUserDataKeyTrackName
|
||||||
id trackName_oldFormat = [[[AVMetadataItem metadataItemsFromArray:trackMetadata withKey:@"name" keySpace:nil] lastObject] value];
|
keySpace:nil] firstObject] stringValue];
|
||||||
if (trackName && [trackName isKindOfClass:[NSString class]]) {
|
|
||||||
|
if (trackName.length) {
|
||||||
newTrack.name = trackName;
|
newTrack.name = trackName;
|
||||||
} else if (trackName_oldFormat && [trackName_oldFormat isKindOfClass:[NSString class]]) {
|
}
|
||||||
newTrack.name = trackName_oldFormat;
|
else {
|
||||||
} else if (trackName_oldFormat && [trackName_oldFormat isKindOfClass:[NSData class]]) {
|
id trackName_oldFormat = [[[AVMetadataItem metadataItemsFromArray:trackMetadata
|
||||||
newTrack.name = [NSString stringWithCString:[trackName_oldFormat bytes] encoding:NSMacOSRomanStringEncoding];
|
withKey:@"name"
|
||||||
|
keySpace:nil] firstObject] value];
|
||||||
|
|
||||||
|
if ([trackName_oldFormat isKindOfClass:[NSString class]]) {
|
||||||
|
newTrack.name = trackName_oldFormat;
|
||||||
|
}
|
||||||
|
else if ([trackName_oldFormat isKindOfClass:[NSData class]]) {
|
||||||
|
newTrack.name = [NSString stringWithCString:[trackName_oldFormat bytes]
|
||||||
|
encoding:NSMacOSRomanStringEncoding];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
newTrack.language = [self langForTrack:track];
|
newTrack.language = [self langForTrack:track];
|
||||||
|
|
||||||
CMTimeRange timeRange = [track timeRange];
|
// Media characteristic tags, requires 10.10 or later
|
||||||
|
if ([[AVMetadataItem class] respondsToSelector:@selector(metadataItemsFromArray:filteredByIdentifier:)]) {
|
||||||
|
|
||||||
|
NSArray<AVMetadataItem *> *mediaTags = [AVMetadataItem metadataItemsFromArray:trackMetadata
|
||||||
|
filteredByIdentifier:AVMetadataIdentifierQuickTimeUserDataTaggedCharacteristic];
|
||||||
|
|
||||||
|
if (mediaTags.count) {
|
||||||
|
NSMutableSet<NSString *> *tags = [NSMutableSet set];
|
||||||
|
|
||||||
|
for (AVMetadataItem *tag in mediaTags) {
|
||||||
|
[tags addObject:tag.stringValue];
|
||||||
|
}
|
||||||
|
newTrack.mediaCharacteristicTags = tags;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CMTimeRange timeRange = track.timeRange;
|
||||||
newTrack.duration = timeRange.duration.value / timeRange.duration.timescale * 1000;
|
newTrack.duration = timeRange.duration.value / timeRange.duration.timescale * 1000;
|
||||||
|
|
||||||
[self addTrack:newTrack];
|
[self addTrack:newTrack];
|
||||||
@@ -314,16 +353,13 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
[self convertMetadata];
|
[self convertMetadata];
|
||||||
|
|
||||||
[pool release];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the AVAsset metadata to the MP42File format
|
* Converts the AVAsset metadata to the MP42Metadata format
|
||||||
*/
|
*/
|
||||||
- (void)convertMetadata {
|
- (void)convertMetadata {
|
||||||
NSDictionary *commonItemsDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Name", AVMetadataCommonKeyTitle,
|
NSDictionary *commonItemsDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Name", AVMetadataCommonKeyTitle,
|
||||||
@@ -354,15 +390,22 @@
|
|||||||
_metadata = [[MP42Metadata alloc] init];
|
_metadata = [[MP42Metadata alloc] init];
|
||||||
|
|
||||||
for (NSString *commonKey in commonItemsDict.allKeys) {
|
for (NSString *commonKey in commonItemsDict.allKeys) {
|
||||||
NSArray<AVMetadataItem *> *items = [AVMetadataItem metadataItemsFromArray:_localAsset.commonMetadata withKey:commonKey keySpace:AVMetadataKeySpaceCommon];
|
NSArray<AVMetadataItem *> *items = [AVMetadataItem metadataItemsFromArray:_localAsset.commonMetadata
|
||||||
if ([items count]) {
|
withKey:commonKey
|
||||||
[_metadata setTag:[[items lastObject] value] forKey:[commonItemsDict objectForKey:commonKey]];
|
keySpace:AVMetadataKeySpaceCommon];
|
||||||
|
if (items.count) {
|
||||||
|
[_metadata setTag:items.lastObject.value forKey:commonItemsDict[commonKey]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NSArray<AVMetadataItem *> *items = [AVMetadataItem metadataItemsFromArray:_localAsset.commonMetadata withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKeySpaceCommon];
|
// Copy the artowrks
|
||||||
if ([items count]) {
|
NSArray<AVMetadataItem *> *items = [AVMetadataItem metadataItemsFromArray:_localAsset.commonMetadata
|
||||||
id artworkData = [[items lastObject] value];
|
withKey:AVMetadataCommonKeyArtwork
|
||||||
|
keySpace:AVMetadataKeySpaceCommon];
|
||||||
|
|
||||||
|
for (AVMetadataItem *item in items) {
|
||||||
|
NSData *artworkData = item.dataValue;
|
||||||
|
|
||||||
if ([artworkData isKindOfClass:[NSData class]]) {
|
if ([artworkData isKindOfClass:[NSData class]]) {
|
||||||
NSImage *image = [[NSImage alloc] initWithData:artworkData];
|
NSImage *image = [[NSImage alloc] initWithData:artworkData];
|
||||||
[_metadata.artworks addObject:[[[MP42Image alloc] initWithImage:image] autorelease]];
|
[_metadata.artworks addObject:[[[MP42Image alloc] initWithImage:image] autorelease]];
|
||||||
@@ -373,7 +416,7 @@
|
|||||||
NSArray<NSString *> *availableMetadataFormats = [_localAsset availableMetadataFormats];
|
NSArray<NSString *> *availableMetadataFormats = [_localAsset availableMetadataFormats];
|
||||||
|
|
||||||
if ([availableMetadataFormats containsObject:AVMetadataFormatiTunesMetadata]) {
|
if ([availableMetadataFormats containsObject:AVMetadataFormatiTunesMetadata]) {
|
||||||
NSArray *itunesMetadata = [_localAsset metadataForFormat:AVMetadataFormatiTunesMetadata];
|
NSArray<AVMetadataItem *> *itunesMetadata = [_localAsset metadataForFormat:AVMetadataFormatiTunesMetadata];
|
||||||
|
|
||||||
NSDictionary *itunesMetadataDict = [NSDictionary dictionaryWithObjectsAndKeys:
|
NSDictionary *itunesMetadataDict = [NSDictionary dictionaryWithObjectsAndKeys:
|
||||||
@"Album", AVMetadataiTunesMetadataKeyAlbum,
|
@"Album", AVMetadataiTunesMetadataKeyAlbum,
|
||||||
@@ -446,10 +489,10 @@
|
|||||||
@"Executive Producer", AVMetadataiTunesMetadataKeyExecProducer,
|
@"Executive Producer", AVMetadataiTunesMetadataKeyExecProducer,
|
||||||
nil];
|
nil];
|
||||||
|
|
||||||
for (NSString *itunesKey in [itunesMetadataDict allKeys]) {
|
for (NSString *itunesKey in itunesMetadataDict.allKeys) {
|
||||||
items = [AVMetadataItem metadataItemsFromArray:itunesMetadata withKey:itunesKey keySpace:AVMetadataKeySpaceiTunes];
|
items = [AVMetadataItem metadataItemsFromArray:itunesMetadata withKey:itunesKey keySpace:AVMetadataKeySpaceiTunes];
|
||||||
if (items.count) {
|
if (items.count) {
|
||||||
[_metadata setTag:[[items lastObject] value] forKey:[itunesMetadataDict objectForKey:itunesKey]];
|
[_metadata setTag:items.lastObject.value forKey:itunesMetadataDict[itunesKey]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -495,7 +538,7 @@
|
|||||||
for (NSString *qtKey in quicktimeMetadataDict.allKeys) {
|
for (NSString *qtKey in quicktimeMetadataDict.allKeys) {
|
||||||
items = [AVMetadataItem metadataItemsFromArray:quicktimeMetadata withKey:qtKey keySpace:AVMetadataKeySpaceQuickTimeUserData];
|
items = [AVMetadataItem metadataItemsFromArray:quicktimeMetadata withKey:qtKey keySpace:AVMetadataKeySpaceQuickTimeUserData];
|
||||||
if (items.count) {
|
if (items.count) {
|
||||||
[_metadata setTag:[[items lastObject] value] forKey:[quicktimeMetadataDict objectForKey:qtKey]];
|
[_metadata setTag:items.lastObject.value forKey:quicktimeMetadataDict[qtKey]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -528,7 +571,7 @@
|
|||||||
for (NSString *qtUserDataKey in quicktimeUserDataMetadataDict.allKeys) {
|
for (NSString *qtUserDataKey in quicktimeUserDataMetadataDict.allKeys) {
|
||||||
items = [AVMetadataItem metadataItemsFromArray:quicktimeUserDataMetadata withKey:qtUserDataKey keySpace:AVMetadataKeySpaceQuickTimeUserData];
|
items = [AVMetadataItem metadataItemsFromArray:quicktimeUserDataMetadata withKey:qtUserDataKey keySpace:AVMetadataKeySpaceQuickTimeUserData];
|
||||||
if (items.count) {
|
if (items.count) {
|
||||||
[_metadata setTag:[[items lastObject] value] forKey:[quicktimeUserDataMetadataDict objectForKey:qtUserDataKey]];
|
[_metadata setTag:items.lastObject.value forKey:quicktimeUserDataMetadataDict[qtUserDataKey]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -920,8 +963,9 @@
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (trackDuration)
|
if (trackDuration) {
|
||||||
MP4SetTrackIntegerProperty(fileHandle, track.Id, "tkhd.duration", trackDuration);
|
MP4SetTrackIntegerProperty(fileHandle, track.Id, "tkhd.duration", trackDuration);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return YES;
|
return YES;
|
||||||
|
|||||||
Reference in new issue
Block a user