Retrieving movie codec under iOS?

前端 未结 4 1773
别那么骄傲
别那么骄傲 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: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())
        }
    }
    

提交回复
热议问题