在iOS下检索电影编解码器?

我试图find用于压缩电影的编解码器。 我相信如果我需要以某种方式使用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]; } 

我认为这肯定比应该更难

  #define FourCC2Str(code) (char[5]){(code >> 24) & 0xFF, (code >> 16) & 0xFF, (code >> 8) & 0xFF, code & 0xFF, 0} if ([assetTrack.mediaType isEqualToString:AVMediaTypeVideo]) { for (id formatDescription in assetTrack.formatDescriptions) { NSLog(@"formatDescription: %@", formatDescription); CMFormatDescriptionRef desc = (__bridge CMFormatDescriptionRef)formatDescription; //CMMediaType mediaType = CMFormatDescriptionGetMediaType(desc); // CMVideoCodecType is typedefed to CMVideoCodecType CMVideoCodecType codec = CMFormatDescriptionGetMediaSubType(desc); NSString* codecString = [NSString stringWithCString:(const char *)FourCC2Str(codec) encoding:NSUTF8StringEncoding]; NSLog(@"%@", codecString); } } 

@ jbat100的答案是一个稍微可读的版本(对于那些和我一样对#define FourCC2Str感到困惑的人来说, #define FourCC2Str

 // Get your format description from whichever track you want CMFormatDescriptionRef formatHint; // Get the codec and correct endianness CMVideoCodecType formatCodec = CFSwapInt32BigToHost(CMFormatDescriptionGetMediaSubType(formatHint)); // add 1 for null terminator char formatCodecBuf[sizeof(CMVideoCodecType) + 1] = {0}; memcpy(formatCodecBuf, &formatCodec, sizeof(CMVideoCodecType)); NSString *formatCodecString = @(formatCodecBuf); 

检索与电影关联的audio和video编解码器的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_VideokCMMediaType_Audio以分别检索video和audio编解码器。

toString()函数将编解码器格式的FourCharCode表示forms转换为可读的string,并且可以在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()) } }