8

我正在尝试找到用于压缩电影的编解码器。我确定我是否需要以某种方式使用 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];
 }   
4

4 回答 4

6

检索与电影相关的音频和视频编解码器的 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来分别检索视频和音频编解码器。

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())
    }
}
于 2016-04-15T19:31:11.607 回答
3

@jbat100 答案的可读性稍强的版本(对于那些和我一样困惑的人#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);
于 2014-04-17T21:13:44.773 回答
3

我认为这绝对比应该的要难

    #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);

        }
    }
于 2013-05-16T15:38:31.057 回答
1
NSArray *formatDescriptions = videoTrack.formatDescriptions;
NSString *formats = [self mediaFormatWithFormatDescriptions:formatDescriptions];

- (NSString *)mediaFormatWithFormatDescriptions:(NSArray *)formatDescriptions {
    NSMutableString *format = [[NSMutableString alloc] init];
    for (int i = 0; i < formatDescriptions.count; i++) {
        CMFormatDescriptionRef desc =
            (__bridge CMFormatDescriptionRef)formatDescriptions[i];
        NSString *type = FourCCString(CMFormatDescriptionGetMediaType(desc));
        NSString *subType = FourCCString(CMFormatDescriptionGetMediaSubType(desc));
        [format appendFormat:@"%@/%@", type, subType];
        if (i < formatDescriptions.count - 1) {
            [format appendString:@","];
        }
    }
    return format;
}

static NSString * FourCCString(FourCharCode code) {
    NSString *result = [NSString stringWithFormat:@"%c%c%c%c",
                        (code >> 24) & 0xff,
                        (code >> 16) & 0xff,
                        (code >> 8) & 0xff,
                        code & 0xff];
    NSCharacterSet *characterSet = [NSCharacterSet whitespaceCharacterSet];
    return [result stringByTrimmingCharactersInSet:characterSet];
}
于 2019-10-22T03:46:52.433 回答