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_VideoまたはkCMMediaType_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 に答える