avsubtitleWriter demo解析(三):SubtitlesTextReader

前面两篇 我们已经完成了subtitle类的几个方法的说明,现在我们回过头来了解SubtitlesTextReader。这当然我们前面已经结合subtitle的初始化讲到了SubtitlesTextReader的初始化。除了实例初始化方法initWithText外,还有一个便捷初始化方法subtitlesTextReaderWithText。

这部分我们只介绍剩余的工作。

第一个方法copyFormatDescription,这是每个字幕的结构描述,因为都是一样的,所以只需要返回字幕数组中第一个元素的结构描述就行。这个结构描述不是字幕的一个属性,是一个方法,在前面一篇我们已经讲过了。

- (CMFormatDescriptionRef)copyFormatDescription
{
	// Take the format description from the first object. They are all the same since the display flag are all the same.
	return [[_subtitles firstObject] copyFormatDescription];
}

第二个方法是metadata

- (NSArray *)metadata
{
	NSMutableArray *mutableMetadata = [NSMutableArray array];
	
	// All subtitles must have the AVMediaCharacteristicTranscribesSpokenDialogForAccessibility characteristic.
	AVMutableMetadataItem *spokenItem = [AVMutableMetadataItem metadataItem];
	[spokenItem setKey:AVMetadataQuickTimeUserDataKeyTaggedCharacteristic];
	[spokenItem setKeySpace:AVMetadataKeySpaceQuickTimeUserData];
	[spokenItem setValue:AVMediaCharacteristicTranscribesSpokenDialogForAccessibility];
	[mutableMetadata addObject:spokenItem];
	
	if (_wantsSDH)
	{
		// SDH subtitles must also have the AVMediaCharacteristicDescribesMusicAndSoundForAccessibility characteristic.
		AVMutableMetadataItem *describesItem = [AVMutableMetadataItem metadataItem];
		[describesItem setKey:AVMetadataQuickTimeUserDataKeyTaggedCharacteristic];
		[describesItem setKeySpace:AVMetadataKeySpaceQuickTimeUserData];
		[describesItem setValue:AVMediaCharacteristicDescribesMusicAndSoundForAccessibility];
		
		[mutableMetadata addObject:describesItem];
	}
	
	return [mutableMetadata copy];
}

这边设置QuickTime Metadata,这部分可以参看文档AV Foundation QuickTime Constants,AV Foundation Constants Reference(QuickTime User Data Keys,QuickTime User Data,QuickTime Metadata Keys,AVMediaSelectionOption Constants)

The key property contains the true key used to identify the contents of the metadata item. This value is specific to the key space of the metadata item.


The key space specified by this property is typically the default key space for the metadata container in which the metadata item is stored.
AV Foundation uses key spaces to group related sets of keys. For example, the framework defines different key spaces for common keys, iTunes keys, ID3 keys, and QuickTime keys. Key spaces aid in filtering arrays of metadata items.


desirable characteristics of legible media may include AVMediaCharacteristicTranscribesSpokenDialogForAccessibility and AVMediaCharacteristicDescribesMusicAndSoundForAccessibility.

第三个方法是copyNextSampleBuffer,你可以预见我们要连续copy buffer,因此需要这么一个方法来指示进度。

- (CMSampleBufferRef)copyNextSampleBuffer
{
	CMSampleBufferRef sampleBuffer = NULL;
	
	if (_index < _subtitles.count)
	{
		sampleBuffer = [(Subtitle *)_subtitles[_index] copySampleBuffer];
		_index++;
	}
	
	return sampleBuffer;
}

至此我们完成了前期的准备工作。当然接下去是调用SubtitlesTextReader来创建input。下面的的确也繁琐。


本篇中设置metaData是个难点,你需要去了解下这部分常量的意思。

你可能感兴趣的:(Mac开发)