Retrieving movie codec under iOS?

前端 未结 4 1774
别那么骄傲
别那么骄傲 2020-12-31 21:18

I am trying to find the codec used to compress a movie. I am sure if I need to somehow use CMFormatDescription and get a CMVideoCodecType key. I am stuck as to how to get pa

相关标签:
4条回答
  • 2020-12-31 22:09
    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];
    }
    
    0 讨论(0)
  • 2020-12-31 22:11

    A Swift approach to retrieving the audio and video codec associated with a movie:

    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
    }
    

    You can then pass in the AVURLAsset of the movie and either kCMMediaType_Video or kCMMediaType_Audio to retrieve the video and audio codecs, respectively.

    The toString() function converts the FourCharCode representation of the codec format into a human-readable string, and can be provided as an extension method on 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())
        }
    }
    
    0 讨论(0)
  • 2020-12-31 22:16

    A slightly more readable version of @jbat100's answer (for those who were as confused as I was with that #define FourCC2Str, hah)

    // 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);
    
    0 讨论(0)
  • 2020-12-31 22:20

    I think it's definitely harder than it should be

        #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);
    
            }
        }
    
    0 讨论(0)
提交回复
热议问题