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
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())
}
}