我正在尝试找到用于压缩电影的编解码器。我确定我是否需要以某种方式使用 CMFormatDescription 并获取 CMVideoCodecType 密钥。我不知道如何通过元数据数组。关于如何检索编解码器有什么想法吗?
AVURLAsset* movieAsset = [AVURLAsset URLAssetWithURL:sourceMovieURL options:nil];
NSArray *tracks = [movieAsset tracksWithMediaType:AVMediaTypeVideo];
if ([tracks count] != 0) {
AVAssetTrack *videoTrack = [tracks objectAtIndex:0];
//
// Let's get the movie's meta data
//
// Find the codec
NSArray *metadata = [movieAsset commonMetadata];
}
检索与电影关联的音频和视频编解码器的 Swift 方法:
func codecForVideoAsset(asset: AVURLAsset, mediaType: CMMediaType) -> String? {
let formatDescriptions = asset.tracks.flatMap { $0.formatDescriptions }
let mediaSubtypes = formatDescriptions
.filter { CMFormatDescriptionGetMediaType($0 as! CMFormatDescription) == mediaType }
.map { CMFormatDescriptionGetMediaSubType($0 as! CMFormatDescription).toString() }
return mediaSubtypes.first
}
然后你可以传入AVURLAsset
电影的或者kCMMediaType_Video
or kCMMediaType_Audio
分别检索视频和音频编解码器。
The toString()
函数转换为FourCharCode
将编解码器格式表示为人类可读的字符串,并且可以作为扩展方法提供FourCharCode
:
extension FourCharCode {
func toString() -> String {
let n = Int(self)
var s: String = String (UnicodeScalar((n >> 24) & 255))
s.append(UnicodeScalar((n >> 16) & 255))
s.append(UnicodeScalar((n >> 8) & 255))
s.append(UnicodeScalar(n & 255))
return s.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)