A quick approach to extracting the audio and video codec associated with the 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 transfer the AVURLAssetmovie and kCMMediaType_Videoor kCMMediaType_Audioto receive video and audio codecs, respectively.
The function toString()converts the presentation of FourCharCodethe codec format into a human-readable string and can be provided as an extension method to 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())
}
}
source
share